magento / magento2-upward-connector

Magento module for routing front-end requests to UPWARD-PHP
Open Software License 3.0
24 stars 27 forks source link

Generate MAGENTO_BACKEND_URL at Runtime #4

Open bbatsche opened 5 years ago

bbatsche commented 5 years ago

Venia Concept requires a MAGENTO_BACKEND_URL environment variable. This can be generated in one of three ways.

  1. From Magento's own configs. Look up web/secure/base_url or web/unsecure/base_url and set the value accordingly.

  2. From MAGENTO_CLOUD_ROUTES

The MAGENTO_CLOUD_ROUTES environment variable is the contains the live URLs Magento Cloud uses. We should use this data to set MAGENTO_BACKEND_URL before forwarding the request on to UPWARD & Venia.

The value of MAGENTO_CLOUD_ROUTES is base64 encoded JSON. Once decoded it looks like the following:

{
   "https://venia-test2-btfocsy-lnw62ehme7md2.us-4.magentosite.cloud/" : {
      "upstream" : "mymagento",
      "id" : null,
      "original_url" : "https://{default}/",
      "restrict_robots" : true,
      "type" : "upstream",
      "ssi" : {
         "enabled" : false
      },
      "primary" : false,
      "tls" : {
         "client_certificate_authorities" : [],
         "client_authentication" : null,
         "strict_transport_security" : {
            "preload" : null,
            "enabled" : null,
            "include_subdomains" : null
         },
         "min_version" : null
      },
      "cache" : {
         "headers" : [
            "Accept",
            "Accept-Language"
         ],
         "enabled" : true,
         "cookies" : [
            "*"
         ],
         "default_ttl" : 0
      }
   },
   "http://venia-test2-btfocsy-lnw62ehme7md2.us-4.magentosite.cloud/" : {
      "upstream" : "mymagento",
      "id" : null,
      "original_url" : "http://{default}/",
      "restrict_robots" : true,
      "ssi" : {
         "enabled" : false
      },
      "type" : "upstream",
      "primary" : true,
      "tls" : {
         "strict_transport_security" : {
            "enabled" : null,
            "preload" : null,
            "include_subdomains" : null
         },
         "client_authentication" : null,
         "client_certificate_authorities" : [],
         "min_version" : null
      },
      "cache" : {
         "default_ttl" : 0,
         "enabled" : true,
         "headers" : [
            "Accept",
            "Accept-Language"
         ],
         "cookies" : [
            "*"
         ]
      }
   }
}

Look through the JSON and pick the appropriate key (preferring https)

  1. Fallback on localhost (not 100% certain this will work)