Watched files logic refactor, loading api / redirect from file

This commit is contained in:
mrfry 2020-12-16 17:48:34 +01:00
parent 3b902d736f
commit c8161e4dce
3 changed files with 54 additions and 25 deletions

View file

@ -55,6 +55,8 @@ const testUsersFile = 'data/testUsers.json'
const idStatFile = 'stats/idstats' const idStatFile = 'stats/idstats'
const idvStatFile = 'stats/idvstats' const idvStatFile = 'stats/idvstats'
const todosFile = 'data/todos.json' const todosFile = 'data/todos.json'
const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js'
const rootRedirectToFile = 'data/apiRootRedirectTo'
// other constants // other constants
const line = '====================================================' // lol const line = '====================================================' // lol
@ -107,7 +109,6 @@ function GetApp(): ModuleType {
] ]
const motdFile = publicDir + 'motd' const motdFile = publicDir + 'motd'
const userSpecificMotdFile = publicDir + 'userSpecificMotd.json' const userSpecificMotdFile = publicDir + 'userSpecificMotd.json'
const versionFile = publicDir + 'version'
let domain = url.split('.') // [ "https://api", "frylabs", "net" ] let domain = url.split('.') // [ "https://api", "frylabs", "net" ]
domain.shift() // [ "frylabs", "net" ] domain.shift() // [ "frylabs", "net" ]
@ -156,6 +157,7 @@ function GetApp(): ModuleType {
const questionDbs = loadJSON(dataFiles) const questionDbs = loadJSON(dataFiles)
let version = '' let version = ''
let rootRedirectURL = ''
let motd = '' let motd = ''
let userSpecificMotd = {} let userSpecificMotd = {}
// FIXME: check type from file // FIXME: check type from file
@ -175,7 +177,16 @@ function GetApp(): ModuleType {
} }
function LoadVersion() { 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() { function LoadMOTD() {
@ -198,6 +209,10 @@ function GetApp(): ModuleType {
} }
} }
function reloadRootRedirectURL() {
rootRedirectURL = utils.ReadFile(rootRedirectToFile)
}
function userShouldGetUserSpecificMOTD(id) { function userShouldGetUserSpecificMOTD(id) {
let shouldSee = true let shouldSee = true
let write = false let write = false
@ -231,30 +246,44 @@ function GetApp(): ModuleType {
return shouldSee 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() { function Load() {
backupData(questionDbs) backupData(questionDbs)
utils.WatchFile(userSpecificMotdFile, () => { filesToWatch.forEach((ftw) => {
logger.Log(`User Specific Motd updated`, logger.GetColor('green')) utils.WatchFile(ftw.fname, () => {
LoadUserSpecificMOTD() logger.Log(ftw.logMsg)
ftw.action()
}) })
utils.WatchFile(motdFile, () => { ftw.action()
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() Load()
@ -761,7 +790,7 @@ function GetApp(): ModuleType {
app.get('/', function(req: Request, res: any) { app.get('/', function(req: Request, res: any) {
logger.LogReq(req) logger.LogReq(req)
res.redirect('https://www.youtube.com/watch?v=qLrnkK2YEcE') res.redirect(rootRedirectURL)
}) })
app.post('/postfeedbackfile', function(req: Request, res: any) { app.post('/postfeedbackfile', function(req: Request, res: any) {

View file

@ -152,7 +152,7 @@ const cookieSecret = uuidv4()
app.use(cookieParser(cookieSecret)) app.use(cookieParser(cookieSecret))
app.use( app.use(
reqlogger({ reqlogger({
loggableKeywords: [], loggableKeywords: ['news.json'],
loggableModules: ['dataeditor'], loggableModules: ['dataeditor'],
}) })
) )

@ -1 +1 @@
Subproject commit f8d4bf2a414d2973582c08bfa8b8b8f19389b2e9 Subproject commit dc07c903d39003a5cc13302a9ee6e6e3e0b6ee17