update - clean

This commit is contained in:
2025-06-29 13:57:23 +00:00
parent 5c5e0bfe1a
commit 49ce30f86c
7483 changed files with 486 additions and 1607714 deletions

View File

@@ -4,6 +4,9 @@ import cors from "cors";
import helmet from "helmet";
import path from "path";
import fs from "fs/promises";
import { createServer } from "http";
import { Server } from "socket.io";
import morgan from "morgan";
import { STATIC_DIR } from "./constants.js";
import { getStructure, cache } from "./helper.js";
@@ -11,6 +14,23 @@ if (typeof process.env.API_TOKEN === "undefined") {
throw new Error("Service cannot be started without API_TOKEN");
}
// Load cache on start
getStructure({ refresh: true });
const app = express();
const server = createServer(app);
// Global boolean array
let booleanArray = new Array(10).fill(false);
// Socket.IO setup with CORS
const io = new Server(server, {
cors: {
origin: "http://localhost:5173", // React app URL
methods: ["GET", "POST"],
},
});
const corsOptions = {
methods: ["GET", "POST", "PUT", "DELETE"],
credentials: true,
@@ -30,20 +50,53 @@ const corsOptions = {
},
};
// Load cache on start
getStructure({ refresh: true });
const app = express();
app.use(cors(corsOptions));
app.use(helmet());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan("tiny"));
// Socket connection handling
io.on("connection", (socket) => {
console.log("Client connected:", socket.id);
// Send current array state to newly connected client
socket.emit("arrayChanged", booleanArray);
// Handle array updates from client
socket.on("setArrayValue", (data) => {
const { index, value } = data;
if (index >= 0 && index < booleanArray.length) {
booleanArray[index] = value;
console.log(`Updated index ${index} to ${value}`);
// Broadcast updated array to all clients
io.emit("arrayChanged", booleanArray);
}
});
// Handle getting current array state
socket.on("getArray", () => {
socket.emit("arrayChanged", booleanArray);
});
socket.on("disconnect", () => {
console.log("Client disconnected:", socket.id);
});
});
app.get("/_health", (req, res) => {
res.json({ healthy: true });
});
app.get(
"/resources-status",
asyncHandler((req, res) => {
res.json({ array: booleanArray });
}),
);
app.get(
"/structure",
asyncHandler(async (req, res) => {
@@ -142,7 +195,7 @@ app.delete(
app.use(errorRequestHandler);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log("Resource Provider started"));
server.listen(PORT, () => console.log("Resource Provider started"));
/**
* HELPERS