Skip to content

Commit

Permalink
Remove redirections from the functions, handled by CF
Browse files Browse the repository at this point in the history
  • Loading branch information
gpmayorga committed Jun 26, 2024
1 parent 793f869 commit ce985a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
26 changes: 12 additions & 14 deletions functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,25 @@ const corsWhitelist = [
const testEnvRegex = new RegExp('https://preview-pr[0-9]*(.k-f.dev)')

exports.handler = async (req: Request, res: Response) => {
// Check for redirects first
const redirect = routes.find(route => route.source && req.path.endsWith(route.source));
if (redirect) {
return res.redirect(redirect.statusCode, redirect.destination);
if (routes.length < 0) {
return res.status(400).send('No functions defined')
}

// Existing CORS and route handling
const origin = req.get('origin');
const origin = req.get('origin')

if (corsWhitelist.indexOf(origin) !== -1 || testEnvRegex.test(origin)) {
res.set('Access-Control-Allow-Origin', origin);
res.set('Access-Control-Allow-Methods', ['GET', 'POST']);
res.set('Access-Control-Allow-Origin', origin)
res.set('Access-Control-Allow-Methods', ['GET', 'POST'])
} else {
return res.status(405).send('Not allowed');
return res.status(405).send('Not allowed')
}

for (let route of routes) {
if (route.name && req.path.replace('/', '') === route.name) {
const method = require(`./src/${route.name}`);
return method.default(req, res, route?.options ?? {});
if (req.path.replace('/', '') === route.name) {
const method = require(`./src/${route.name}`)

return method.default(req, res, route?.options ?? {})
}
}

return res.status(500).send('An error occurred');
return res.status(500).send('An error occured')
}
12 changes: 1 addition & 11 deletions functions/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,5 @@
{
"name": "getExample",
"options": {}
},
{
"source": "/parachain/crowdloan",
"destination": "/",
"statusCode": 301
},
{
"source": "/altair/crowdloan",
"destination": "/",
"statusCode": 301
}
}
]

0 comments on commit ce985a9

Please sign in to comment.