Last active
June 9, 2024 02:05
-
-
Save jesse1981/c7ae2524e69718dd0574a88598f96138 to your computer and use it in GitHub Desktop.
This file contains 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 index = require('../index'); | |
const YAML = require('yaml'); | |
const fs = require('fs'); | |
const express = require('express'); | |
const router = express.Router(); | |
router.use(function timeLog (req,res,next) { | |
console.log('Time: ',Date.now()) | |
res.header('Access-Control-Allow-Origin', '*'); | |
next(); | |
}) | |
const serverless = YAML.parse(fs.readFileSync('./serverless.yml','utf8')); | |
function comparePathsWithRegex(apiPath, actualPath) { | |
// Regex pattern to match path segments, path parameters, and optional query string | |
const pathRegex = new RegExp(`^${apiPath.replace(/\{([^}]+)}/g, "([^/]+)")}(?:\\?.*)?$`); | |
return pathRegex.test(actualPath); | |
} | |
function extractPathParams(serverlessPath, actualPath) { | |
const params = {}; | |
const serverlessParts = serverlessPath.split("/"); | |
const actualParts = actualPath.split("/"); | |
if (serverlessParts.length !== actualParts.length) { | |
throw new Error("Serverless path and actual path have different lengths"); | |
} | |
for (let i = 0; i < serverlessParts.length; i++) { | |
const serverlessPart = serverlessParts[i]; | |
const actualPart = actualParts[i]; | |
if (serverlessPart && serverlessPart[0] === "{") { | |
const key = serverlessPart.slice(1, -1); | |
params[key] = actualPart; | |
} else if (serverlessPart !== actualPart) { | |
throw new Error("Serverless path and actual path do not match at segment:", i); | |
} | |
} | |
return params; | |
} | |
router.all('*', async (req,res) => { | |
let path = req.url, | |
evt = req.body || {}, | |
cxt = {}; | |
path = path.replace('/api','/') | |
for (var i in serverless.functions) { | |
let f = serverless.functions[i] | |
for (var x in f.events) { | |
let e = f.events[x]; | |
if (e.httpApi && | |
comparePathsWithRegex(e.httpApi.path,path) && | |
req.method.toLocaleLowerCase() == e.httpApi.method.toLocaleLowerCase()) { | |
cxt.httpMethod = req.method | |
try { | |
let pathParmam = extractPathParams(e.httpApi.path, path); | |
evt.pathParameters = pathParmam; | |
//console.log("evt",evt,"req.body",req.body) | |
Object.keys(req.body).forEach((k) => { evt[k] = req.body[k] }); | |
let result = await index.handler[String(f.handler).replace('index.handler.','')](evt,cxt); | |
res.header('Content-Type', 'application/json') | |
res.status(result.statusCode).send(result.body); | |
} | |
catch (e) { | |
console.log("API Error",e); | |
res.status(500).send(); | |
} | |
} | |
else if (e.httpApi && | |
comparePathsWithRegex(e.httpApi.path,path) && | |
req.method == 'OPTIONS' && | |
res.headersSent == false) { | |
res.header('Access-Control-Allow-Methods', 'GET,POST,OPTIONS'); | |
res.header('Access-Control-Allow-Headers', 'Content-Type'); | |
res.header('Access-Control-Allow-Origin', '*'); | |
res.status(200).send(); | |
} | |
} | |
} | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment