added support for http and https

This commit is contained in:
WeeXnes 2025-04-22 20:37:05 +02:00
parent 476e69fe1b
commit 3097255bc7

37
api.js
View file

@ -11,11 +11,7 @@ const gameMap = {};
const sslOptions = {
key: fs.readFileSync(path.join(os.homedir(), 'certs', 'privkey.pem')),
cert: fs.readFileSync(path.join(os.homedir(), 'certs', 'cert.pem')),
ca: fs.readFileSync(path.join(os.homedir(), 'certs', 'fullchain.pem'))
};
data.split('\n').forEach(line => { data.split('\n').forEach(line => {
@ -55,6 +51,31 @@ app.get('/contact', (req, res) => {
}); });
https.createServer(sslOptions, app).listen(PORT, () => { const certDir = path.join(os.homedir(), 'certs');
console.log(`Server running at https://localhost:${PORT}`); const keyPath = path.join(certDir, 'privkey.pem');
}); const certPath = path.join(certDir, 'cert.pem');
const caPath = path.join(certDir, 'fullchain.pem');
const areCertsPresent = () => {
return fs.existsSync(keyPath) && fs.existsSync(certPath) && fs.existsSync(caPath);
};
if (areCertsPresent()) {
console.log('SSL certificates found. Starting HTTPS server...');
const sslOptions = {
key: fs.readFileSync(keyPath),
cert: fs.readFileSync(certPath),
ca: fs.readFileSync(caPath)
};
https.createServer(sslOptions, app).listen(PORT, () => {
console.log(`HTTPS Server running at https://localhost:${PORT}`);
});
} else {
console.log('SSL certificates not found. Starting HTTP server...');
app.listen(PORT, () => {
console.log(`HTTP Server running at http://localhost:${PORT}`);
});
}