Files
startup/server/index.js

71 lines
1.9 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 { selectProjectFolder } from "./projectPicker.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.use(express.json());
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()
});
});
app.post("/api/project/select", async (req, res) => {
try {
const selectedPath = req.body?.projectPath ? String(req.body.projectPath) : await selectProjectFolder();
await sessionManager.setProjectPath(selectedPath);
res.json({
ok: true,
projectPath: sessionManager.getState().currentProjectPath
});
} catch (error) {
res.status(500).json({
ok: false,
error: error.message
});
}
});
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}`);
});