diff --git a/api.js b/api.js index b323216..ff7b215 100644 --- a/api.js +++ b/api.js @@ -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 => { @@ -55,6 +51,31 @@ app.get('/contact', (req, res) => { }); -https.createServer(sslOptions, app).listen(PORT, () => { - console.log(`Server running at https://localhost:${PORT}`); -}); +const certDir = path.join(os.homedir(), 'certs'); +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}`); + }); +}