From 2a0926aa7e3e67a15eb43ddafce7953c88b644d1 Mon Sep 17 00:00:00 2001 From: mrfry Date: Tue, 30 Mar 2021 15:56:04 +0200 Subject: [PATCH] Added support for submodules --- .eslintrc.js | 2 +- src/modules/api/api.ts | 38 +++++++++++++++++++++++++++++++++++--- src/types/basicTypes.ts | 8 ++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 7882a39..72a6976 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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', diff --git a/src/modules/api/api.ts b/src/modules/api/api.ts index 110af5b..452f426 100644 --- a/src/modules/api/api.ts +++ b/src/modules/api/api.ts @@ -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 diff --git a/src/types/basicTypes.ts b/src/types/basicTypes.ts index 585db75..1f8d57d 100644 --- a/src/types/basicTypes.ts +++ b/src/types/basicTypes.ts @@ -73,3 +73,11 @@ export interface Request extends express.Request { session: any busboy: any } + +export interface SubmoduleData { + app: express.Application + url: string + publicdirs: Array + userDB?: any + nextdir?: string +}