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 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) {
|
app.post('/isAdding', function (req, res) {
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
|
|
||||||
// automatically saves to dataFile every n write
|
// 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({
|
res.json({
|
||||||
success: result !== -1,
|
success: result !== -1,
|
||||||
newQuestions: result
|
newQuestions: result
|
||||||
|
@ -328,12 +357,11 @@ app.get('/infos', function (req, res) {
|
||||||
if (req.query.subjinfo) {
|
if (req.query.subjinfo) {
|
||||||
result.subjinfo = getSimplreRes()
|
result.subjinfo = getSimplreRes()
|
||||||
}
|
}
|
||||||
// TODO: watch file, and global variable for motd version
|
|
||||||
if (req.query.version) {
|
if (req.query.version) {
|
||||||
result.version = utils.ReadFile(versionFile)
|
result.version = version
|
||||||
}
|
}
|
||||||
if (req.query.motd) {
|
if (req.query.motd) {
|
||||||
result.motd = utils.ReadFile(motdFile)
|
result.motd = motd
|
||||||
}
|
}
|
||||||
logger.LogReq(req)
|
logger.LogReq(req)
|
||||||
res.json(result)
|
res.json(result)
|
||||||
|
|
|
@ -25,20 +25,18 @@ module.exports = {
|
||||||
|
|
||||||
const dataFile = './public/data.json'
|
const dataFile = './public/data.json'
|
||||||
const recDataFile = './stats/recdata'
|
const recDataFile = './stats/recdata'
|
||||||
const versionFile = './public/version'
|
|
||||||
const motdFile = './public/motd'
|
|
||||||
const qaFile = './public/qa'
|
const qaFile = './public/qa'
|
||||||
|
|
||||||
const logger = require('../utils/logger.js')
|
const logger = require('../utils/logger.js')
|
||||||
const idStats = require('../utils/ids.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 utils = require('../utils/utils.js')
|
||||||
const classes = require('./question-classes/classes.js')
|
const classes = require('./question-classes/classes.js')
|
||||||
|
|
||||||
const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate
|
const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate
|
||||||
var currWrites = 0
|
var currWrites = 0
|
||||||
|
|
||||||
function ProcessIncomingRequest (recievedData, qdb) {
|
function ProcessIncomingRequest (recievedData, qdb, infos) {
|
||||||
if (recievedData === undefined) {
|
if (recievedData === undefined) {
|
||||||
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
|
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
|
||||||
return
|
return
|
||||||
|
@ -63,12 +61,6 @@ function ProcessIncomingRequest (recievedData, qdb) {
|
||||||
d.quiz.forEach((question) => {
|
d.quiz.forEach((question) => {
|
||||||
let q = new classes.Question(question.Q, question.A, question.data)
|
let q = new classes.Question(question.Q, question.A, question.data)
|
||||||
let sames = qdb.Search(q, d.subj)
|
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
|
// if it didnt find any question, or every found questions match is lower thatn 80
|
||||||
let isNew = sames.length === 0 || sames.every(searchResItem => {
|
let isNew = sames.length === 0 || sames.every(searchResItem => {
|
||||||
return searchResItem.match < classes.minMatchAmmount
|
return searchResItem.match < classes.minMatchAmmount
|
||||||
|
@ -93,9 +85,8 @@ function ProcessIncomingRequest (recievedData, qdb) {
|
||||||
if (currWrites >= writeAfter) {
|
if (currWrites >= writeAfter) {
|
||||||
currWrites = 0
|
currWrites = 0
|
||||||
try {
|
try {
|
||||||
// TODO: with utils.WatchFile
|
qdb.version = infos.version
|
||||||
qdb.version = utils.ReadFile(versionFile)
|
qdb.motd = infos.motd
|
||||||
qdb.motd = utils.ReadFile(motdFile)
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.Log('MOTD/Version writing/reading error!')
|
logger.Log('MOTD/Version writing/reading error!')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue