mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
added bit more advanced file existance checking, removed vhosts in favor or routes
This commit is contained in:
parent
ba89f4a342
commit
113a114821
24 changed files with 2720 additions and 2474 deletions
106
src/server.ts
106
src/server.ts
|
@ -21,9 +21,6 @@
|
|||
console.log('Node version: ' + process.version)
|
||||
console.log('Current working directory: ' + process.cwd())
|
||||
|
||||
const startHTTPS = true
|
||||
const isRoot = process.getuid && process.getuid() === 0
|
||||
|
||||
const port = process.env.PORT || 8080
|
||||
const httpsport = 5001
|
||||
|
||||
|
@ -32,7 +29,6 @@ const httpsport = 5001
|
|||
// console.log(`Process priority set to ${os.getPriority()}`)
|
||||
|
||||
import express from 'express'
|
||||
import vhost from 'vhost'
|
||||
import http from 'http'
|
||||
import https from 'https'
|
||||
import cors from 'cors'
|
||||
|
@ -45,12 +41,10 @@ import utils from './utils/utils'
|
|||
import dbtools from './utils/dbtools'
|
||||
import reqlogger from './middlewares/reqlogger.middleware'
|
||||
import idStats from './utils/ids'
|
||||
const extraModulesFile = './src/extraModules/extraModules.json'
|
||||
const statExcludeFile = './data/statExclude.json'
|
||||
const modulesFile = './src/modules.json'
|
||||
const usersDBPath = './data/dbs/users.db'
|
||||
const logFile = logger.logDir + logger.logFileName
|
||||
const vlogFile = logger.vlogDir + logger.logFileName
|
||||
import { paths, validateFiles } from './utils/files'
|
||||
|
||||
const logFile = paths.logDir + logger.logFileName
|
||||
const vlogFile = paths.vlogDir + logger.logFileName
|
||||
|
||||
function moveLogIfNotFromToday(path: string, to: string) {
|
||||
if (utils.FileExists(path)) {
|
||||
|
@ -65,8 +59,8 @@ function moveLogIfNotFromToday(path: string, to: string) {
|
|||
}
|
||||
}
|
||||
}
|
||||
moveLogIfNotFromToday(logFile, logger.logDir)
|
||||
moveLogIfNotFromToday(vlogFile, logger.vlogDir)
|
||||
moveLogIfNotFromToday(logFile, paths.logDir)
|
||||
moveLogIfNotFromToday(vlogFile, paths.vlogDir)
|
||||
|
||||
idStats.Load()
|
||||
logger.Load()
|
||||
|
@ -79,7 +73,7 @@ interface Module {
|
|||
path: string
|
||||
publicdirs: Array<string>
|
||||
name: string
|
||||
urls: Array<string>
|
||||
route: string
|
||||
nextdir?: string
|
||||
isNextJs?: boolean
|
||||
app: express.Application
|
||||
|
@ -96,11 +90,16 @@ export interface SetupData {
|
|||
httpsServer: https.Server
|
||||
}
|
||||
|
||||
if (!utils.FileExists(usersDBPath)) {
|
||||
throw new Error('No user DB exists yet! please run utils/dbSetup.js first!')
|
||||
const filesValid = validateFiles()
|
||||
if (!filesValid) {
|
||||
const msg =
|
||||
'Not all files are valid which are needed to run the server! Please resolve the above issues, and start again.'
|
||||
logger.Log(msg, 'red')
|
||||
throw new Error(msg)
|
||||
}
|
||||
const userDB = dbtools.GetDB(usersDBPath)
|
||||
let modules: Modules = utils.ReadJSON(modulesFile)
|
||||
|
||||
const userDB = dbtools.GetDB(paths.usersDBPath)
|
||||
let modules: Modules = utils.ReadJSON(paths.modulesFile)
|
||||
|
||||
const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0
|
||||
logger.Log('Loglevel is: ' + debugLevel)
|
||||
|
@ -108,8 +107,8 @@ logger.Log(`Log path: ${logFile}`)
|
|||
logger.Log(`vLog path: ${vlogFile}`)
|
||||
|
||||
try {
|
||||
if (utils.FileExists(extraModulesFile)) {
|
||||
const extraModules = JSON.parse(utils.ReadFile(extraModulesFile))
|
||||
if (utils.FileExists(paths.extraModulesFile)) {
|
||||
const extraModules = JSON.parse(utils.ReadFile(paths.extraModulesFile))
|
||||
modules = {
|
||||
...extraModules,
|
||||
...modules,
|
||||
|
@ -147,28 +146,20 @@ function exit(reason: string) {
|
|||
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: { key: string; cert: string; ca: string }
|
||||
|
||||
// https://certbot.eff.org/
|
||||
if (
|
||||
startHTTPS &&
|
||||
utils.FileExists(privkeyFile) &&
|
||||
utils.FileExists(fullchainFile) &&
|
||||
utils.FileExists(chainFile)
|
||||
utils.FileExists(paths.privkeyFile) &&
|
||||
utils.FileExists(paths.fullchainFile) &&
|
||||
utils.FileExists(paths.chainFile)
|
||||
) {
|
||||
try {
|
||||
const key = utils.ReadFile(privkeyFile)
|
||||
const cert = utils.ReadFile(fullchainFile)
|
||||
const ca = utils.ReadFile(chainFile)
|
||||
certs = {
|
||||
key: key,
|
||||
cert: cert,
|
||||
ca: ca,
|
||||
key: utils.ReadFile(paths.privkeyFile),
|
||||
cert: utils.ReadFile(paths.fullchainFile),
|
||||
ca: utils.ReadFile(paths.chainFile),
|
||||
}
|
||||
certsLoaded = true
|
||||
} catch (err) {
|
||||
|
@ -218,11 +209,13 @@ app.use(
|
|||
|
||||
const cookieSecret = uuidv4()
|
||||
app.use(cookieParser(cookieSecret))
|
||||
app.set('view engine', 'ejs')
|
||||
app.set('views', ['./src/modules/api/views', './src/sharedViews'])
|
||||
|
||||
if (!utils.FileExists(statExcludeFile)) {
|
||||
utils.WriteFile('[]', statExcludeFile)
|
||||
if (!utils.FileExists(paths.statExcludeFile)) {
|
||||
utils.WriteFile('[]', paths.statExcludeFile)
|
||||
}
|
||||
const excludeFromStats = utils.ReadJSON(statExcludeFile)
|
||||
const excludeFromStats = utils.ReadJSON(paths.statExcludeFile)
|
||||
|
||||
app.use(
|
||||
reqlogger({
|
||||
|
@ -233,12 +226,13 @@ app.use(
|
|||
})
|
||||
)
|
||||
|
||||
const domain = utils.ReadFile(paths.domainFile)
|
||||
|
||||
Object.keys(modules).forEach(function (key) {
|
||||
const module = modules[key]
|
||||
try {
|
||||
const mod = require(module.path).default // eslint-disable-line
|
||||
// const mod = require(module.path)
|
||||
logger.Log(`Loading ${mod.name} module`, logger.GetColor('yellow'))
|
||||
|
||||
module.publicdirs.forEach((pdir) => {
|
||||
utils.CreatePath(pdir)
|
||||
|
@ -246,7 +240,7 @@ Object.keys(modules).forEach(function (key) {
|
|||
|
||||
if (mod.setup) {
|
||||
mod.setup({
|
||||
url: 'https://' + module.urls[0],
|
||||
url: domain,
|
||||
userDB: userDB,
|
||||
publicdirs: module.publicdirs,
|
||||
nextdir: module.nextdir,
|
||||
|
@ -259,14 +253,32 @@ Object.keys(modules).forEach(function (key) {
|
|||
module.app = modApp.app
|
||||
module.dailyAction = modApp.dailyAction
|
||||
module.cleanup = modApp.cleanup
|
||||
module.urls.forEach((url) => {
|
||||
app.use(vhost(url, module.app))
|
||||
})
|
||||
logger.Log(
|
||||
`Module "${mod.name}" loaded at "${module.route}"`,
|
||||
logger.GetColor('yellow')
|
||||
)
|
||||
app.use(module.route, module.app)
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
|
||||
app.get('*', (req, res) => {
|
||||
if (req.is('application/json')) {
|
||||
res.status(404).end()
|
||||
} else {
|
||||
res.status(404).render('404')
|
||||
}
|
||||
})
|
||||
|
||||
app.post('*', (req, res) => {
|
||||
if (req.is('application/json')) {
|
||||
res.status(404).end()
|
||||
} else {
|
||||
res.status(404).render('404')
|
||||
}
|
||||
})
|
||||
|
||||
setLogTimer()
|
||||
function setLogTimer() {
|
||||
const now = new Date()
|
||||
|
@ -309,10 +321,10 @@ function rotateLog() {
|
|||
('0' + date.getDate()).slice(-2)
|
||||
|
||||
if (utils.FileExists(logFile)) {
|
||||
utils.CopyFile(logFile, logger.logDir + fname)
|
||||
utils.CopyFile(logFile, paths.logDir + fname)
|
||||
}
|
||||
if (utils.FileExists(vlogFile)) {
|
||||
utils.CopyFile(vlogFile, logger.vlogDir + fname)
|
||||
utils.CopyFile(vlogFile, paths.vlogDir + fname)
|
||||
}
|
||||
|
||||
utils.WriteFile(fname, logFile)
|
||||
|
@ -345,9 +357,9 @@ function LogTimerAction() {
|
|||
|
||||
logger.Log('Node version: ' + process.version)
|
||||
logger.Log('Current working directory: ' + process.cwd())
|
||||
logger.Log('Listening on port: ' + port)
|
||||
if (isRoot) {
|
||||
logger.Log('Running as root', logger.GetColor('red'))
|
||||
logger.Log('Listening on port: ' + logger.C('blue') + port + logger.C())
|
||||
if (process.getuid && process.getuid() === 0) {
|
||||
logger.Log('Running as root', 'redbg')
|
||||
}
|
||||
|
||||
httpServer.listen(port)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue