added http support
This commit is contained in:
parent
1026f13843
commit
bf3538196c
|
@ -12,7 +12,8 @@ const exchangeRate = require("./exchange-rate/exchange-rate");
|
||||||
const whois = require("./whois/whois");
|
const whois = require("./whois/whois");
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = process.env.PORT || 2589;
|
const HTTP_PORT = process.env.HTTP_PORT || 2589;
|
||||||
|
const HTTPS_PORT = process.env.HTTPS_PORT || 2845;
|
||||||
|
|
||||||
const key = process.env.SSL_KEY_PATH || "/etc/letsencrypt/live/blahaj.tr/privkey.pem";
|
const key = process.env.SSL_KEY_PATH || "/etc/letsencrypt/live/blahaj.tr/privkey.pem";
|
||||||
const cert = process.env.SSL_CERT_PATH || "/etc/letsencrypt/live/blahaj.tr/fullchain.pem";
|
const cert = process.env.SSL_CERT_PATH || "/etc/letsencrypt/live/blahaj.tr/fullchain.pem";
|
||||||
|
@ -22,15 +23,20 @@ app.use("/status", status);
|
||||||
app.use("/exchange-rate", exchangeRate);
|
app.use("/exchange-rate", exchangeRate);
|
||||||
app.use("/whois", whois);
|
app.use("/whois", whois);
|
||||||
|
|
||||||
// try to load certificates
|
// Start HTTP server
|
||||||
|
http.createServer(app).listen(HTTP_PORT, () => {
|
||||||
|
console.log(`API running at http://localhost:${HTTP_PORT}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
// try to load certificates and start HTTPS server
|
||||||
try {
|
try {
|
||||||
const sslOptions = {
|
const sslOptions = {
|
||||||
key: fs.readFileSync(key),
|
key: fs.readFileSync(key),
|
||||||
cert: fs.readFileSync(cert),
|
cert: fs.readFileSync(cert),
|
||||||
};
|
};
|
||||||
|
|
||||||
https.createServer(sslOptions, app).listen(PORT, () => {
|
https.createServer(sslOptions, app).listen(HTTPS_PORT, () => {
|
||||||
console.log(`API running at https://localhost:${PORT}`);
|
console.log(`API running at https://localhost:${HTTPS_PORT}`);
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.code === 'ENOENT') {
|
if (e.code === 'ENOENT') {
|
||||||
|
@ -38,11 +44,5 @@ try {
|
||||||
} else {
|
} else {
|
||||||
console.warn(`Error loading SSL certificates: ${e.message}`);
|
console.warn(`Error loading SSL certificates: ${e.message}`);
|
||||||
}
|
}
|
||||||
|
console.warn("HTTPS server could not be started");
|
||||||
console.log("Starting server without SSL...");
|
}
|
||||||
|
|
||||||
// start http server as fallback
|
|
||||||
http.createServer(app).listen(PORT, () => {
|
|
||||||
console.log(`API running at http://localhost:${PORT}`);
|
|
||||||
});
|
|
||||||
}
|
|
Loading…
Reference in a new issue