Skip to content

Commit

Permalink
feat: add config option for redirects on base url
Browse files Browse the repository at this point in the history
Signed-off-by: Thaddeus Kuah <[email protected]>
  • Loading branch information
thaddeuskkr committed Aug 22, 2024
1 parent 8b722a4 commit dda6192
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] || '/';

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' +
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Config = {
version: string;
};
baseUrl: string;
baseUrlRedirect: string;
prohibitedSlugs: string[];
prohibitedCharacters: string[];
};

0 comments on commit dda6192

Please sign in to comment.