Scaffold and Phase 1
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import "dotenv/config";
|
||||
import { createServer } from "node:http";
|
||||
import { Server } from "socket.io";
|
||||
import type {
|
||||
ServerToClientEvents,
|
||||
ClientToServerEvents,
|
||||
InterServerEvents,
|
||||
SocketData,
|
||||
} from "@storybid/shared";
|
||||
|
||||
import { app } from "./app.js";
|
||||
import { registerSocketHandlers } from "./socket/index.js";
|
||||
import { prisma } from "./lib/prisma.js";
|
||||
|
||||
const PORT = parseInt(process.env["PORT"] ?? "3001", 10);
|
||||
|
||||
const httpServer = createServer(app);
|
||||
|
||||
export const io = new Server<
|
||||
ClientToServerEvents,
|
||||
ServerToClientEvents,
|
||||
InterServerEvents,
|
||||
SocketData
|
||||
>(httpServer, {
|
||||
cors: {
|
||||
origin: process.env["NODE_ENV"] === "production"
|
||||
? [process.env["PUBLIC_URL"] ?? "", process.env["CLIENT_URL"] ?? ""]
|
||||
: "*",
|
||||
credentials: true,
|
||||
},
|
||||
});
|
||||
|
||||
registerSocketHandlers(io);
|
||||
|
||||
httpServer.listen(PORT, () => {
|
||||
console.log(`[server] listening on http://localhost:${PORT}`);
|
||||
console.log(`[server] NODE_ENV=${process.env["NODE_ENV"] ?? "development"}`);
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
const shutdown = async () => {
|
||||
console.log("[server] shutting down…");
|
||||
await prisma.$disconnect();
|
||||
httpServer.close(() => process.exit(0));
|
||||
};
|
||||
|
||||
process.on("SIGTERM", shutdown);
|
||||
process.on("SIGINT", shutdown);
|
||||
Reference in New Issue
Block a user