mirror of
https://github.com/hydra-synth/hydra.git
synced 2025-12-13 18:30:00 +01:00
42 lines
1.1 KiB
JavaScript
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
|
|
}
|