mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
prettier 4 tabwidth
This commit is contained in:
parent
00ec614f1d
commit
96b413a365
42 changed files with 7034 additions and 6905 deletions
390
src/server.ts
390
src/server.ts
|
@ -53,17 +53,17 @@ const logFile = logger.logDir + logger.logFileName
|
|||
const vlogFile = logger.vlogDir + logger.logFileName
|
||||
|
||||
function moveLogIfNotFromToday(path: string, to: string) {
|
||||
if (utils.FileExists(path)) {
|
||||
const today = new Date()
|
||||
const stat = utils.statFile(path)
|
||||
if (
|
||||
today.getFullYear() !== stat.mtime.getFullYear() ||
|
||||
today.getMonth() !== stat.mtime.getMonth() ||
|
||||
today.getDate() !== stat.mtime.getDate()
|
||||
) {
|
||||
utils.renameFile(path, to + utils.GetDateString(stat.mtime))
|
||||
if (utils.FileExists(path)) {
|
||||
const today = new Date()
|
||||
const stat = utils.statFile(path)
|
||||
if (
|
||||
today.getFullYear() !== stat.mtime.getFullYear() ||
|
||||
today.getMonth() !== stat.mtime.getMonth() ||
|
||||
today.getDate() !== stat.mtime.getDate()
|
||||
) {
|
||||
utils.renameFile(path, to + utils.GetDateString(stat.mtime))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
moveLogIfNotFromToday(logFile, logger.logDir)
|
||||
moveLogIfNotFromToday(vlogFile, logger.vlogDir)
|
||||
|
@ -72,32 +72,32 @@ idStats.Load()
|
|||
logger.Load()
|
||||
|
||||
interface Modules {
|
||||
[name: string]: Module
|
||||
[name: string]: Module
|
||||
}
|
||||
|
||||
interface Module {
|
||||
path: string
|
||||
publicdirs: Array<string>
|
||||
name: string
|
||||
urls: Array<string>
|
||||
nextdir?: string
|
||||
isNextJs?: boolean
|
||||
app: express.Application
|
||||
dailyAction: Function
|
||||
cleanup: Function
|
||||
path: string
|
||||
publicdirs: Array<string>
|
||||
name: string
|
||||
urls: Array<string>
|
||||
nextdir?: string
|
||||
isNextJs?: boolean
|
||||
app: express.Application
|
||||
dailyAction: Function
|
||||
cleanup: Function
|
||||
}
|
||||
|
||||
export interface SetupData {
|
||||
url: string
|
||||
publicdirs: Array<string>
|
||||
userDB?: Database
|
||||
nextdir?: string
|
||||
httpServer: http.Server
|
||||
httpsServer: https.Server
|
||||
url: string
|
||||
publicdirs: Array<string>
|
||||
userDB?: Database
|
||||
nextdir?: string
|
||||
httpServer: http.Server
|
||||
httpsServer: https.Server
|
||||
}
|
||||
|
||||
if (!utils.FileExists(usersDBPath)) {
|
||||
throw new Error('No user DB exists yet! please run utils/dbSetup.js first!')
|
||||
throw new Error('No user DB exists yet! please run utils/dbSetup.js first!')
|
||||
}
|
||||
const userDB = dbtools.GetDB(usersDBPath)
|
||||
let modules: Modules = utils.ReadJSON(modulesFile)
|
||||
|
@ -108,43 +108,43 @@ logger.Log(`Log path: ${logFile}`)
|
|||
logger.Log(`vLog path: ${vlogFile}`)
|
||||
|
||||
try {
|
||||
if (utils.FileExists(extraModulesFile)) {
|
||||
const extraModules = JSON.parse(utils.ReadFile(extraModulesFile))
|
||||
modules = {
|
||||
...extraModules,
|
||||
...modules,
|
||||
if (utils.FileExists(extraModulesFile)) {
|
||||
const extraModules = JSON.parse(utils.ReadFile(extraModulesFile))
|
||||
modules = {
|
||||
...extraModules,
|
||||
...modules,
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
logger.Log('Failed to read extra modules file')
|
||||
console.error(err)
|
||||
logger.Log('Failed to read extra modules file')
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
process.on('SIGINT', () => exit('SIGINT'))
|
||||
process.on('SIGTERM', () => exit('SIGTERM'))
|
||||
|
||||
function exit(reason: string) {
|
||||
console.log()
|
||||
logger.Log(`Exiting, reason: ${reason}`)
|
||||
Object.keys(modules).forEach((key) => {
|
||||
const module = modules[key]
|
||||
if (module.cleanup) {
|
||||
try {
|
||||
module.cleanup()
|
||||
} catch (err) {
|
||||
logger.Log(
|
||||
`Error in ${key} cleanup! Details in STDERR`,
|
||||
logger.GetColor('redbg')
|
||||
)
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
console.log()
|
||||
logger.Log(`Exiting, reason: ${reason}`)
|
||||
Object.keys(modules).forEach((key) => {
|
||||
const module = modules[key]
|
||||
if (module.cleanup) {
|
||||
try {
|
||||
module.cleanup()
|
||||
} catch (err) {
|
||||
logger.Log(
|
||||
`Error in ${key} cleanup! Details in STDERR`,
|
||||
logger.GetColor('redbg')
|
||||
)
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.Log('Closing Auth DB')
|
||||
userDB.close()
|
||||
logger.Log('Closing Auth DB')
|
||||
userDB.close()
|
||||
|
||||
process.exit()
|
||||
process.exit()
|
||||
}
|
||||
|
||||
// https://certbot.eff.org/
|
||||
|
@ -156,201 +156,201 @@ let certsLoaded = false
|
|||
let certs: { key: string; cert: string; ca: string }
|
||||
|
||||
if (
|
||||
startHTTPS &&
|
||||
utils.FileExists(privkeyFile) &&
|
||||
utils.FileExists(fullchainFile) &&
|
||||
utils.FileExists(chainFile)
|
||||
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,
|
||||
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)
|
||||
}
|
||||
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: https.Server
|
||||
if (certsLoaded) {
|
||||
httpsServer = https.createServer(certs, app)
|
||||
logger.Log('Listening on port: ' + httpsport + ' (https)')
|
||||
httpsServer = https.createServer(certs, app)
|
||||
logger.Log('Listening on port: ' + httpsport + ' (https)')
|
||||
} else {
|
||||
logger.Log('Https not avaible')
|
||||
logger.Log('Https not avaible')
|
||||
}
|
||||
|
||||
if (!process.env.NS_DEVEL) {
|
||||
app.use(function (req, res, next) {
|
||||
if (req.secure) {
|
||||
next()
|
||||
} else {
|
||||
logger.DebugLog(
|
||||
`HTTPS ${req.method} redirect to: ${
|
||||
'https://' + req.headers.host + req.url
|
||||
}`,
|
||||
'https',
|
||||
1
|
||||
)
|
||||
if (req.method === 'POST') {
|
||||
res.redirect(307, 'https://' + req.headers.host + req.url)
|
||||
} else {
|
||||
res.redirect('https://' + req.headers.host + req.url)
|
||||
}
|
||||
}
|
||||
})
|
||||
app.use(function (req, res, next) {
|
||||
if (req.secure) {
|
||||
next()
|
||||
} else {
|
||||
logger.DebugLog(
|
||||
`HTTPS ${req.method} redirect to: ${
|
||||
'https://' + req.headers.host + req.url
|
||||
}`,
|
||||
'https',
|
||||
1
|
||||
)
|
||||
if (req.method === 'POST') {
|
||||
res.redirect(307, 'https://' + req.headers.host + req.url)
|
||||
} else {
|
||||
res.redirect('https://' + req.headers.host + req.url)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
// https://github.com/expressjs/cors#configuration-options
|
||||
app.use(
|
||||
cors({
|
||||
credentials: true,
|
||||
origin: true,
|
||||
// origin: [ /\.frylabs\.net$/ ]
|
||||
})
|
||||
cors({
|
||||
credentials: true,
|
||||
origin: true,
|
||||
// origin: [ /\.frylabs\.net$/ ]
|
||||
})
|
||||
)
|
||||
|
||||
const cookieSecret = uuidv4()
|
||||
app.use(cookieParser(cookieSecret))
|
||||
|
||||
if (!utils.FileExists(statExcludeFile)) {
|
||||
utils.WriteFile('[]', statExcludeFile)
|
||||
utils.WriteFile('[]', statExcludeFile)
|
||||
}
|
||||
const excludeFromStats = utils.ReadJSON(statExcludeFile)
|
||||
|
||||
app.use(
|
||||
reqlogger({
|
||||
loggableKeywords: ['news.json'],
|
||||
loggableModules: [],
|
||||
exceptions: ['_next/static'],
|
||||
excludeFromStats: excludeFromStats,
|
||||
})
|
||||
reqlogger({
|
||||
loggableKeywords: ['news.json'],
|
||||
loggableModules: [],
|
||||
exceptions: ['_next/static'],
|
||||
excludeFromStats: excludeFromStats,
|
||||
})
|
||||
)
|
||||
|
||||
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'))
|
||||
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)
|
||||
})
|
||||
module.publicdirs.forEach((pdir) => {
|
||||
utils.CreatePath(pdir)
|
||||
})
|
||||
|
||||
if (mod.setup) {
|
||||
mod.setup({
|
||||
url: 'https://' + module.urls[0],
|
||||
userDB: userDB,
|
||||
publicdirs: module.publicdirs,
|
||||
nextdir: module.nextdir,
|
||||
httpServer: httpServer,
|
||||
httpsServer: httpsServer,
|
||||
})
|
||||
if (mod.setup) {
|
||||
mod.setup({
|
||||
url: 'https://' + module.urls[0],
|
||||
userDB: userDB,
|
||||
publicdirs: module.publicdirs,
|
||||
nextdir: module.nextdir,
|
||||
httpServer: httpServer,
|
||||
httpsServer: httpsServer,
|
||||
})
|
||||
}
|
||||
|
||||
const modApp = mod.getApp()
|
||||
module.app = modApp.app
|
||||
module.dailyAction = modApp.dailyAction
|
||||
module.cleanup = modApp.cleanup
|
||||
module.urls.forEach((url) => {
|
||||
app.use(vhost(url, module.app))
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
const modApp = mod.getApp()
|
||||
module.app = modApp.app
|
||||
module.dailyAction = modApp.dailyAction
|
||||
module.cleanup = modApp.cleanup
|
||||
module.urls.forEach((url) => {
|
||||
app.use(vhost(url, module.app))
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
})
|
||||
|
||||
setLogTimer()
|
||||
function setLogTimer() {
|
||||
const now = new Date()
|
||||
const night = new Date(
|
||||
now.getFullYear(),
|
||||
now.getMonth(),
|
||||
now.getDate() + 1,
|
||||
0,
|
||||
0,
|
||||
1
|
||||
)
|
||||
logger.DebugLog(`Next daily action: ${night}`, 'daily', 1)
|
||||
const msToMidnight = night.getTime() - now.getTime() + 10000
|
||||
logger.DebugLog(`msToMidnight: ${msToMidnight}`, 'daily', 1)
|
||||
logger.DebugLog(`Seconds To Midnight: ${msToMidnight / 1000}`, 'daily', 1)
|
||||
|
||||
if (msToMidnight < 0) {
|
||||
logger.Log(
|
||||
`Error setting up Log Timer, msToMidnight is negative! (${msToMidnight})`,
|
||||
logger.GetColor('redbg')
|
||||
const now = new Date()
|
||||
const night = new Date(
|
||||
now.getFullYear(),
|
||||
now.getMonth(),
|
||||
now.getDate() + 1,
|
||||
0,
|
||||
0,
|
||||
1
|
||||
)
|
||||
return
|
||||
}
|
||||
logger.DebugLog(`Next daily action: ${night}`, 'daily', 1)
|
||||
const msToMidnight = night.getTime() - now.getTime() + 10000
|
||||
logger.DebugLog(`msToMidnight: ${msToMidnight}`, 'daily', 1)
|
||||
logger.DebugLog(`Seconds To Midnight: ${msToMidnight / 1000}`, 'daily', 1)
|
||||
|
||||
setTimeout(function () {
|
||||
LogTimerAction()
|
||||
rotateLog()
|
||||
setLogTimer()
|
||||
}, msToMidnight)
|
||||
if (msToMidnight < 0) {
|
||||
logger.Log(
|
||||
`Error setting up Log Timer, msToMidnight is negative! (${msToMidnight})`,
|
||||
logger.GetColor('redbg')
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
setTimeout(function () {
|
||||
LogTimerAction()
|
||||
rotateLog()
|
||||
setLogTimer()
|
||||
}, msToMidnight)
|
||||
}
|
||||
|
||||
function rotateLog() {
|
||||
const date = new Date()
|
||||
date.setDate(date.getDate() - 1)
|
||||
const fname =
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
('0' + (date.getMonth() + 1)).slice(-2) +
|
||||
'-' +
|
||||
('0' + date.getDate()).slice(-2)
|
||||
const date = new Date()
|
||||
date.setDate(date.getDate() - 1)
|
||||
const fname =
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
('0' + (date.getMonth() + 1)).slice(-2) +
|
||||
'-' +
|
||||
('0' + date.getDate()).slice(-2)
|
||||
|
||||
if (utils.FileExists(logFile)) {
|
||||
utils.CopyFile(logFile, logger.logDir + fname)
|
||||
}
|
||||
if (utils.FileExists(vlogFile)) {
|
||||
utils.CopyFile(vlogFile, logger.vlogDir + fname)
|
||||
}
|
||||
if (utils.FileExists(logFile)) {
|
||||
utils.CopyFile(logFile, logger.logDir + fname)
|
||||
}
|
||||
if (utils.FileExists(vlogFile)) {
|
||||
utils.CopyFile(vlogFile, logger.vlogDir + fname)
|
||||
}
|
||||
|
||||
utils.WriteFile(fname, logFile)
|
||||
utils.WriteFile(fname, vlogFile)
|
||||
utils.WriteFile(fname, logFile)
|
||||
utils.WriteFile(fname, vlogFile)
|
||||
}
|
||||
|
||||
function LogTimerAction() {
|
||||
logger.DebugLog(`Running Log Timer Action`, 'daily', 1)
|
||||
Object.keys(modules).forEach((key) => {
|
||||
const module = modules[key]
|
||||
logger.DebugLog(`Ckecking ${key}`, 'daily', 1)
|
||||
if (module.dailyAction) {
|
||||
try {
|
||||
logger.Log(`Running daily action of ${key}`)
|
||||
module.dailyAction()
|
||||
} catch (err) {
|
||||
logger.Log(
|
||||
`Error in ${key} daily action! Details in STDERR`,
|
||||
logger.GetColor('redbg')
|
||||
)
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
logger.DebugLog(`Running Log Timer Action`, 'daily', 1)
|
||||
Object.keys(modules).forEach((key) => {
|
||||
const module = modules[key]
|
||||
logger.DebugLog(`Ckecking ${key}`, 'daily', 1)
|
||||
if (module.dailyAction) {
|
||||
try {
|
||||
logger.Log(`Running daily action of ${key}`)
|
||||
module.dailyAction()
|
||||
} catch (err) {
|
||||
logger.Log(
|
||||
`Error in ${key} daily action! Details in STDERR`,
|
||||
logger.GetColor('redbg')
|
||||
)
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const line =
|
||||
'==================================================================================================================================================='
|
||||
logger.Log(line)
|
||||
const line =
|
||||
'==================================================================================================================================================='
|
||||
logger.Log(line)
|
||||
}
|
||||
|
||||
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('Running as root', logger.GetColor('red'))
|
||||
}
|
||||
|
||||
httpServer.listen(port)
|
||||
if (httpsServer) {
|
||||
httpsServer.listen(httpsport)
|
||||
httpsServer.listen(httpsport)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue