a
This commit is contained in:
parent
550a6f6c3f
commit
6d5bd3e507
44
api/server.js
Normal file
44
api/server.js
Normal 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}`);
|
||||||
|
});
|
||||||
|
}
|
|
@ -1,14 +1,9 @@
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
const fs = require("fs");
|
|
||||||
const https = require("https");
|
|
||||||
const path = require("path");
|
|
||||||
const ping = require("ping");
|
const ping = require("ping");
|
||||||
|
|
||||||
const app = express();
|
const router = express.Router();
|
||||||
const PORT = 2589;
|
|
||||||
const key = "/etc/letsencrypt/live/blahaj.tr/privkey.pem"
|
|
||||||
const cert = "/etc/letsencrypt/live/blahaj.tr/fullchain.pem"
|
|
||||||
const REMOTE_SERVERS = [
|
const REMOTE_SERVERS = [
|
||||||
{ name: "blahaj.tr", host: "blahaj.tr" },
|
{ name: "blahaj.tr", host: "blahaj.tr" },
|
||||||
{ name: "xargana.com", host: "xargana.com" },
|
{ name: "xargana.com", host: "xargana.com" },
|
||||||
|
@ -26,8 +21,6 @@ REMOTE_SERVERS.forEach(server => {
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
app.use(cors());
|
|
||||||
|
|
||||||
async function checkServers() {
|
async function checkServers() {
|
||||||
for (const server of REMOTE_SERVERS) {
|
for (const server of REMOTE_SERVERS) {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
|
@ -46,21 +39,8 @@ async function checkServers() {
|
||||||
setInterval(checkServers, CHECK_INTERVAL);
|
setInterval(checkServers, CHECK_INTERVAL);
|
||||||
checkServers();
|
checkServers();
|
||||||
|
|
||||||
app.get("/status", (req, res) => {
|
router.get("/", (req, res) => {
|
||||||
res.json(serversStatus);
|
res.json(serversStatus);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Load SSL Certificates
|
module.exports = router;
|
||||||
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);
|
|
||||||
}
|
|
Loading…
Reference in a new issue