init
This commit is contained in:
35
services/api/src/app.ts
Normal file
35
services/api/src/app.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import "dotenv/config";
|
||||
import express from "express";
|
||||
import db from "./lib/db.js";
|
||||
import crypto from "crypto";
|
||||
import morgan from "morgan";
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use(morgan("dev"));
|
||||
|
||||
app.get("/", async (req, res, next) => {
|
||||
const notes = await db.selectFrom("notes").selectAll().execute();
|
||||
|
||||
res.json({ notes });
|
||||
});
|
||||
|
||||
app.post("/", async (req, res, next) => {
|
||||
const created = await db
|
||||
.insertInto("notes")
|
||||
.values({
|
||||
title: "test",
|
||||
content: "text",
|
||||
completed: true,
|
||||
uuid: crypto.randomUUID(),
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
completed_at: new Date(),
|
||||
})
|
||||
.returningAll()
|
||||
.executeTakeFirstOrThrow();
|
||||
|
||||
res.json({ created });
|
||||
});
|
||||
|
||||
export { app };
|
||||
10
services/api/src/index.ts
Normal file
10
services/api/src/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import "dotenv/config";
|
||||
import { app } from "./app.js";
|
||||
import { shutdown } from "./lib/shutdown.js";
|
||||
|
||||
const server = app.listen(process.env.PORT, () => {
|
||||
console.log(`App running on port ${process.env.PORT} !`);
|
||||
});
|
||||
|
||||
process.on("SIGTERM", () => shutdown("SIGTERM", server));
|
||||
process.on("SIGINT", () => shutdown("SIGINT", server));
|
||||
13
services/api/src/lib/db.ts
Normal file
13
services/api/src/lib/db.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Kysely, PostgresDialect } from "kysely";
|
||||
import type { DB } from "kysely-codegen";
|
||||
import { Pool } from "pg";
|
||||
|
||||
const db = new Kysely<DB>({
|
||||
dialect: new PostgresDialect({
|
||||
pool: new Pool({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
export default db;
|
||||
26
services/api/src/lib/shutdown.ts
Normal file
26
services/api/src/lib/shutdown.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import type { SignalConstants } from "node:os";
|
||||
import db from "./db.js";
|
||||
import type { Server } from "node:http";
|
||||
|
||||
export const shutdown = async (
|
||||
signal: keyof SignalConstants,
|
||||
server: Server,
|
||||
) => {
|
||||
console.log(`${signal} received. Shutting down gracefully...`);
|
||||
|
||||
server.close(async () => {
|
||||
try {
|
||||
await db.destroy();
|
||||
console.log("Database connection closed.");
|
||||
process.exit(0);
|
||||
} catch (err) {
|
||||
console.error("Error during shutdown:", err);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
console.error("Forcing shutdown after timeout");
|
||||
process.exit(1);
|
||||
}, 10000);
|
||||
};
|
||||
Reference in New Issue
Block a user