Merge branch 'master' of gitlab.com:MrFry/mrfrys-node-server

This commit is contained in:
MrFry 2020-05-10 21:29:08 +02:00
commit fe8503a43e
4 changed files with 26 additions and 4 deletions

View file

@ -6,6 +6,7 @@ module.exports = function (options) {
const { userDB, jsonResponse, exceptions } = options const { userDB, jsonResponse, exceptions } = options
const renderLogin = (req, res) => { const renderLogin = (req, res) => {
res.status('401') // Unauthorized
if (jsonResponse) { if (jsonResponse) {
res.json({ res.json({
result: 'nouser', result: 'nouser',

View file

@ -41,6 +41,7 @@ const dailyDataCountFile = 'stats/dailyDataCount'
const usersDbBackupPath = 'data/dbs/backup' const usersDbBackupPath = 'data/dbs/backup'
const quickVoteResultsDir = 'stats/qvote' const quickVoteResultsDir = 'stats/qvote'
const quickVotes = 'stats/qvote/votes.json' const quickVotes = 'stats/qvote/votes.json'
const testUsersFile = 'data/testUsers.json'
// other constants // other constants
const maxVeteranPwGetCount = 10 const maxVeteranPwGetCount = 10
@ -104,6 +105,7 @@ function GetApp () {
var data = actions.LoadJSON(dataFile) var data = actions.LoadJSON(dataFile)
var version = '' var version = ''
var motd = '' var motd = ''
var testUsers = []
function LoadVersion () { function LoadVersion () {
version = utils.ReadFile(versionFile) version = utils.ReadFile(versionFile)
@ -113,6 +115,13 @@ function GetApp () {
motd = utils.ReadFile(motdFile) motd = utils.ReadFile(motdFile)
} }
function LoadTestUsers () {
testUsers = utils.ReadJSON(testUsersFile)
if (testUsers) {
testUsers = testUsers.userIds
}
}
function Load () { function Load () {
utils.WatchFile(motdFile, (newData) => { utils.WatchFile(motdFile, (newData) => {
logger.Log(`Motd changed: ${newData.replace(/\/n/g, '')}`) logger.Log(`Motd changed: ${newData.replace(/\/n/g, '')}`)
@ -122,7 +131,12 @@ function GetApp () {
logger.Log(`Version changed: ${newData.replace(/\/n/g, '')}`) logger.Log(`Version changed: ${newData.replace(/\/n/g, '')}`)
LoadVersion() LoadVersion()
}) })
utils.WatchFile(testUsersFile, (newData) => {
logger.Log(`Test Users file changed: ${newData.replace(/\/n/g, '')}`)
LoadTestUsers()
})
LoadTestUsers()
LoadVersion() LoadVersion()
LoadMOTD() LoadMOTD()
} }
@ -566,12 +580,17 @@ function GetApp () {
app.post('/isAdding', function (req, res) { app.post('/isAdding', function (req, res) {
logger.LogReq(req) logger.LogReq(req)
const user = req.session.user
const dryRun = testUsers.includes(user.id)
// automatically saves to dataFile every n write // automatically saves to dataFile every n write
// FIXME: req.body.datatoadd is for backwards compatibility, remove this sometime in the future // FIXME: req.body.datatoadd is for backwards compatibility, remove this sometime in the future
let result = actions.ProcessIncomingRequest( let result = actions.ProcessIncomingRequest(
req.body.datatoadd || req.body, req.body.datatoadd || req.body,
data, data,
{ motd, version } { motd, version },
dryRun
) )
res.json({ res.json({

@ -1 +1 @@
Subproject commit 2c815c8d20b6c25a4214d6118760d993f2287f36 Subproject commit f8d4bf2a414d2973582c08bfa8b8b8f19389b2e9

View file

@ -38,7 +38,7 @@ const minMatchAmmountToAdd = 90 // FIXME: test this value
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, infos) { function ProcessIncomingRequest (recievedData, qdb, infos, dryRun) {
logger.DebugLog('Processing incoming request', 'actions', 1) logger.DebugLog('Processing incoming request', 'actions', 1)
if (recievedData === undefined) { if (recievedData === undefined) {
logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg')) logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
@ -108,7 +108,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
currWrites++ currWrites++
logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1) logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1)
if (currWrites >= writeAfter) { if (currWrites >= writeAfter && !dryRun) {
currWrites = 0 currWrites = 0
try { try {
qdb.version = infos.version qdb.version = infos.version
@ -120,6 +120,8 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
logger.DebugLog('Writing data.json', 'actions', 1) logger.DebugLog('Writing data.json', 'actions', 1)
utils.WriteFile(JSON.stringify(qdb), dataFile) utils.WriteFile(JSON.stringify(qdb), dataFile)
logger.Log('\tData file written', color) logger.Log('\tData file written', color)
} else if (dryRun) {
logger.Log('\tDry run')
} }
} else { } else {
msg += `No new data ( ${allQLength} )` msg += `No new data ( ${allQLength} )`