mirror of
https://github.com/movie-web/backend.git
synced 2025-09-13 13:03:26 +00:00
Register routes before metrics
Co-authored-by: mrjvs <mistrjvs@gmail.com>
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import { setupFastify, startFastify } from '@/modules/fastify';
|
||||
import {
|
||||
setupFastify,
|
||||
setupFastifyRoutes,
|
||||
startFastify,
|
||||
} from '@/modules/fastify';
|
||||
import { setupJobs } from '@/modules/jobs';
|
||||
import { setupMetrics } from '@/modules/metrics';
|
||||
import { setupMikroORM } from '@/modules/mikro';
|
||||
@@ -18,6 +22,7 @@ async function bootstrap(): Promise<void> {
|
||||
await setupMetrics(app);
|
||||
await setupJobs();
|
||||
|
||||
await setupFastifyRoutes(app);
|
||||
await startFastify(app);
|
||||
|
||||
log.info(`App setup, ready to accept connections`, {
|
||||
|
@@ -19,7 +19,6 @@ export async function setupFastify(): Promise<FastifyInstance> {
|
||||
logger: makeFastifyLogger(log) as any,
|
||||
trustProxy: conf.server.trustProxy,
|
||||
});
|
||||
let exportedApp: FastifyInstance | null = null;
|
||||
|
||||
app.setValidatorCompiler(validatorCompiler);
|
||||
app.setSerializerCompiler(serializerCompiler);
|
||||
@@ -55,26 +54,14 @@ export async function setupFastify(): Promise<FastifyInstance> {
|
||||
});
|
||||
});
|
||||
|
||||
// plugins & routes
|
||||
log.info(`setting up plugins and routes`, { evt: 'setup-plugins' });
|
||||
// plugins
|
||||
log.info(`setting up plugins`, { evt: 'setup-plugins' });
|
||||
await app.register(cors, {
|
||||
origin: conf.server.cors.split(' ').filter((v) => v.length > 0),
|
||||
credentials: true,
|
||||
});
|
||||
await app.register(
|
||||
async (api, opts, done) => {
|
||||
setupRoutes(api);
|
||||
|
||||
exportedApp = api;
|
||||
done();
|
||||
},
|
||||
{
|
||||
prefix: conf.server.basePath,
|
||||
},
|
||||
);
|
||||
|
||||
if (!exportedApp) throw new Error('no app to export in fastify');
|
||||
return exportedApp;
|
||||
return app;
|
||||
}
|
||||
|
||||
export function startFastify(app: FastifyInstance) {
|
||||
@@ -102,3 +89,16 @@ export function startFastify(app: FastifyInstance) {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export async function setupFastifyRoutes(app: FastifyInstance) {
|
||||
log.info(`setting up routes`, { evt: 'setup-plugins' });
|
||||
await app.register(
|
||||
async (api, opts, done) => {
|
||||
setupRoutes(api);
|
||||
done();
|
||||
},
|
||||
{
|
||||
prefix: conf.server.basePath,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@@ -1,5 +1,4 @@
|
||||
import { Session } from '@/db/models/Session';
|
||||
import { User } from '@/db/models/User';
|
||||
import { job } from '@/modules/jobs/job';
|
||||
|
||||
// every day at 12:00:00
|
||||
|
@@ -17,6 +17,7 @@ export type Metrics = {
|
||||
| 'status'
|
||||
| 'type'
|
||||
| 'provider_id'
|
||||
| 'embed_id'
|
||||
>;
|
||||
};
|
||||
|
||||
@@ -30,7 +31,13 @@ export function getMetrics() {
|
||||
export async function setupMetrics(app: FastifyInstance) {
|
||||
log.info(`Setting up metrics...`, { evt: 'start' });
|
||||
|
||||
await app.register(metricsPlugin, { endpoint: '/metrics' });
|
||||
await app.register(metricsPlugin, {
|
||||
endpoint: '/metrics',
|
||||
routeMetrics: {
|
||||
enabled: true,
|
||||
registeredRoutesOnly: true,
|
||||
},
|
||||
});
|
||||
|
||||
metrics = {
|
||||
user: new Counter({
|
||||
@@ -49,6 +56,7 @@ export async function setupMetrics(app: FastifyInstance) {
|
||||
'title',
|
||||
'tmdb_id',
|
||||
'type',
|
||||
'embed_id',
|
||||
],
|
||||
}),
|
||||
};
|
||||
|
Reference in New Issue
Block a user