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

@ -15,10 +15,7 @@ const testUser = {
created: new Date(),
}
export default function (options: Options): any {
const { userDB, jsonResponse, exceptions } = options
const renderLogin = (req, res) => {
function renderLogin(req, res, jsonResponse) {
res.status('401') // Unauthorized
if (jsonResponse) {
res.json({
@ -32,6 +29,9 @@ export default function (options: Options): any {
}
}
export default function (options: Options): any {
const { userDB, jsonResponse, exceptions } = options
return function (req, res, next) {
const sessionID = req.cookies.sessionID
const isException = exceptions.some((exc) => {
@ -66,7 +66,7 @@ export default function (options: Options): any {
return
}
logger.DebugLog(`No session ID: ${req.url}`, 'auth', 1)
renderLogin(req, res)
renderLogin(req, res, jsonResponse)
return
}
@ -80,7 +80,7 @@ export default function (options: Options): any {
return
}
logger.DebugLog(`No user:${req.url}`, 'auth', 1)
renderLogin(req, res)
renderLogin(req, res, jsonResponse)
return
}

View file

@ -40,6 +40,8 @@ const moduleName = 'API'
let userDB
let url
let publicdirs = []
let httpServer
let httpsServer
function GetApp(): ModuleType {
const app = express()
@ -203,6 +205,8 @@ function setupSubModules(
url: url,
publicdirs: publicdirs,
moduleSpecificData: moduleSpecificData,
httpServer: httpServer,
httpsServer: httpsServer,
})
moduleDatas.push(loadedModData || {})
} catch (e) {
@ -219,7 +223,9 @@ export default {
getApp: GetApp,
setup: (data: SetupData): void => {
userDB = data.userDB
url = data.url // eslint-disable-line
url = data.url
publicdirs = data.publicdirs
httpServer = data.httpServer
httpsServer = data.httpsServer
},
}

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')
}

View file

@ -81,5 +81,7 @@ export interface SubmoduleData {
publicdirs: Array<string>
userDB?: any
nextdir?: string
httpServer: any
moduleSpecificData?: any
httpsServer?: any
}

@ -1 +1 @@
Subproject commit dafcefd4f459d14737a9922cc4ba0400fbc9040a
Subproject commit c60ace4f9b9b7a0f45963c226b809d0b8b6bab29