xargana-srv/api/server.js

76 lines
2.4 KiB
JavaScript
Raw Normal View History

2025-04-17 16:11:04 +02:00
const express = require("express");
const cors = require("cors");
const fs = require("fs");
const https = require("https");
const http = require("http");
const path = require("path");
// load environment variables from .env file
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");
2025-05-03 12:06:37 +02:00
const messageBoard = require("./message-board/message-board");
2025-04-17 16:11:04 +02:00
2025-05-03 12:06:37 +02:00
// Main API app
2025-04-17 16:11:04 +02:00
const app = express();
const PORT = process.env.PORT || 2589;
2025-05-03 12:06:37 +02:00
// Message board app (separate instance)
const messageBoardApp = express();
const MESSAGE_BOARD_PORT = process.env.MESSAGE_BOARD_PORT || 2845;
// SSL certificate paths
2025-04-27 14:26:06 +02:00
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";
2025-04-17 16:11:04 +02:00
2025-05-03 12:06:37 +02:00
// Configure main API app
2025-04-17 16:11:04 +02:00
app.use(cors());
app.use("/status", status);
app.use("/exchange-rate", exchangeRate);
app.use("/whois", whois);
2025-05-03 12:06:37 +02:00
// Configure message board app
messageBoardApp.use(cors());
messageBoardApp.use("/", messageBoard);
// Try to load SSL certificates
let sslOptions;
2025-04-17 16:11:04 +02:00
try {
2025-05-03 12:06:37 +02:00
sslOptions = {
2025-04-17 16:11:04 +02:00
key: fs.readFileSync(key),
cert: fs.readFileSync(cert),
};
} 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}`);
}
2025-05-03 12:06:37 +02:00
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...");
2025-04-17 16:11:04 +02:00
http.createServer(app).listen(PORT, () => {
2025-05-03 12:06:37 +02:00
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}`);
2025-04-17 16:11:04 +02:00
});
}