52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
import express from "express";
|
|
import http from "http";
|
|
import path from "path";
|
|
import { fileURLToPath } from "url";
|
|
import { Server } from "socket.io";
|
|
import { getPublicRuntimeConfig, getRuntimeConfig } from "./config.js";
|
|
import { SessionManager } from "./sessionManager.js";
|
|
import { registerSocketHandlers } from "./socketHandlers.js";
|
|
|
|
const config = getRuntimeConfig();
|
|
const app = express();
|
|
const server = http.createServer(app);
|
|
const io = new Server(server, {
|
|
cors: {
|
|
origin: "*"
|
|
}
|
|
});
|
|
|
|
const sessionManager = new SessionManager({ io, config });
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
const webDistPath = path.resolve(__dirname, "../web/dist");
|
|
|
|
app.get("/health", (req, res) => {
|
|
res.json({
|
|
ok: true,
|
|
runtime: getPublicRuntimeConfig(config),
|
|
session: sessionManager.getState()
|
|
});
|
|
});
|
|
|
|
app.get("/api/session/state", (req, res) => {
|
|
res.json({
|
|
state: sessionManager.getState(),
|
|
logs: sessionManager.getLogSnapshot(),
|
|
chat: sessionManager.getChatSnapshot()
|
|
});
|
|
});
|
|
|
|
if (config.nodeEnv === "production") {
|
|
app.use(express.static(webDistPath));
|
|
app.get("*", (req, res) => {
|
|
res.sendFile(path.join(webDistPath, "index.html"));
|
|
});
|
|
}
|
|
|
|
registerSocketHandlers(io, sessionManager);
|
|
|
|
server.listen(config.port, () => {
|
|
console.log(`Retro console server listening on http://localhost:${config.port}`);
|
|
});
|