mirror of
				https://gitlab.com/MrFry/mrfrys-node-server
				synced 2025-04-01 20:24:18 +02:00 
			
		
		
		
	merge with master
This commit is contained in:
		@@ -68,4 +68,4 @@ exports.setup = (x) => {
 | 
			
		||||
  url = x.url
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
logger.Log('CV module started', logger.GetColor('yellow'))
 | 
			
		||||
logger.Log('Qmining module started', logger.GetColor('yellow'))
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										12
									
								
								server.js
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								server.js
									
									
									
									
									
								
							@@ -25,7 +25,6 @@ const httpsport = 5001
 | 
			
		||||
const express = require('express')
 | 
			
		||||
const vhost = require('vhost')
 | 
			
		||||
const logger = require('./utils/logger.js')
 | 
			
		||||
logger.Load()
 | 
			
		||||
const utils = require('./utils/utils.js')
 | 
			
		||||
const http = require('http')
 | 
			
		||||
const https = require('https')
 | 
			
		||||
@@ -39,6 +38,17 @@ const loggableKeywords = [
 | 
			
		||||
]
 | 
			
		||||
let modules = JSON.parse(utils.ReadFile(modulesFile))
 | 
			
		||||
 | 
			
		||||
let logLevel = parseInt(GetParams()[0])
 | 
			
		||||
if (isNaN(logLevel)) {
 | 
			
		||||
  logLevel = 0
 | 
			
		||||
}
 | 
			
		||||
logger.Log('Loglevel is: ' + logLevel)
 | 
			
		||||
logger.Load(logLevel) // TODO: debug level in enviromet variable / param
 | 
			
		||||
 | 
			
		||||
function GetParams () {
 | 
			
		||||
  return process.argv.splice(2)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
try {
 | 
			
		||||
  if (utils.FileExists(extraModulesFile)) {
 | 
			
		||||
    const extraModules = JSON.parse(utils.ReadFile(extraModulesFile))
 | 
			
		||||
 
 | 
			
		||||
@@ -32,11 +32,16 @@ const idStats = require('../utils/ids.js')
 | 
			
		||||
idStats.Load() // FIXME: dont always load when actions.js is used
 | 
			
		||||
const utils = require('../utils/utils.js')
 | 
			
		||||
const classes = require('./question-classes/classes.js')
 | 
			
		||||
classes.initLogger(logger.DebugLog)
 | 
			
		||||
// if a recievend question doesnt match at least this % to any other question in the db it gets
 | 
			
		||||
// added to db
 | 
			
		||||
const minMatchAmmountToAdd = 90 // FIXME: test this value
 | 
			
		||||
 | 
			
		||||
const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate
 | 
			
		||||
var currWrites = 0
 | 
			
		||||
 | 
			
		||||
function ProcessIncomingRequest (recievedData, qdb, infos) {
 | 
			
		||||
  logger.DebugLog('Processing incoming request', 'actions', 1)
 | 
			
		||||
  if (recievedData === undefined) {
 | 
			
		||||
    logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg'))
 | 
			
		||||
    return
 | 
			
		||||
@@ -48,6 +53,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
 | 
			
		||||
    towrite += recievedData
 | 
			
		||||
    towrite += '\n------------------------------------------------------------------------------\n'
 | 
			
		||||
    utils.AppendToFile(towrite, recDataFile)
 | 
			
		||||
    logger.DebugLog('recDataFile written', 'actions', 1)
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    logger.log('Error writing recieved data.')
 | 
			
		||||
  }
 | 
			
		||||
@@ -55,16 +61,23 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
 | 
			
		||||
  try {
 | 
			
		||||
    // recievedData: { version: "", id: "", subj: "" quiz: {} }
 | 
			
		||||
    let d = JSON.parse(recievedData)
 | 
			
		||||
    logger.DebugLog('recievedData JSON  parsed', 'actions', 1)
 | 
			
		||||
    let allQLength = d.quiz.length
 | 
			
		||||
    let allQuestions = []
 | 
			
		||||
 | 
			
		||||
    d.quiz.forEach((question) => {
 | 
			
		||||
      logger.DebugLog('Question:', 'actions', 2)
 | 
			
		||||
      logger.DebugLog(question, 'actions', 2)
 | 
			
		||||
      let q = new classes.Question(question.Q, question.A, question.data)
 | 
			
		||||
      let sames = qdb.Search(q, d.subj)
 | 
			
		||||
      logger.DebugLog('Same questions:', 'actions', 2)
 | 
			
		||||
      logger.DebugLog('Length: ' + sames.length, 'actions', 2)
 | 
			
		||||
      logger.DebugLog(sames, 'actions', 3)
 | 
			
		||||
      // 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
 | 
			
		||||
        return searchResItem.match < minMatchAmmountToAdd
 | 
			
		||||
      })
 | 
			
		||||
      logger.DebugLog('isNew: ' + isNew, 'actions', 2)
 | 
			
		||||
      if (isNew) {
 | 
			
		||||
        allQuestions.push(q)
 | 
			
		||||
      }
 | 
			
		||||
@@ -81,15 +94,17 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
 | 
			
		||||
      })
 | 
			
		||||
 | 
			
		||||
      currWrites++
 | 
			
		||||
      console.log(currWrites)
 | 
			
		||||
      logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1)
 | 
			
		||||
      if (currWrites >= writeAfter) {
 | 
			
		||||
        currWrites = 0
 | 
			
		||||
        try {
 | 
			
		||||
          qdb.version = infos.version
 | 
			
		||||
          qdb.motd = infos.motd
 | 
			
		||||
          logger.DebugLog('version and motd set for data.json', 'actions', 3)
 | 
			
		||||
        } catch (e) {
 | 
			
		||||
          logger.Log('MOTD/Version writing/reading error!')
 | 
			
		||||
        }
 | 
			
		||||
        logger.DebugLog('Writing data.json', 'actions', 1)
 | 
			
		||||
        utils.WriteFile(JSON.stringify(qdb), dataFile)
 | 
			
		||||
        logger.Log('\tData file written', color)
 | 
			
		||||
      }
 | 
			
		||||
@@ -106,6 +121,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
 | 
			
		||||
    if (d.version !== undefined) { msg += '. Version: ' + d.version }
 | 
			
		||||
 | 
			
		||||
    logger.Log('\t' + msg, color)
 | 
			
		||||
    logger.DebugLog('ProcessIncomingRequest done', 'actions', 1)
 | 
			
		||||
    return allQuestions.length
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    console.log(e)
 | 
			
		||||
 
 | 
			
		||||
@@ -18,14 +18,18 @@
 | 
			
		||||
 | 
			
		||||
 ------------------------------------------------------------------------- */
 | 
			
		||||
 | 
			
		||||
const hr = '---------------------------------------------------------------------------------'
 | 
			
		||||
 | 
			
		||||
module.exports = {
 | 
			
		||||
  GetDateString: GetDateString,
 | 
			
		||||
  Log: Log,
 | 
			
		||||
  DebugLog: DebugLog,
 | 
			
		||||
  GetColor: GetColor,
 | 
			
		||||
  LogReq: LogReq,
 | 
			
		||||
  LogStat: LogStat,
 | 
			
		||||
  Load: Load,
 | 
			
		||||
  logHashed: logHashed
 | 
			
		||||
  logHashed: logHashed,
 | 
			
		||||
  hr: hr
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const DELIM = C('green') + '|' + C()
 | 
			
		||||
@@ -43,6 +47,7 @@ const writeInterval = 10
 | 
			
		||||
let data = {}
 | 
			
		||||
let vData = {}
 | 
			
		||||
let writes = 0
 | 
			
		||||
let debugLevel = 0
 | 
			
		||||
 | 
			
		||||
const colors = [
 | 
			
		||||
  'green',
 | 
			
		||||
@@ -66,9 +71,28 @@ function GetDateString () {
 | 
			
		||||
  return GetRandomColor(m.getHours().toString()) + d + C()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function DebugLog (msg, name, lvl) {
 | 
			
		||||
  if (lvl <= debugLevel) {
 | 
			
		||||
    if (msg === 'hr') {
 | 
			
		||||
      msg = hr
 | 
			
		||||
    }
 | 
			
		||||
    let s = msg
 | 
			
		||||
    let header = `${C('red')}#DEBUG${lvl}#${C('yellow')}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}`
 | 
			
		||||
    if (typeof msg !== 'object') {
 | 
			
		||||
      s = header + msg
 | 
			
		||||
    } else {
 | 
			
		||||
      Log(header + 'OBJECT:', 'yellow')
 | 
			
		||||
    }
 | 
			
		||||
    Log(s, 'yellow')
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function Log (s, c) {
 | 
			
		||||
  let dl = DELIM + C(c)
 | 
			
		||||
  let log = C(c) + GetDateString() + dl + s
 | 
			
		||||
  let log = s
 | 
			
		||||
  if (typeof s !== 'object') {
 | 
			
		||||
    let dl = DELIM + C(c)
 | 
			
		||||
    log = C(c) + GetDateString() + dl + s
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  console.log(log)
 | 
			
		||||
  utils.AppendToFile(log, logFile)
 | 
			
		||||
@@ -132,7 +156,10 @@ function setNoLogReadInterval () {
 | 
			
		||||
  parseNoLogFile(utils.ReadFile(nologFile))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function Load () {
 | 
			
		||||
function Load (debugLvl) {
 | 
			
		||||
  if (debugLvl) {
 | 
			
		||||
    debugLevel = debugLvl
 | 
			
		||||
  }
 | 
			
		||||
  Log('Loading logger...')
 | 
			
		||||
  try {
 | 
			
		||||
    var prevData = utils.ReadFile(statFile)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user