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 { setupJobs } from '@/modules/jobs';
|
||||||
import { setupMetrics } from '@/modules/metrics';
|
import { setupMetrics } from '@/modules/metrics';
|
||||||
import { setupMikroORM } from '@/modules/mikro';
|
import { setupMikroORM } from '@/modules/mikro';
|
||||||
@@ -18,6 +22,7 @@ async function bootstrap(): Promise<void> {
|
|||||||
await setupMetrics(app);
|
await setupMetrics(app);
|
||||||
await setupJobs();
|
await setupJobs();
|
||||||
|
|
||||||
|
await setupFastifyRoutes(app);
|
||||||
await startFastify(app);
|
await startFastify(app);
|
||||||
|
|
||||||
log.info(`App setup, ready to accept connections`, {
|
log.info(`App setup, ready to accept connections`, {
|
||||||
|
@@ -19,7 +19,6 @@ export async function setupFastify(): Promise<FastifyInstance> {
|
|||||||
logger: makeFastifyLogger(log) as any,
|
logger: makeFastifyLogger(log) as any,
|
||||||
trustProxy: conf.server.trustProxy,
|
trustProxy: conf.server.trustProxy,
|
||||||
});
|
});
|
||||||
let exportedApp: FastifyInstance | null = null;
|
|
||||||
|
|
||||||
app.setValidatorCompiler(validatorCompiler);
|
app.setValidatorCompiler(validatorCompiler);
|
||||||
app.setSerializerCompiler(serializerCompiler);
|
app.setSerializerCompiler(serializerCompiler);
|
||||||
@@ -55,26 +54,14 @@ export async function setupFastify(): Promise<FastifyInstance> {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// plugins & routes
|
// plugins
|
||||||
log.info(`setting up plugins and routes`, { evt: 'setup-plugins' });
|
log.info(`setting up plugins`, { evt: 'setup-plugins' });
|
||||||
await app.register(cors, {
|
await app.register(cors, {
|
||||||
origin: conf.server.cors.split(' ').filter((v) => v.length > 0),
|
origin: conf.server.cors.split(' ').filter((v) => v.length > 0),
|
||||||
credentials: true,
|
credentials: true,
|
||||||
});
|
});
|
||||||
await app.register(
|
|
||||||
async (api, opts, done) => {
|
|
||||||
setupRoutes(api);
|
|
||||||
|
|
||||||
exportedApp = api;
|
return app;
|
||||||
done();
|
|
||||||
},
|
|
||||||
{
|
|
||||||
prefix: conf.server.basePath,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!exportedApp) throw new Error('no app to export in fastify');
|
|
||||||
return exportedApp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function startFastify(app: FastifyInstance) {
|
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 { Session } from '@/db/models/Session';
|
||||||
import { User } from '@/db/models/User';
|
|
||||||
import { job } from '@/modules/jobs/job';
|
import { job } from '@/modules/jobs/job';
|
||||||
|
|
||||||
// every day at 12:00:00
|
// every day at 12:00:00
|
||||||
|
@@ -17,6 +17,7 @@ export type Metrics = {
|
|||||||
| 'status'
|
| 'status'
|
||||||
| 'type'
|
| 'type'
|
||||||
| 'provider_id'
|
| 'provider_id'
|
||||||
|
| 'embed_id'
|
||||||
>;
|
>;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -30,7 +31,13 @@ export function getMetrics() {
|
|||||||
export async function setupMetrics(app: FastifyInstance) {
|
export async function setupMetrics(app: FastifyInstance) {
|
||||||
log.info(`Setting up metrics...`, { evt: 'start' });
|
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 = {
|
metrics = {
|
||||||
user: new Counter({
|
user: new Counter({
|
||||||
@@ -49,6 +56,7 @@ export async function setupMetrics(app: FastifyInstance) {
|
|||||||
'title',
|
'title',
|
||||||
'tmdb_id',
|
'tmdb_id',
|
||||||
'type',
|
'type',
|
||||||
|
'embed_id',
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user