From dda6192cab32712570ff0b0533f65ae41241a51f Mon Sep 17 00:00:00 2001 From: Thaddeus Kuah Date: Thu, 22 Aug 2024 11:53:45 +0800 Subject: [PATCH] feat: add config option for redirects on base url Signed-off-by: Thaddeus Kuah --- src/index.ts | 2 ++ src/routes/index.ts | 4 ++++ src/types.ts | 1 + 3 files changed, 7 insertions(+) diff --git a/src/index.ts b/src/index.ts index 1ebb6a6..0ab55a5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,6 +17,7 @@ const host = process.env['HOST'] || 'localhost'; const port = process.env['PORT'] || '3000'; const level = process.env['LOG_LEVEL'] || 'info'; const baseUrl = process.env['BASE_URL'] || `http://${host}:${port}`; +const baseUrlRedirect = process.env['BASE_URL_REDIRECT'] || ''; const prohibitedSlugs = process.env['PROHIBITED_SLUGS']?.split(',') || ['api']; const prohibitedCharacters = process.env['PROHIBITED_CHARACTERS_IN_SLUGS'] || '/'; @@ -56,6 +57,7 @@ for (const route of readFiles(path.join(__dirname, 'routes'))) { config: { info: { name, author, version }, baseUrl, + baseUrlRedirect, prohibitedSlugs, prohibitedCharacters: [...prohibitedCharacters], } as Config, diff --git a/src/routes/index.ts b/src/routes/index.ts index 729699b..ec0aaf5 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -5,6 +5,10 @@ export const routes: Route = (fastify, { config }, done) => { method: ['GET'], url: '/', handler: async (request, reply) => { + if (config.baseUrlRedirect.length > 0) { + reply.code(301).redirect(config.baseUrlRedirect); + return; + } reply.code(200).send(`${config.info.name} v${config.info.version} by ${config.info.author}` + '\n' + 'https://github.com/thaddeuskkr/nova' + diff --git a/src/types.ts b/src/types.ts index e3f2074..a9ebfac 100644 --- a/src/types.ts +++ b/src/types.ts @@ -9,6 +9,7 @@ export type Config = { version: string; }; baseUrl: string; + baseUrlRedirect: string; prohibitedSlugs: string[]; prohibitedCharacters: string[]; };