med-notes/resource-provider/server.js

209 lines
5.1 KiB
JavaScript

import { readdir } from "fs/promises";
import express from "express";
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, TOPICS } from "./constants.js";
import { getStructure, cache } from "./helper.js";
if (typeof process.env.API_TOKEN === "undefined") {
throw new Error("Service cannot be started without API_TOKEN");
}
// Load cache on start
getStructure({ refresh: true }).catch(console.error);
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,
origin: (origin, callback) => {
if (!origin) {
if (process.env.NODE_ENV === "production") {
return callback(new Error("Origin required in production"));
}
return callback(null, true);
}
if (isOriginAllowed(origin)) {
return callback(null, true);
} else {
return callback(new Error("Not allowed by CORS"));
}
},
};
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 });
}),
);
// Serve static files with automatic ETag handling
app.use(
"/content",
express.static(STATIC_DIR, {
etag: true, // Enable automatic ETag generation
lastModified: true, // Include Last-Modified header
maxAge: 3600000, // Cache for 1 hour, but always revalidate
immutable: false, // Files can change
}),
);
app.get(
"/structure",
asyncHandler(async (req, res) => {
let { refresh } = req.query;
refresh = Boolean(refresh);
const structure = await getStructure({ refresh });
res.json(structure);
}),
);
app.post(
"/resources",
verifyToken,
asyncHandler(async (req, res) => {
try {
const { topicSeq, resourceSeq, content } = req.body;
if (!topicSeq || !resourceSeq || !content) {
res.status(400).json({ message: "Missing body" });
return;
}
if (
typeof topicSeq !== "number" ||
typeof resourceSeq !== "number" ||
typeof content !== "string"
) {
res.status(400).json({ message: "Invalid body" });
}
const topicIdx = topicSeq - 1;
const resourceIdx = resourceSeq - 1;
const topics = await getStructure();
const resource = topics[topicIdx].resources[resourceIdx];
if (!resource) {
res.status(404).json({ message: "Resource not found to update" });
}
const filename = `F_${resource.id}_${Date.now()}.md`;
const filePath = path.join(STATIC_DIR, filename);
await fs.writeFile(filePath, content, "utf8");
resource.filename = filename;
res.json({ success: true, filename, structure: topics });
} catch (error) {
console.log(error);
res.status(500).json({ error: "Failed to update topic resource" });
}
}),
);
app.use(errorRequestHandler);
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log("Resource Provider started"));
/**
* HELPERS
*/
function asyncHandler(fn) {
return (req, res, next) => {
return Promise.resolve(fn(req, res, next)).catch(next);
};
}
function errorRequestHandler(error, _req, res, next) {
if (error) {
console.log(error);
res
.status(error.status || 500)
.json({ message: error.message || "Server failed" });
} else {
next();
}
}
function isOriginAllowed(origin) {
const url = new URL(origin);
if (url.hostname === "localhost" || url.hostname === "127.0.0.1") {
return true;
}
return url.hostname.endsWith("tomastm.com");
}
function verifyToken(req, res, next) {
const token = req.headers["token"];
if (!token || token !== process.env.API_TOKEN) {
res.status(401).json({ message: "Token not provided" });
return;
}
next();
}