Passing servers to modules, minor code refactor

This commit is contained in:
mrfry 2021-05-26 18:38:02 +02:00
parent 314914e898
commit a12aadf32d
5 changed files with 68 additions and 53 deletions

View file

@ -75,6 +75,8 @@ export interface SetupData {
publicdirs: Array<string>
userDB?: any
nextdir?: string
httpServer: any
httpsServer: any
}
if (!utils.FileExists(usersDBPath)) {
@ -128,7 +130,44 @@ function exit(reason) {
process.exit()
}
// https://certbot.eff.org/
const privkeyFile = '/etc/letsencrypt/live/frylabs.net/privkey.pem'
const fullchainFile = '/etc/letsencrypt/live/frylabs.net/fullchain.pem'
const chainFile = '/etc/letsencrypt/live/frylabs.net/chain.pem'
let certsLoaded = false
let certs
if (
startHTTPS &&
utils.FileExists(privkeyFile) &&
utils.FileExists(fullchainFile) &&
utils.FileExists(chainFile)
) {
try {
const key = utils.ReadFile(privkeyFile)
const cert = utils.ReadFile(fullchainFile)
const ca = utils.ReadFile(chainFile)
certs = {
key: key,
cert: cert,
ca: ca,
}
certsLoaded = true
} catch (err) {
logger.Log('Error loading cert files!', logger.GetColor('redbg'))
console.error(err)
}
}
const app = express()
const httpServer = http.createServer(app)
let httpsServer
if (certsLoaded) {
httpsServer = https.createServer(certs, app)
logger.Log('Listening on port: ' + httpsport + ' (https)')
} else {
logger.Log('Https not avaible')
}
if (!process.env.NS_DEVEL) {
app.use(function (req, res, next) {
@ -193,6 +232,8 @@ Object.keys(modules).forEach(function (key) {
userDB: userDB,
publicdirs: module.publicdirs,
nextdir: module.nextdir,
httpServer: httpServer,
httpsServer: httpsServer,
})
}
@ -208,35 +249,6 @@ Object.keys(modules).forEach(function (key) {
}
})
// https://certbot.eff.org/
const privkeyFile = '/etc/letsencrypt/live/frylabs.net/privkey.pem'
const fullchainFile = '/etc/letsencrypt/live/frylabs.net/fullchain.pem'
const chainFile = '/etc/letsencrypt/live/frylabs.net/chain.pem'
let certsLoaded = false
let certs
if (
startHTTPS &&
utils.FileExists(privkeyFile) &&
utils.FileExists(fullchainFile) &&
utils.FileExists(chainFile)
) {
try {
const key = utils.ReadFile(privkeyFile)
const cert = utils.ReadFile(fullchainFile)
const ca = utils.ReadFile(chainFile)
certs = {
key: key,
cert: cert,
ca: ca,
}
certsLoaded = true
} catch (err) {
logger.Log('Error loading cert files!', logger.GetColor('redbg'))
console.error(err)
}
}
setLogTimer()
function setLogTimer() {
const now = new Date()
@ -320,12 +332,7 @@ if (isRoot) {
logger.Log('Running as root', logger.GetColor('red'))
}
const httpServer = http.createServer(app)
httpServer.listen(port)
if (certsLoaded) {
const httpsServer = https.createServer(certs, app)
if (httpsServer) {
httpsServer.listen(httpsport)
logger.Log('Listening on port: ' + httpsport + ' (https)')
} else {
logger.Log('Https not avaible')
}