mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Passing servers to modules, minor code refactor
This commit is contained in:
parent
314914e898
commit
a12aadf32d
5 changed files with 68 additions and 53 deletions
|
@ -15,10 +15,7 @@ const testUser = {
|
||||||
created: new Date(),
|
created: new Date(),
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function (options: Options): any {
|
function renderLogin(req, res, jsonResponse) {
|
||||||
const { userDB, jsonResponse, exceptions } = options
|
|
||||||
|
|
||||||
const renderLogin = (req, res) => {
|
|
||||||
res.status('401') // Unauthorized
|
res.status('401') // Unauthorized
|
||||||
if (jsonResponse) {
|
if (jsonResponse) {
|
||||||
res.json({
|
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) {
|
return function (req, res, next) {
|
||||||
const sessionID = req.cookies.sessionID
|
const sessionID = req.cookies.sessionID
|
||||||
const isException = exceptions.some((exc) => {
|
const isException = exceptions.some((exc) => {
|
||||||
|
@ -66,7 +66,7 @@ export default function (options: Options): any {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.DebugLog(`No session ID: ${req.url}`, 'auth', 1)
|
logger.DebugLog(`No session ID: ${req.url}`, 'auth', 1)
|
||||||
renderLogin(req, res)
|
renderLogin(req, res, jsonResponse)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ export default function (options: Options): any {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.DebugLog(`No user:${req.url}`, 'auth', 1)
|
logger.DebugLog(`No user:${req.url}`, 'auth', 1)
|
||||||
renderLogin(req, res)
|
renderLogin(req, res, jsonResponse)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,8 @@ const moduleName = 'API'
|
||||||
let userDB
|
let userDB
|
||||||
let url
|
let url
|
||||||
let publicdirs = []
|
let publicdirs = []
|
||||||
|
let httpServer
|
||||||
|
let httpsServer
|
||||||
|
|
||||||
function GetApp(): ModuleType {
|
function GetApp(): ModuleType {
|
||||||
const app = express()
|
const app = express()
|
||||||
|
@ -203,6 +205,8 @@ function setupSubModules(
|
||||||
url: url,
|
url: url,
|
||||||
publicdirs: publicdirs,
|
publicdirs: publicdirs,
|
||||||
moduleSpecificData: moduleSpecificData,
|
moduleSpecificData: moduleSpecificData,
|
||||||
|
httpServer: httpServer,
|
||||||
|
httpsServer: httpsServer,
|
||||||
})
|
})
|
||||||
moduleDatas.push(loadedModData || {})
|
moduleDatas.push(loadedModData || {})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -219,7 +223,9 @@ export default {
|
||||||
getApp: GetApp,
|
getApp: GetApp,
|
||||||
setup: (data: SetupData): void => {
|
setup: (data: SetupData): void => {
|
||||||
userDB = data.userDB
|
userDB = data.userDB
|
||||||
url = data.url // eslint-disable-line
|
url = data.url
|
||||||
publicdirs = data.publicdirs
|
publicdirs = data.publicdirs
|
||||||
|
httpServer = data.httpServer
|
||||||
|
httpsServer = data.httpsServer
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,6 +75,8 @@ export interface SetupData {
|
||||||
publicdirs: Array<string>
|
publicdirs: Array<string>
|
||||||
userDB?: any
|
userDB?: any
|
||||||
nextdir?: string
|
nextdir?: string
|
||||||
|
httpServer: any
|
||||||
|
httpsServer: any
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!utils.FileExists(usersDBPath)) {
|
if (!utils.FileExists(usersDBPath)) {
|
||||||
|
@ -128,7 +130,44 @@ function exit(reason) {
|
||||||
process.exit()
|
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 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) {
|
if (!process.env.NS_DEVEL) {
|
||||||
app.use(function (req, res, next) {
|
app.use(function (req, res, next) {
|
||||||
|
@ -193,6 +232,8 @@ Object.keys(modules).forEach(function (key) {
|
||||||
userDB: userDB,
|
userDB: userDB,
|
||||||
publicdirs: module.publicdirs,
|
publicdirs: module.publicdirs,
|
||||||
nextdir: module.nextdir,
|
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()
|
setLogTimer()
|
||||||
function setLogTimer() {
|
function setLogTimer() {
|
||||||
const now = new Date()
|
const now = new Date()
|
||||||
|
@ -320,12 +332,7 @@ if (isRoot) {
|
||||||
logger.Log('Running as root', logger.GetColor('red'))
|
logger.Log('Running as root', logger.GetColor('red'))
|
||||||
}
|
}
|
||||||
|
|
||||||
const httpServer = http.createServer(app)
|
|
||||||
httpServer.listen(port)
|
httpServer.listen(port)
|
||||||
if (certsLoaded) {
|
if (httpsServer) {
|
||||||
const httpsServer = https.createServer(certs, app)
|
|
||||||
httpsServer.listen(httpsport)
|
httpsServer.listen(httpsport)
|
||||||
logger.Log('Listening on port: ' + httpsport + ' (https)')
|
|
||||||
} else {
|
|
||||||
logger.Log('Https not avaible')
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,5 +81,7 @@ export interface SubmoduleData {
|
||||||
publicdirs: Array<string>
|
publicdirs: Array<string>
|
||||||
userDB?: any
|
userDB?: any
|
||||||
nextdir?: string
|
nextdir?: string
|
||||||
|
httpServer: any
|
||||||
moduleSpecificData?: any
|
moduleSpecificData?: any
|
||||||
|
httpsServer?: any
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit dafcefd4f459d14737a9922cc4ba0400fbc9040a
|
Subproject commit c60ace4f9b9b7a0f45963c226b809d0b8b6bab29
|
Loading…
Add table
Add a link
Reference in a new issue