generated from nholtware/service-template-express
Initial commit
This commit is contained in:
commit
107d03869b
28 changed files with 6437 additions and 0 deletions
28
src/bin/server.ts
Normal file
28
src/bin/server.ts
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { createApp } from "../app.js";
|
||||
import { parseConfig } from "../lib/config.js";
|
||||
import { createDb } from "../lib/db.js";
|
||||
import { createLogger } from "../lib/logger.js";
|
||||
import { createCache } from "../lib/redis.js";
|
||||
|
||||
const config = parseConfig(process.env);
|
||||
const logger = createLogger(config);
|
||||
const db = config.DATABASE_URL ? createDb(config.DATABASE_URL) : undefined;
|
||||
const cache = config.REDIS_URL ? createCache(config.REDIS_URL) : undefined;
|
||||
|
||||
const server = createApp({ config, logger, db, cache }).listen(
|
||||
config.PORT,
|
||||
() => {
|
||||
logger.info({ port: config.PORT }, "listening");
|
||||
},
|
||||
);
|
||||
|
||||
function shutdown(signal: string) {
|
||||
logger.info({ signal }, "shutting down");
|
||||
server.close(async () => {
|
||||
await Promise.allSettled([db?.close(), cache?.close()]);
|
||||
process.exit(0);
|
||||
});
|
||||
setTimeout(() => process.exit(1), 10_000).unref();
|
||||
}
|
||||
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
||||
process.on("SIGINT", () => shutdown("SIGINT"));
|
||||
Loading…
Add table
Add a link
Reference in a new issue