mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Watched files logic refactor, loading api / redirect from file
This commit is contained in:
parent
3b902d736f
commit
c8161e4dce
3 changed files with 54 additions and 25 deletions
|
@ -55,6 +55,8 @@ const testUsersFile = 'data/testUsers.json'
|
|||
const idStatFile = 'stats/idstats'
|
||||
const idvStatFile = 'stats/idvstats'
|
||||
const todosFile = 'data/todos.json'
|
||||
const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js'
|
||||
const rootRedirectToFile = 'data/apiRootRedirectTo'
|
||||
|
||||
// other constants
|
||||
const line = '====================================================' // lol
|
||||
|
@ -107,7 +109,6 @@ function GetApp(): ModuleType {
|
|||
]
|
||||
const motdFile = publicDir + 'motd'
|
||||
const userSpecificMotdFile = publicDir + 'userSpecificMotd.json'
|
||||
const versionFile = publicDir + 'version'
|
||||
|
||||
let domain = url.split('.') // [ "https://api", "frylabs", "net" ]
|
||||
domain.shift() // [ "frylabs", "net" ]
|
||||
|
@ -156,6 +157,7 @@ function GetApp(): ModuleType {
|
|||
|
||||
const questionDbs = loadJSON(dataFiles)
|
||||
let version = ''
|
||||
let rootRedirectURL = ''
|
||||
let motd = ''
|
||||
let userSpecificMotd = {}
|
||||
// FIXME: check type from file
|
||||
|
@ -175,7 +177,16 @@ function GetApp(): ModuleType {
|
|||
}
|
||||
|
||||
function LoadVersion() {
|
||||
version = utils.ReadFile(versionFile)
|
||||
const scriptContent = utils.ReadFile(userScriptFile)
|
||||
|
||||
let temp: any = scriptContent.split('\n').find((x) => {
|
||||
return x.includes('@version')
|
||||
})
|
||||
temp = temp.split(' ')
|
||||
temp = temp[temp.length - 1]
|
||||
|
||||
version = temp
|
||||
console.log(version)
|
||||
}
|
||||
|
||||
function LoadMOTD() {
|
||||
|
@ -198,6 +209,10 @@ function GetApp(): ModuleType {
|
|||
}
|
||||
}
|
||||
|
||||
function reloadRootRedirectURL() {
|
||||
rootRedirectURL = utils.ReadFile(rootRedirectToFile)
|
||||
}
|
||||
|
||||
function userShouldGetUserSpecificMOTD(id) {
|
||||
let shouldSee = true
|
||||
let write = false
|
||||
|
@ -231,30 +246,44 @@ function GetApp(): ModuleType {
|
|||
return shouldSee
|
||||
}
|
||||
|
||||
const filesToWatch = [
|
||||
{
|
||||
fname: userSpecificMotdFile,
|
||||
logMsg: 'User Specific Motd updated',
|
||||
action: LoadUserSpecificMOTD,
|
||||
},
|
||||
{
|
||||
fname: motdFile,
|
||||
logMsg: 'Motd updated',
|
||||
action: LoadMOTD,
|
||||
},
|
||||
{
|
||||
fname: testUsersFile,
|
||||
logMsg: 'Test Users file changed',
|
||||
action: LoadTestUsers,
|
||||
},
|
||||
{
|
||||
fname: userScriptFile,
|
||||
logMsg: 'User script file changed',
|
||||
action: LoadVersion,
|
||||
},
|
||||
{
|
||||
fname: rootRedirectToFile,
|
||||
logMsg: 'Root redirect URL changed',
|
||||
action: reloadRootRedirectURL,
|
||||
},
|
||||
]
|
||||
|
||||
function Load() {
|
||||
backupData(questionDbs)
|
||||
|
||||
utils.WatchFile(userSpecificMotdFile, () => {
|
||||
logger.Log(`User Specific Motd updated`, logger.GetColor('green'))
|
||||
LoadUserSpecificMOTD()
|
||||
filesToWatch.forEach((ftw) => {
|
||||
utils.WatchFile(ftw.fname, () => {
|
||||
logger.Log(ftw.logMsg)
|
||||
ftw.action()
|
||||
})
|
||||
ftw.action()
|
||||
})
|
||||
utils.WatchFile(motdFile, () => {
|
||||
logger.Log(`Motd updated`, logger.GetColor('green'))
|
||||
LoadMOTD()
|
||||
})
|
||||
utils.WatchFile(versionFile, (newData) => {
|
||||
logger.Log(`Version changed: ${newData.replace(/\/n/g, '')}`)
|
||||
LoadVersion()
|
||||
})
|
||||
utils.WatchFile(testUsersFile, () => {
|
||||
logger.Log(`Test Users file changed`, logger.GetColor('green'))
|
||||
LoadTestUsers()
|
||||
})
|
||||
|
||||
LoadUserSpecificMOTD()
|
||||
LoadTestUsers()
|
||||
LoadVersion()
|
||||
LoadMOTD()
|
||||
}
|
||||
|
||||
Load()
|
||||
|
@ -761,7 +790,7 @@ function GetApp(): ModuleType {
|
|||
|
||||
app.get('/', function(req: Request, res: any) {
|
||||
logger.LogReq(req)
|
||||
res.redirect('https://www.youtube.com/watch?v=qLrnkK2YEcE')
|
||||
res.redirect(rootRedirectURL)
|
||||
})
|
||||
|
||||
app.post('/postfeedbackfile', function(req: Request, res: any) {
|
||||
|
|
|
@ -152,7 +152,7 @@ const cookieSecret = uuidv4()
|
|||
app.use(cookieParser(cookieSecret))
|
||||
app.use(
|
||||
reqlogger({
|
||||
loggableKeywords: [],
|
||||
loggableKeywords: ['news.json'],
|
||||
loggableModules: ['dataeditor'],
|
||||
})
|
||||
)
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit f8d4bf2a414d2973582c08bfa8b8b8f19389b2e9
|
||||
Subproject commit dc07c903d39003a5cc13302a9ee6e6e3e0b6ee17
|
Loading…
Add table
Add a link
Reference in a new issue