Подавать контент с другого маршрута на поддомен - PullRequest
0 голосов
/ 25 марта 2020

В фокусе нашей компании произошел общий сдвиг, когда мы отошли от подачи контента из сегментов S3 в соответствии со следующими пунктами обсуждения:

Теперь вместо этого мы хотим обслуживать его из другого места, внутреннего / внешнего маршрута внутри сайта. Шаги следующие:

  • Пользователь переходит на adoring-elion-23496793.ourwebsite.com (где adoring-elion-23496793 - это имя их сайта)

  • Внутри лямбда-функции выполняется API-вызов: https://us-central1-ourwebsite-dev.cloudfunctions.net/getWebsiteIdByName/adoring-elion-23496793

  • Возвращает websiteId для лямбда-функции в формате, например: { "websiteId": "0e7bzyt5IcGPMtX9wKzn" }

  • Затем функция Lambda берет websiteId и передает содержимое с URL ourwebsite.com/websites/0e7bzyt5IcGPMtX9wKzn на adoring-elion-23496793.ourwebsite.com

Я считаю, что шаги уже готовы для этого, например: jolly-kepler-12154838.ourwebsite.com в настоящее время обслуживает содержимое в расположении корзины S3. Как бы я обновил эту функцию, чтобы она работала исключительно с поддоменов? Я думаю, что потребуется только изменение функции Lambda, однако я не уверен, как сделать вызов нашего API, извлечь данные и использовать его для обслуживания контента:

'use strict';

// if the end of incoming Host header matches this string, 
// strip this part and prepend the remaining characters onto the request path,
// along with a new leading slash (otherwise, the request will be handled
// with an unmodified path, at the root of the bucket)

const remove_suffix = '.ourwebsite.com';

// provide the correct origin hostname here so that we send the correct 
// Host header to the S3 website endpoint

const origin_hostname = 'apps.opsonion.com.s3.amazonaws.com'; // see comments, below

exports.handler = (event, context, callback) => {
    const request = event.Records[0].cf.request;
    const headers = request.headers;
    const host_header = headers.host[0].value;

    if(host_header.endsWith(remove_suffix))
    {
        // prepend '/' + the subdomain onto the existing request path ("uri")
        request.uri = '/' + host_header.substring(0,host_header.length - remove_suffix.length) + request.uri;
    }

    // fix the host header so that S3 understands the request
    headers.host[0].value = origin_hostname;

    // return control to CloudFront with the modified request
    return callback(null,request);
};
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...