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
 | 
					  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 express = require('express')
 | 
				
			||||||
const vhost = require('vhost')
 | 
					const vhost = require('vhost')
 | 
				
			||||||
const logger = require('./utils/logger.js')
 | 
					const logger = require('./utils/logger.js')
 | 
				
			||||||
logger.Load()
 | 
					 | 
				
			||||||
const utils = require('./utils/utils.js')
 | 
					const utils = require('./utils/utils.js')
 | 
				
			||||||
const http = require('http')
 | 
					const http = require('http')
 | 
				
			||||||
const https = require('https')
 | 
					const https = require('https')
 | 
				
			||||||
@@ -39,6 +38,17 @@ const loggableKeywords = [
 | 
				
			|||||||
]
 | 
					]
 | 
				
			||||||
let modules = JSON.parse(utils.ReadFile(modulesFile))
 | 
					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 {
 | 
					try {
 | 
				
			||||||
  if (utils.FileExists(extraModulesFile)) {
 | 
					  if (utils.FileExists(extraModulesFile)) {
 | 
				
			||||||
    const extraModules = JSON.parse(utils.ReadFile(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
 | 
					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')
 | 
				
			||||||
 | 
					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
 | 
					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) {
 | 
				
			||||||
 | 
					  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'))
 | 
				
			||||||
    return
 | 
					    return
 | 
				
			||||||
@@ -48,6 +53,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
 | 
				
			|||||||
    towrite += recievedData
 | 
					    towrite += recievedData
 | 
				
			||||||
    towrite += '\n------------------------------------------------------------------------------\n'
 | 
					    towrite += '\n------------------------------------------------------------------------------\n'
 | 
				
			||||||
    utils.AppendToFile(towrite, recDataFile)
 | 
					    utils.AppendToFile(towrite, recDataFile)
 | 
				
			||||||
 | 
					    logger.DebugLog('recDataFile written', 'actions', 1)
 | 
				
			||||||
  } catch (e) {
 | 
					  } catch (e) {
 | 
				
			||||||
    logger.log('Error writing recieved data.')
 | 
					    logger.log('Error writing recieved data.')
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -55,16 +61,23 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
 | 
				
			|||||||
  try {
 | 
					  try {
 | 
				
			||||||
    // recievedData: { version: "", id: "", subj: "" quiz: {} }
 | 
					    // recievedData: { version: "", id: "", subj: "" quiz: {} }
 | 
				
			||||||
    let d = JSON.parse(recievedData)
 | 
					    let d = JSON.parse(recievedData)
 | 
				
			||||||
 | 
					    logger.DebugLog('recievedData JSON  parsed', 'actions', 1)
 | 
				
			||||||
    let allQLength = d.quiz.length
 | 
					    let allQLength = d.quiz.length
 | 
				
			||||||
    let allQuestions = []
 | 
					    let allQuestions = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    d.quiz.forEach((question) => {
 | 
					    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 q = new classes.Question(question.Q, question.A, question.data)
 | 
				
			||||||
      let sames = qdb.Search(q, d.subj)
 | 
					      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
 | 
					      // 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 < minMatchAmmountToAdd
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
 | 
					      logger.DebugLog('isNew: ' + isNew, 'actions', 2)
 | 
				
			||||||
      if (isNew) {
 | 
					      if (isNew) {
 | 
				
			||||||
        allQuestions.push(q)
 | 
					        allQuestions.push(q)
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@@ -81,15 +94,17 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
 | 
				
			|||||||
      })
 | 
					      })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      currWrites++
 | 
					      currWrites++
 | 
				
			||||||
      console.log(currWrites)
 | 
					      logger.DebugLog('currWrites for data.json: ' + currWrites, 'actions', 1)
 | 
				
			||||||
      if (currWrites >= writeAfter) {
 | 
					      if (currWrites >= writeAfter) {
 | 
				
			||||||
        currWrites = 0
 | 
					        currWrites = 0
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
          qdb.version = infos.version
 | 
					          qdb.version = infos.version
 | 
				
			||||||
          qdb.motd = infos.motd
 | 
					          qdb.motd = infos.motd
 | 
				
			||||||
 | 
					          logger.DebugLog('version and motd set for data.json', 'actions', 3)
 | 
				
			||||||
        } catch (e) {
 | 
					        } catch (e) {
 | 
				
			||||||
          logger.Log('MOTD/Version writing/reading error!')
 | 
					          logger.Log('MOTD/Version writing/reading error!')
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        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)
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@@ -106,6 +121,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) {
 | 
				
			|||||||
    if (d.version !== undefined) { msg += '. Version: ' + d.version }
 | 
					    if (d.version !== undefined) { msg += '. Version: ' + d.version }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    logger.Log('\t' + msg, color)
 | 
					    logger.Log('\t' + msg, color)
 | 
				
			||||||
 | 
					    logger.DebugLog('ProcessIncomingRequest done', 'actions', 1)
 | 
				
			||||||
    return allQuestions.length
 | 
					    return allQuestions.length
 | 
				
			||||||
  } catch (e) {
 | 
					  } catch (e) {
 | 
				
			||||||
    console.log(e)
 | 
					    console.log(e)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,14 +18,18 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 ------------------------------------------------------------------------- */
 | 
					 ------------------------------------------------------------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const hr = '---------------------------------------------------------------------------------'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
  GetDateString: GetDateString,
 | 
					  GetDateString: GetDateString,
 | 
				
			||||||
  Log: Log,
 | 
					  Log: Log,
 | 
				
			||||||
 | 
					  DebugLog: DebugLog,
 | 
				
			||||||
  GetColor: GetColor,
 | 
					  GetColor: GetColor,
 | 
				
			||||||
  LogReq: LogReq,
 | 
					  LogReq: LogReq,
 | 
				
			||||||
  LogStat: LogStat,
 | 
					  LogStat: LogStat,
 | 
				
			||||||
  Load: Load,
 | 
					  Load: Load,
 | 
				
			||||||
  logHashed: logHashed
 | 
					  logHashed: logHashed,
 | 
				
			||||||
 | 
					  hr: hr
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const DELIM = C('green') + '|' + C()
 | 
					const DELIM = C('green') + '|' + C()
 | 
				
			||||||
@@ -43,6 +47,7 @@ const writeInterval = 10
 | 
				
			|||||||
let data = {}
 | 
					let data = {}
 | 
				
			||||||
let vData = {}
 | 
					let vData = {}
 | 
				
			||||||
let writes = 0
 | 
					let writes = 0
 | 
				
			||||||
 | 
					let debugLevel = 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const colors = [
 | 
					const colors = [
 | 
				
			||||||
  'green',
 | 
					  'green',
 | 
				
			||||||
@@ -66,9 +71,28 @@ function GetDateString () {
 | 
				
			|||||||
  return GetRandomColor(m.getHours().toString()) + d + C()
 | 
					  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) {
 | 
					function Log (s, c) {
 | 
				
			||||||
  let dl = DELIM + C(c)
 | 
					  let log = s
 | 
				
			||||||
  let log = C(c) + GetDateString() + dl + s
 | 
					  if (typeof s !== 'object') {
 | 
				
			||||||
 | 
					    let dl = DELIM + C(c)
 | 
				
			||||||
 | 
					    log = C(c) + GetDateString() + dl + s
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  console.log(log)
 | 
					  console.log(log)
 | 
				
			||||||
  utils.AppendToFile(log, logFile)
 | 
					  utils.AppendToFile(log, logFile)
 | 
				
			||||||
@@ -132,7 +156,10 @@ function setNoLogReadInterval () {
 | 
				
			|||||||
  parseNoLogFile(utils.ReadFile(nologFile))
 | 
					  parseNoLogFile(utils.ReadFile(nologFile))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function Load () {
 | 
					function Load (debugLvl) {
 | 
				
			||||||
 | 
					  if (debugLvl) {
 | 
				
			||||||
 | 
					    debugLevel = debugLvl
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  Log('Loading logger...')
 | 
					  Log('Loading logger...')
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    var prevData = utils.ReadFile(statFile)
 | 
					    var prevData = utils.ReadFile(statFile)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user