Files
hydra/backend/configure-ssl.js
2022-10-26 16:53:23 +02:00

42 lines
1.1 KiB
JavaScript

const fs = require('fs')
const path = require('path')
// if on glitch, force https
module.exports = (app) => {
var server
if(process.env.GLITCH) {
var http = require('http')
server = http.createServer(app)
function checkHttps(req, res, next){
if(req.get('X-Forwarded-Proto').indexOf("https")!=-1){
// console.log("https, yo")
return next()
} else {
res.redirect('https://' + req.hostname + req.url);
}
}
app.all('*', checkHttps)
} else {
try {
var https = require('https')
var privateKey = fs.readFileSync(path.join(__dirname, '/certs/key.pem'), 'utf8')
var certificate = fs.readFileSync(path.join(__dirname, '/certs/certificate.pem'), 'utf8')
var credentials = {key: privateKey, cert: certificate}
server = https.createServer(credentials, app)
} catch (err) {
if (err.code === 'ENOENT') {
console.log("no TLS certificate at", err.path)
var http = require('http')
server = http.createServer(app)
} else {
throw err
}
}
}
return server
}