feat(fastify-core): deny CORS by default when origin unset, add graceful shutdown handlers
This commit is contained in:
parent
04f4a5f81e
commit
4863b62055
@ -30,8 +30,8 @@ export async function createServiceApp(options: ServiceAppOptions): Promise<Fast
|
|||||||
|
|
||||||
const app = Fastify({ logger });
|
const app = Fastify({ logger });
|
||||||
|
|
||||||
// CORS
|
// CORS — deny all origins when CORS_ORIGIN is not explicitly set
|
||||||
const origin = corsOrigin ? corsOrigin.split(',').map(o => o.trim()) : true;
|
const origin = corsOrigin ? corsOrigin.split(',').map(o => o.trim()) : false;
|
||||||
await app.register(cors, { origin });
|
await app.register(cors, { origin });
|
||||||
|
|
||||||
// OpenAPI spec (optional — consumer must have @fastify/swagger installed)
|
// OpenAPI spec (optional — consumer must have @fastify/swagger installed)
|
||||||
|
|||||||
@ -6,6 +6,16 @@ import type { FastifyApp, StartOptions } from './types.js';
|
|||||||
|
|
||||||
export async function startService(app: FastifyApp, options: StartOptions): Promise<void> {
|
export async function startService(app: FastifyApp, options: StartOptions): Promise<void> {
|
||||||
const { port, host = '0.0.0.0' } = options;
|
const { port, host = '0.0.0.0' } = options;
|
||||||
|
|
||||||
|
// Graceful shutdown on SIGTERM/SIGINT (Docker, K8s, Ctrl-C)
|
||||||
|
for (const signal of ['SIGTERM', 'SIGINT'] as const) {
|
||||||
|
process.on(signal, async () => {
|
||||||
|
app.log.info(`Received ${signal}, shutting down gracefully…`);
|
||||||
|
await app.close();
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await app.listen({ port, host });
|
await app.listen({ port, host });
|
||||||
app.log.info(`Service listening on ${host}:${port}`);
|
app.log.info(`Service listening on ${host}:${port}`);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user