Skip to content

Commit dda6192

Browse files
committed
feat: add config option for redirects on base url
Signed-off-by: Thaddeus Kuah <[email protected]>
1 parent 8b722a4 commit dda6192

File tree

3 files changed

+7
-0
lines changed

3 files changed

+7
-0
lines changed

src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const host = process.env['HOST'] || 'localhost';
1717
const port = process.env['PORT'] || '3000';
1818
const level = process.env['LOG_LEVEL'] || 'info';
1919
const baseUrl = process.env['BASE_URL'] || `http://${host}:${port}`;
20+
const baseUrlRedirect = process.env['BASE_URL_REDIRECT'] || '';
2021
const prohibitedSlugs = process.env['PROHIBITED_SLUGS']?.split(',') || ['api'];
2122
const prohibitedCharacters = process.env['PROHIBITED_CHARACTERS_IN_SLUGS'] || '/';
2223

@@ -56,6 +57,7 @@ for (const route of readFiles(path.join(__dirname, 'routes'))) {
5657
config: {
5758
info: { name, author, version },
5859
baseUrl,
60+
baseUrlRedirect,
5961
prohibitedSlugs,
6062
prohibitedCharacters: [...prohibitedCharacters],
6163
} as Config,

src/routes/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export const routes: Route = (fastify, { config }, done) => {
55
method: ['GET'],
66
url: '/',
77
handler: async (request, reply) => {
8+
if (config.baseUrlRedirect.length > 0) {
9+
reply.code(301).redirect(config.baseUrlRedirect);
10+
return;
11+
}
812
reply.code(200).send(`${config.info.name} v${config.info.version} by ${config.info.author}` +
913
'\n' +
1014
'https://github.com/thaddeuskkr/nova' +

src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type Config = {
99
version: string;
1010
};
1111
baseUrl: string;
12+
baseUrlRedirect: string;
1213
prohibitedSlugs: string[];
1314
prohibitedCharacters: string[];
1415
};

0 commit comments

Comments
 (0)