Created
October 8, 2024 02:57
-
-
Save g3luka/770d5355c751e66b7ae4280fbb3d0f42 to your computer and use it in GitHub Desktop.
WordPress Lambda Middleware Replace Domain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
const WORDPRESS_ENDPOINT = process.env.WORDPRESS_ENDPOINT; | |
const REPLACE_DOMAIN_FROM = process.env.REPLACE_DOMAIN_FROM; | |
const REPLACE_DOMAIN_TO = process.env.REPLACE_DOMAIN_TO; | |
const CACHE_SECONDS_200 = process.env.CACHE_SECONDS_200 || 60 * 5; // 5 minutes | |
const CACHE_SECONDS_301 = process.env.CACHE_SECONDS_301 || 60 * 30; // 30 minutes | |
const CACHE_SECONDS_404 = process.env.CACHE_SECONDS_404 || 60 * 15; // 15 minutes | |
exports.handler = async (event) => { | |
try { | |
const body = await getWordPressHTML(event.path); | |
return await successResponse(body); | |
} catch (error) { | |
const redirect = await redirectResponse(error); | |
if (redirect) return redirect; | |
return notFoundResponse(error); | |
} | |
}; | |
const getWordPressHTML = async (path) => { | |
let endpoint = `${WORDPRESS_ENDPOINT}${path}`; | |
endpoint = encodeURI(endpoint.endsWith("/") ? endpoint : endpoint + "/"); | |
try { | |
const response = await axios.get(endpoint, { maxRedirects: 0 }); | |
return response.data; | |
} catch (error) { | |
throw { | |
path: error.request.path, | |
status: parseInt(error.response.status), | |
location: error.response.headers.location, | |
data: error.response.status == 404 ? error.response.data : "", | |
}; | |
} | |
} | |
const replaceDomain = (content) => { | |
return content.replace(REPLACE_DOMAIN_FROM, REPLACE_DOMAIN_TO); | |
}; | |
const successResponse = async (body) => { | |
return { | |
statusCode: 200, | |
body: replaceDomain(body), | |
headers: { | |
"cache-control": `max-age=${CACHE_SECONDS_200}`, | |
"content-type": "text/html; charset=UTF-8", | |
}, | |
}; | |
}; | |
const notFoundResponse = (error) => { | |
return { | |
statusCode: 404, | |
body: replaceDomain(error.data), | |
headers: { | |
"cache-control": `max-age=${CACHE_SECONDS_404}`, | |
"content-type": "text/html; charset=utf-8", | |
}, | |
} | |
}; | |
const redirectResponse = async (error) => { | |
if (error.status !== 301) return; | |
return { | |
statusCode: 301, | |
body: null, | |
headers: { | |
"cache-control": `max-age=${CACHE_SECONDS_301}`, | |
"location": replaceDomain(error.location), | |
}, | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "wordpress-lambda-middleware", | |
"license": "MIT", | |
"scripts": { | |
"deploy:prd": "sls deploy --stage=prd", | |
"deploy:stg": "sls deploy --stage=stg", | |
"package:prd": "sls package --stage=prd", | |
"package:stg": "sls package --stage=stg" | |
}, | |
"devDependencies": { | |
"aws-sdk": "^2.1274.0", | |
"serverless": "^3.25.1" | |
}, | |
"dependencies": { | |
"axios": "^1.2.1" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
service: wordpress-lambda-middleware | |
provider: | |
name: aws | |
region: ${opt:region, 'us-east-1'} | |
runtime: nodejs20.x | |
stage: ${opt:stage, 'stg'} | |
logRetentionInDays: 14 | |
tracing: | |
lambda: true | |
apiGateway: true | |
deploymentBucket: | |
name: ----> YOUR-S3-DEPLOY-BUCKET <---- | |
iam: | |
role: | |
statements: | |
- Effect: Allow | |
Action: | |
- s3:* | |
Resource: | |
- "arn:aws:s3:::${----> YOUR-S3-DEPLOY-BUCKET <----}/*" | |
- Effect: "Allow" | |
Action: | |
- "xray:PutTraceSegments" | |
- "xray:PutTelemetryRecords" | |
Resource: | |
- "*" | |
functions: | |
app: | |
description: WordPress Middleware | |
handler: src/app.handler | |
memorySize: 1024 | |
timeout: 30 | |
environment: | |
APP_ENVIRONMENT: ${self:provider.stage} | |
WORDPRESS_ENDPOINT: '' | |
REPLACE_DOMAIN_FROM: '' | |
REPLACE_DOMAIN_TO: '' | |
CACHE_SECONDS_200: 300 | |
CACHE_SECONDS_301: 1800 | |
CACHE_SECONDS_404: 900 | |
events: | |
- http: | |
path: / | |
method: get | |
- http: | |
path: '/{proxy+}' | |
method: get | |
package: | |
excludeDevDependencies: true | |
patterns: | |
- '!**' | |
- node_modules/** | |
- 'src/**' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment