-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
64 lines (53 loc) · 1.82 KB
/
server.ts
File metadata and controls
64 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import { createServer } from "http";
import { Server } from "socket.io";
import apiRoutes from "./server/routes/api.js";
import { ConfigService } from "./server/services/configService.js";
import { TrafficController } from "./server/controllers/trafficController.js";
import { SystemService } from "./server/services/systemService.js";
async function startServer() {
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer);
app.use(express.json());
// API Routes
app.use("/api", apiRoutes);
// WebSocket logic
io.on("connection", (socket) => {
console.log("Client connected to WebSocket");
// Start log streaming
SystemService.startLogStream(io);
// Emit traffic data every 2 seconds to connected clients
const trafficInterval = setInterval(async () => {
try {
const data = await TrafficController.getTrafficData();
socket.emit("traffic", data);
} catch (e) {}
}, 2000);
socket.on("disconnect", () => {
clearInterval(trafficInterval);
});
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
const config = ConfigService.getDashboardConfig();
const PORT = 3000; // Port 3000 is mandatory
httpServer.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();