Added support for submodules

This commit is contained in:
mrfry 2021-03-30 15:56:04 +02:00
parent 7e70aa6c8e
commit 2a0926aa7e
3 changed files with 44 additions and 4 deletions

View file

@ -28,7 +28,7 @@ module.exports = {
'@typescript-eslint/ban-types': 'off',
'id-length': [
'warn',
{ exceptions: ['i', 'j', 't', 'Q', 'A', 'C', 'q', 'a', 'b', 'x'] },
{ exceptions: ['i', 'j', 't', 'Q', 'A', 'C', 'q', 'a', 'b', 'x', 'e'] },
],
'object-shorthand': ['warn', 'never'],
'prefer-const': 'warn',

View file

@ -46,7 +46,7 @@ import {
dataToString,
getSubjNameWithoutYear,
createQuestion,
compareQuestionObj,
// compareQuestionObj,
} from '../../utils/classes'
import {
initWorkerPool,
@ -85,6 +85,7 @@ const uloadFiles = 'data/f'
// other constants
const line = '====================================================' // lol
const moduleName = 'API'
const addPWPerDay = 3 // every x day a user can give a pw
const maxPWCount = 6 // maximum pw give opportunities a user can have at once
const addPWCount = 1 // how many pw gen opportunities to add each time
@ -93,7 +94,7 @@ const minimumAlowwedSessions = 2 // how many sessions are allowed for a user
// stuff gotten from server.js
let userDB
let url // eslint-disable-line
let url
let publicdirs = []
function GetApp(): ModuleType {
@ -1951,6 +1952,8 @@ function GetApp(): ModuleType {
})
})
setupSubModules(app)
// -------------------------------------------------------------------------------------------
app.get('*', function(req: Request, res: any) {
@ -2050,8 +2053,37 @@ function GetApp(): ModuleType {
}
}
function setupSubModules(parentApp) {
const submoduleDir = './submodules/'
const absolutePath = __dirname + '/' + submoduleDir
if (!utils.FileExists(absolutePath)) {
return
}
const files = utils.ReadDir(absolutePath)
files.forEach((file) => {
if (!file.endsWith('.js')) {
return
}
const submodulePath = submoduleDir + file
try {
const mod = require(submodulePath).default // eslint-disable-line
mod.setup({
app: parentApp,
userDB: userDB,
url: url,
publicdirs: publicdirs,
})
logger.Log(`Submodule '${file}' loaded for '${moduleName}'`)
} catch (e) {
logger.Log(`Error loading submodule from ${submodulePath}`)
console.error(e)
}
})
}
export default {
name: 'API',
name: moduleName,
getApp: GetApp,
setup: (data: SetupData): void => {
userDB = data.userDB

View file

@ -73,3 +73,11 @@ export interface Request extends express.Request {
session: any
busboy: any
}
export interface SubmoduleData {
app: express.Application
url: string
publicdirs: Array<string>
userDB?: any
nextdir?: string
}