mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
motd and version file watching and loading
This commit is contained in:
parent
a1e99c53ae
commit
7c7ed47455
2 changed files with 36 additions and 17 deletions
|
@ -66,6 +66,30 @@ app.use(bodyParser.json({
|
|||
}))
|
||||
|
||||
var data = actions.LoadJSON(dataFile)
|
||||
var version = ''
|
||||
var motd = ''
|
||||
|
||||
function LoadVersion () {
|
||||
version = utils.ReadFile(versionFile)
|
||||
}
|
||||
|
||||
function LoadMOTD () {
|
||||
motd = utils.ReadFile(motdFile)
|
||||
}
|
||||
|
||||
function Load () {
|
||||
utils.WatchFile(motdFile, (newData) => {
|
||||
LoadMOTD()
|
||||
})
|
||||
utils.WatchFile(versionFile, (newData) => {
|
||||
LoadVersion()
|
||||
})
|
||||
|
||||
LoadVersion()
|
||||
LoadMOTD()
|
||||
}
|
||||
|
||||
Load()
|
||||
|
||||
// --------------------------------------------------------------
|
||||
|
||||
|
@ -248,8 +272,13 @@ app.route('/badtestsender').post(function (req, res, next) {
|
|||
|
||||
app.post('/isAdding', function (req, res) {
|
||||
logger.LogReq(req)
|
||||
|
||||
// automatically saves to dataFile every n write
|
||||
let result = actions.ProcessIncomingRequest(req.body.datatoadd, data)
|
||||
let result = actions.ProcessIncomingRequest(
|
||||
req.body.datatoadd,
|
||||
data,
|
||||
{ motd, version })
|
||||
|
||||
res.json({
|
||||
success: result !== -1,
|
||||
newQuestions: result
|
||||
|
@ -328,12 +357,11 @@ app.get('/infos', function (req, res) {
|
|||
if (req.query.subjinfo) {
|
||||
result.subjinfo = getSimplreRes()
|
||||
}
|
||||
// TODO: watch file, and global variable for motd version
|
||||
if (req.query.version) {
|
||||
result.version = utils.ReadFile(versionFile)
|
||||
result.version = version
|
||||
}
|
||||
if (req.query.motd) {
|
||||
result.motd = utils.ReadFile(motdFile)
|
||||
result.motd = motd
|
||||
}
|
||||
logger.LogReq(req)
|
||||
res.json(result)
|
||||
|
|
|
@ -25,20 +25,18 @@ module.exports = {
|
|||
|
||||
const dataFile = './public/data.json'
|
||||
const recDataFile = './stats/recdata'
|
||||
const versionFile = './public/version'
|
||||
const motdFile = './public/motd'
|
||||
const qaFile = './public/qa'
|
||||
|
||||
const logger = require('../utils/logger.js')
|
||||
const idStats = require('../utils/ids.js')
|
||||
idStats.Load() // TODO: dont always load when actions.js is used
|
||||
idStats.Load() // FIXME: dont always load when actions.js is used
|
||||
const utils = require('../utils/utils.js')
|
||||
const classes = require('./question-classes/classes.js')
|
||||
|
||||
const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate
|
||||
var currWrites = 0
|
||||
|
||||
function ProcessIncomingRequest (recievedData, qdb) {
|
||||
function ProcessIncomingRequest (recievedData, qdb, infos) {
|
||||
if (recievedData === undefined) {
|
||||
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
|
||||
return
|
||||
|
@ -63,12 +61,6 @@ function ProcessIncomingRequest (recievedData, qdb) {
|
|||
d.quiz.forEach((question) => {
|
||||
let q = new classes.Question(question.Q, question.A, question.data)
|
||||
let sames = qdb.Search(q, d.subj)
|
||||
if (sames.length === 0) {
|
||||
sames = qdb.Search(q, d.subj)
|
||||
if (sames.length !== 0) {
|
||||
logger.Log(`\t'${d.subj}' gave no result but '' did! (adding new questions)`, logger.GetColor('redbg'))
|
||||
}
|
||||
}
|
||||
// if it didnt find any question, or every found questions match is lower thatn 80
|
||||
let isNew = sames.length === 0 || sames.every(searchResItem => {
|
||||
return searchResItem.match < classes.minMatchAmmount
|
||||
|
@ -93,9 +85,8 @@ function ProcessIncomingRequest (recievedData, qdb) {
|
|||
if (currWrites >= writeAfter) {
|
||||
currWrites = 0
|
||||
try {
|
||||
// TODO: with utils.WatchFile
|
||||
qdb.version = utils.ReadFile(versionFile)
|
||||
qdb.motd = utils.ReadFile(motdFile)
|
||||
qdb.version = infos.version
|
||||
qdb.motd = infos.motd
|
||||
} catch (e) {
|
||||
logger.Log('MOTD/Version writing/reading error!')
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue