This commit is contained in:
Xargana 2025-03-29 13:35:11 +03:00
parent 550a6f6c3f
commit 6d5bd3e507
2 changed files with 48 additions and 24 deletions

44
api/server.js Normal file
View file

@ -0,0 +1,44 @@
const express = require("express");
const cors = require("cors");
const fs = require("fs");
const https = require("https");
const http = require("http");
const path = require("path");
const ping = require("ping");
const status = require("./status/server")
const app = express();
const PORT = 2589;
const key = "/etc/letsencrypt/live/blahaj.tr/privkey.pem"
const cert = "/etc/letsencrypt/live/blahaj.tr/fullchain.pem"
app.use(cors());
app.use("/status", status);
// Try to load SSL Certificates
try {
const sslOptions = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert),
};
// Start HTTPS Server if certificates are available
https.createServer(sslOptions, app).listen(PORT, () => {
console.log(`API running securely at https://localhost:${PORT}`);
});
} catch (e) {
// If certificate loading fails, start HTTP server instead
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
http.createServer(app).listen(PORT, () => {
console.log(`API running insecurely at http://localhost:${PORT}`);
});
}

View file

@ -1,14 +1,9 @@
const express = require("express");
const cors = require("cors");
const fs = require("fs");
const https = require("https");
const path = require("path");
const ping = require("ping");
const app = express();
const PORT = 2589;
const key = "/etc/letsencrypt/live/blahaj.tr/privkey.pem"
const cert = "/etc/letsencrypt/live/blahaj.tr/fullchain.pem"
const router = express.Router();
const REMOTE_SERVERS = [
{ name: "blahaj.tr", host: "blahaj.tr" },
{ name: "xargana.com", host: "xargana.com" },
@ -26,8 +21,6 @@ REMOTE_SERVERS.forEach(server => {
};
});
app.use(cors());
async function checkServers() {
for (const server of REMOTE_SERVERS) {
const startTime = Date.now();
@ -46,21 +39,8 @@ async function checkServers() {
setInterval(checkServers, CHECK_INTERVAL);
checkServers();
app.get("/status", (req, res) => {
router.get("/", (req, res) => {
res.json(serversStatus);
});
// Load SSL Certificates
const sslOptions = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert),
};
// Start HTTPS Server
try {
https.createServer(sslOptions, app).listen(PORT, () => {
console.log(`API running at https://localhost:${PORT}`);
});
} catch (e) {
console.error("Error starting server:", e);
}
module.exports = router;