From 47e43be184760de187e72c84f3a090c4ee2233cf Mon Sep 17 00:00:00 2001 From: Xargana Date: Sat, 3 May 2025 13:06:37 +0300 Subject: [PATCH] added stuff --- api/message-board/message-board.js | 114 +++++++++++++++++++++++++++++ api/server.js | 49 ++++++++++--- 2 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 api/message-board/message-board.js diff --git a/api/message-board/message-board.js b/api/message-board/message-board.js new file mode 100644 index 0000000..e8a19e7 --- /dev/null +++ b/api/message-board/message-board.js @@ -0,0 +1,114 @@ +const express = require("express"); +const fs = require("fs"); +const path = require("path"); + +const router = express.Router(); + +// Path for persisting messages +const MESSAGES_FILE_PATH = path.join(__dirname, 'messages-cache.json'); + +// In-memory storage with persistence +let messages = []; + +// Load existing messages from file if it exists +function loadMessages() { + try { + if (fs.existsSync(MESSAGES_FILE_PATH)) { + const data = fs.readFileSync(MESSAGES_FILE_PATH, 'utf8'); + messages = JSON.parse(data); + console.log(`Loaded ${messages.length} messages from cache`); + } + } catch (error) { + console.error("Error loading messages from cache:", error); + } +} + +// Save messages to file +function saveMessages() { + try { + fs.writeFileSync(MESSAGES_FILE_PATH, JSON.stringify(messages), 'utf8'); + } catch (error) { + console.error("Error saving messages to cache:", error); + } +} + +// Load messages at startup +loadMessages(); + +// Submit a new message +router.post("/submit", express.json(), (req, res) => { + try { + const { name, message } = req.body; + + if (!name || !message) { + return res.status(400).json({ error: "Missing name or message fields" }); + } + + messages.push({ + name, + message, + time: new Date().toISOString() + }); + + // Keep only the latest 100 messages + if (messages.length > 100) { + messages = messages.slice(-100); + } + + // Save to file after update + saveMessages(); + + res.json({ success: true, message: "Message received" }); + } catch (error) { + console.error("Error handling message submission:", error); + res.status(500).json({ error: "Internal server error" }); + } +}); + +// Get messages as JSON +router.get("/", (req, res) => { + try { + res.json({ messages }); + } catch (error) { + console.error("Error sending messages:", error); + res.status(500).json({ error: "Internal server error" }); + } +}); + +// Get messages as HTML +router.get("/html", (req, res) => { + try { + const html = ` + + + + Public Message Pool + + + +

Public Message Pool

+ ${messages.map(msg => ` +
+ ${msg.name} + [${msg.time}]: +
${msg.message}
+
+ `).join('')} + + + `; + + res.setHeader('Content-Type', 'text/html'); + res.send(html); + } catch (error) { + console.error("Error sending HTML messages:", error); + res.status(500).send("Internal server error"); + } +}); + +module.exports = router; \ No newline at end of file diff --git a/api/server.js b/api/server.js index ab91912..557c832 100644 --- a/api/server.js +++ b/api/server.js @@ -10,39 +10,66 @@ require('dotenv').config({ path: path.join(__dirname, '../.env') }); const status = require("./status/status"); const exchangeRate = require("./exchange-rate/exchange-rate"); const whois = require("./whois/whois"); +const messageBoard = require("./message-board/message-board"); +// Main API app const app = express(); const PORT = process.env.PORT || 2589; +// Message board app (separate instance) +const messageBoardApp = express(); +const MESSAGE_BOARD_PORT = process.env.MESSAGE_BOARD_PORT || 2845; + +// SSL certificate paths const key = process.env.SSL_KEY_PATH || "/etc/letsencrypt/live/xargana.tr/privkey.pem"; const cert = process.env.SSL_CERT_PATH || "/etc/letsencrypt/live/xargana.tr/fullchain.pem"; +// Configure main API app app.use(cors()); app.use("/status", status); app.use("/exchange-rate", exchangeRate); app.use("/whois", whois); -// try to load certificates +// Configure message board app +messageBoardApp.use(cors()); +messageBoardApp.use("/", messageBoard); + +// Try to load SSL certificates +let sslOptions; try { - const sslOptions = { + sslOptions = { key: fs.readFileSync(key), cert: fs.readFileSync(cert), }; - - https.createServer(sslOptions, app).listen(PORT, () => { - console.log(`API running at https://localhost:${PORT}`); - }); } catch (e) { if (e.code === 'ENOENT') { console.warn(`SSL certificate file(s) not found: ${e.path}`); } else { console.warn(`Error loading SSL certificates: ${e.message}`); } - - console.log("Starting server without SSL..."); - - // start http server as fallback + sslOptions = null; +} + +// Start main API server +if (sslOptions) { + https.createServer(sslOptions, app).listen(PORT, () => { + console.log(`Main API running at https://localhost:${PORT}`); + }); +} else { + console.log("Starting main API server without SSL..."); http.createServer(app).listen(PORT, () => { - console.log(`API running at http://localhost:${PORT}`); + console.log(`Main API running at http://localhost:${PORT}`); + }); +} + +// Start message board server +if (sslOptions) { + https.createServer(sslOptions, messageBoardApp).listen(MESSAGE_BOARD_PORT, () => { + console.log(`Message Board running at https://localhost:${MESSAGE_BOARD_PORT}`); + }); +} else { + console.log("Starting Message Board server without SSL..."); + http.createServer(messageBoardApp).listen(MESSAGE_BOARD_PORT, () => { + console.log(`Message Board running at http://localhost:${MESSAGE_BOARD_PORT}`); }); }