Intermediate 7: Connecting applications

Connect

Services

If you wanted to grant access to a database container to an app container, a relationship is required.

relationships:
    database: 'db:mysql'

Same goes for app containers, since you are essentially configuring a custom service as far as Platform.sh is concerned. If a Next.js application retrieved content via the environments internal network, it could do so through a relationship

relationships:
    api: "drupal:http"

The drupal app would then be accessible on the internal network at api.internal.

Routes

Some frontend frameworks don’t allow communication over http (which is the case on the internal network) and require https to consume content. In this case, data will have to be pulled from the publicly accessible URL for that container.
Let’s revisit the routes configuration:

https://api.{default}/:
  type: upstream
  upstream: "drupal:http"
  id: "api"
  cache:
    enabled: true
    cookies: ['/^SS?ESS/', '/^Drupal.visitor/']
https://www.api.{default}/:
  type: redirect
  to: "https://api.{default}/"
  
https://www.{default}/:
  type: redirect
  to: "https://www.{default}/"
https://{default}/:
  type: upstream
  primary: true
  id: "client"
  upstream: "nextjs:http"

So Next.js needs to retrieve the api subdomain marked URL, but the exact location will change for every environment (which is what the {default} placeholder ensures will be generated for you).

One way to do this is via environment variables.

Notice that the backend container in the above configuration contains an id attribute set to api. We can commit a special .environment file to the codebase to define environment variables that will be sourced post-build and during SSH sessions. We can use the built-in JSON-serializing tool jq to parse the backend URL for the current environment across all environments from the built-in variable PLATFORM_ROUTES.

export API_URL=$(echo $PLATFORM_ROUTES | base64 --decode | jq -r 'to_entries[] | select(.value.id == api) | .key')

At runtime, your application will be able to use this new API_URL environment variable to retrieve the currrent backend location.

Custom services

As a last note, whether you are attempting to communicate with another container via it’s URL or with a relationship, containers should be considered customer services.That is, they play by the same rules.
Frontend frameworks will often expect that a backend resource is available during its build, but on Platform.sh the build phases of both containers (frontend and backend) occur at the same time, making the backend data unavailable until at least the deploy phase.

This caveat will often lead developers to delay frontend builds to a later stage of the build-deploy pipeline (the deploy or post_deploy hook) to get around this.

Review

In this section, you looked deeper into interacting with Platform.sh projects, as well as how code can be added and reused to create more complex multi-application environments.

Congrats!

0

Comments

0 comments

Please sign in to leave a comment.

 

Didn't find what you were looking for?

New post