endpoint load order
This commit is contained in:
parent
b50f603ee2
commit
f5538b6a95
@ -27,6 +27,7 @@ By default any OAuth callbacks are expected at `/api/login/<methodname>`, where
|
||||
|
||||
**Endpoints**
|
||||
Any all endpoints should go in the `/endpoints` directory and are expected to inherit from the Endpoint superclass.
|
||||
Endpoints have a loadOrder property that can be adjusted, lower values are loaded first.
|
||||
|
||||
## Main components
|
||||
**Controller:** `/src/controller/Controller.js`
|
||||
|
@ -1,7 +1,7 @@
|
||||
const path = require('node:path');
|
||||
const fs = require('node:fs');
|
||||
|
||||
const {Endpoint: BaseEndpoint} = require('../interfaces')
|
||||
const { Endpoint: BaseEndpoint } = require('../interfaces');
|
||||
const { Util } = require('../../util');
|
||||
const { Collection } = require('@discordjs/collection');
|
||||
|
||||
@ -33,13 +33,13 @@ class Registry {
|
||||
|
||||
const { server, endpoints, logger } = this;
|
||||
|
||||
return (async function read (pth) {
|
||||
(function read (pth) {
|
||||
|
||||
const dir = fs.readdirSync(pth, { withFileTypes: true });
|
||||
|
||||
for (let file of dir) {
|
||||
|
||||
if (file.isDirectory()) await read(path.join(pth, file.name));
|
||||
if (file.isDirectory()) read(path.join(pth, file.name));
|
||||
else {
|
||||
|
||||
file = path.join(pth, file.name);
|
||||
@ -51,10 +51,9 @@ class Registry {
|
||||
continue;
|
||||
}
|
||||
const endpoint = new Endpoint(server);
|
||||
if(!(endpoint instanceof BaseEndpoint)) throw new Error(`Attempt to load invalid endpoint at ${file.name}`)
|
||||
endpoint.init();
|
||||
|
||||
if (!(endpoint instanceof BaseEndpoint)) throw new Error(`Attempt to load invalid endpoint at ${file.name}`);
|
||||
if (endpoints.has(endpoint.resolveable)) throw new Error(`Error registering endpoint ${endpoint.resolveable}: an endpoint with that resolveable already exists`);
|
||||
|
||||
endpoints.set(endpoint.resolveable, endpoint);
|
||||
logger.info(`Created endpoint ${endpoint.name}`);
|
||||
|
||||
@ -64,6 +63,9 @@ class Registry {
|
||||
|
||||
}(this.path));
|
||||
|
||||
const sorted = this.endpoints.sort((a, b) => a.loadOrder - b.loadOrder);
|
||||
for (const ep of sorted.values()) ep.init();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,8 @@ class Api404 extends ApiEndpoint {
|
||||
|
||||
super(server, {
|
||||
name: 'api404',
|
||||
path: '/*'
|
||||
path: '/*',
|
||||
loadOrder: 10
|
||||
});
|
||||
|
||||
this.methods = [
|
||||
|
@ -1,7 +1,7 @@
|
||||
const { Util } = require('../../util');
|
||||
|
||||
class Endpoint {
|
||||
constructor (server, { path, name }) {
|
||||
constructor (server, { path, name, loadOrder = 5 }) {
|
||||
|
||||
if (!server) Util.fatal(new Error('Missing server object in endpoint'));
|
||||
this.server = server;
|
||||
@ -25,6 +25,11 @@ class Endpoint {
|
||||
this.logger = server.createLogger(this);
|
||||
this.debug = false;
|
||||
|
||||
// Used to sort the endpoints from smallest to highest before initialisation
|
||||
// Useful when needing to have certain endpoints register before or after some other endpoint
|
||||
// E.g. 404 pages should be initialised last by having a loadOrder of 10
|
||||
this.loadOrder = loadOrder;
|
||||
|
||||
}
|
||||
|
||||
init () {
|
||||
|
Loading…
Reference in New Issue
Block a user