From f42b737d29da20bf4b19b4b83388909450cfa53d Mon Sep 17 00:00:00 2001 From: Yaroslav Pshenichnikov Date: Tue, 19 Dec 2023 14:03:57 +0300 Subject: [PATCH] ADD viewer request function --- docspace-reverse-proxy/index.mjs | 23 ------------------ docspace-viewer-request/index.mjs | 40 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 23 deletions(-) delete mode 100644 docspace-reverse-proxy/index.mjs create mode 100644 docspace-viewer-request/index.mjs diff --git a/docspace-reverse-proxy/index.mjs b/docspace-reverse-proxy/index.mjs deleted file mode 100644 index a5b6ca3..0000000 --- a/docspace-reverse-proxy/index.mjs +++ /dev/null @@ -1,23 +0,0 @@ -'use strict'; - -// Redirect from DS to WS if match -const workspaceRegex = /(\/products\/|\/addons\/|.aspx)/i; - -if (workspaceRegex.test(request.uri)) -{ - const newurl = `https://${tenantDomain.replace('onlyoffice.io', 'teamlab.info')}${request.uri}?${request.querystring}`; - console.log("redirect to: %s", newurl); - const response = { - status: '302', - statusDescription: 'Found', - headers: { - location: [{ - key: 'Location', - value: newurl, - }], - }, - }; - - return callback(null, response); - -} \ No newline at end of file diff --git a/docspace-viewer-request/index.mjs b/docspace-viewer-request/index.mjs new file mode 100644 index 0000000..2841bd9 --- /dev/null +++ b/docspace-viewer-request/index.mjs @@ -0,0 +1,40 @@ +'use strict'; + +export const handler = async (event, context, callback) => { + console.log(JSON.stringify(event)); + + // Extract the request from the CloudFront event that is sent to Lambda@Edge + var request = event.Records[0].cf.request; + + const headers = request.headers; + let tenantDomain = headers.host[0].value; + + console.log("Tenant domain: %s", tenantDomain); + + const workspaceRegex = /(\/products\/|\/addons\/|.aspx)/i; + + if (workspaceRegex.test(request.uri)) + { + const newurl = `https://${tenantDomain.replace('onlyoffice.com', 'onlyoffice.co')}${request.uri}?${request.querystring}`; + console.log("redirect to: %s", newurl); + + const response = { + status: '302', + statusDescription: 'Found', + headers: { + location: [{ + key: 'Location', + value: newurl, + }], + 'cache-control': [{ // Add cache-control header + key: 'Cache-Control', + value: 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0' + }], + }, + }; + + return callback(null, response); + } + + return callback(null, request); +} \ No newline at end of file