add missing function + registration variable

This commit is contained in:
Erik 2022-11-15 16:11:58 +02:00
parent 65a5752536
commit 89e6ab5984
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
4 changed files with 31 additions and 11 deletions

View File

@ -76,10 +76,10 @@
],
"lines-around-comment": "warn",
"lines-around-directive": "warn",
"lines-between-class-members": [
"warn",
"always"
],
// "lines-between-class-members": [
// "warn",
// "always"
// ],
"max-classes-per-file": "warn",
"max-nested-callbacks": "warn",
"new-parens": "warn",

View File

@ -9,7 +9,8 @@
"http": {
"port": 4000
},
"validUserTypes": ["user", "service", "system"]
"validUserTypes": ["user", "service", "system"],
"registrationEnabled": true
},
"logger": {
"customTypes": ["access", "unauthorised"],

View File

@ -50,6 +50,7 @@ class Server extends EventEmitter {
this.port = httpOpts.port + this._shardId;
this.domain = NODE_ENV === 'development' ? `localhost:${this.port}` : options.domain;
this.serveFiles = null;
this.registrationEnabled = options.registrationEnabled;
if (options.serveFiles) this.serveFiles = path.resolve(options.serveFiles);
this.server = null;
@ -84,9 +85,9 @@ class Server extends EventEmitter {
this.app.use(helmet());
this.app.use(express.json({ limit: '10mb' }));
this.app.use(express.urlencoded({ extended: true }));
this.app.use(this.logRequest.bind(this)); // Logs every request
this.app.use(this.logError.bind(this)); // Logs endpoints that error and sends a 500
this.app.use(this.ready.bind(this)); // denies requests before the server is ready
this.app.use(this.#logRequest.bind(this)); // Logs every request
this.app.use(this.#logError.bind(this)); // Logs endpoints that error and sends a 500
this.app.use(this.#ready.bind(this)); // denies requests before the server is ready
process.on('message', this._handleMessage.bind(this));
@ -119,19 +120,19 @@ class Server extends EventEmitter {
}
// eslint-disable-next-line no-unused-vars
logError (err, req, res, next) {
#logError (err, req, res, next) {
this.logger.error(`Unhandled error:\n${err.stack || err}`);
res.status(500).send('An internal error was encountered');
}
logRequest (req, res, next) {
#logRequest (req, res, next) {
res.once('finish', () => {
this.logger[[ 401, 403 ].includes(res.statusCode) ? 'unauthorised' : 'access'](`[${req.get('X-Forwarded-For') || req.socket.remoteAddress}] [STATUS: ${res.statusCode}] Request to [${req.method}] ${req.route?.path || req.path}`);
});
next();
}
ready (req, res, next) {
#ready (req, res, next) {
if (!this._ready) return res.status(503).send('Server not ready');
next();
}
@ -156,6 +157,7 @@ class Server extends EventEmitter {
if (msg._shutdown) this.shutdown();
}
// Helper function to pass options to the logger in a unified way
createLogger (comp) {
return new LoggerClient({ name: comp.constructor.name, ...this._options.logger });
}

View File

@ -127,6 +127,23 @@ class MongoDB {
}
/**
* Insert document.
*
* @param {String} db The collection in which the data is to be updated
* @param {Object} filter The filter that is used to find the data
* @param {Object} data The updated data
* @returns {WriteResult} Object containing the followint counts: Matched, Upserted, Modified
* @memberof Database
*/
async insertOne (db, data) {
this.logger.debug(`Incoming insertOne query for ${db} with parameters ${JSON.stringify(data)}`);
const result = await this.db.collection(db).insertOne(data);
return result;
}
/**
* Push data to an array
*