Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add router system #34

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/ZumitoFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class ZumitoFramework {
* @see {@link TranslationManager}
*/
translations: TranslationManager;
routes: any;
routes: Map<string, (req: Request, res: Response) => void>;

/**
* The database models loaded in the framework.
Expand Down Expand Up @@ -162,6 +162,7 @@ export class ZumitoFramework {
this.events = new Map();
this.translations = new TranslationManager();
this.models = [];
this.routes = new Map();
this.eventManager = new EventManager();

if (settings.logLevel) {
Expand All @@ -188,12 +189,12 @@ export class ZumitoFramework {
private async initialize() {
await this.initializeDatabase();
await this.initializeDiscordClient();
this.startApiServer();

this.eventManager.addEventEmitter('discord', this.client);
this.eventManager.addEventEmitter('framework', this.eventEmitter);

await this.registerModules();
this.startApiServer();
await this.refreshSlashCommands();
if (this.settings.statusOptions) {
this.statusManager = new StatusManager(this, this.settings.statusOptions);
Expand Down Expand Up @@ -254,6 +255,10 @@ export class ZumitoFramework {
//Route Prefixes
//this.app.use("/", indexRouter);
//this.app.use("/api/", apiRouter);
console.log(this.routes.size);
this.routes.forEach((router, path) => {
this.app.use(path, router);
});

// throw 404 if URL not found
this.app.all('*', function (req, res) {
Expand Down
11 changes: 11 additions & 0 deletions src/definitions/FrameworkRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Request, Response } from 'express';

export abstract class FrameworkRouter {
basePath: string = '';

constructor(basePath: string) {
this.basePath = basePath;
}

abstract getRoutes(): Map<string, (req: Request, res: Response) => void>;
}
28 changes: 28 additions & 0 deletions src/definitions/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import {
} from 'discord.js';
import { DatabaseModel } from './DatabaseModel.js';
import { CommandManager } from '../services/CommandManager.js';
import { Request, Response } from 'express';
import { FrameworkRouter } from "./FrameworkRouter.js";

export abstract class Module {
protected path: string;
protected framework: ZumitoFramework;
protected commands: CommandManager;
protected events: Map<string, FrameworkEvent> = new Map();
protected models: Array<DatabaseModel> = [];
protected routes: Map<string, (req: Request, res: Response) => void> = new Map();


protected commandManager: CommandManager;

Expand All @@ -34,6 +38,7 @@ export abstract class Module {
await this.registerEvents();
await this.registerTranslations();
await this.registerModels();
await this.registerRoutes();
}

async registerCommands() {
Expand Down Expand Up @@ -165,4 +170,27 @@ export abstract class Module {
getModels(): Array<DatabaseModel> {
return this.models;
}

async registerRoutes(subpath = '') {
if (!fs.existsSync(path.join(this.path, 'routes', subpath))) return;
const files = fs.readdirSync(path.join(this.path, 'routes', subpath));
for (const file of files) {
if (file.endsWith('.js') || file.endsWith('.ts')) {
const Router: any = await import('file://' + `${this.path}/routes/${subpath}/${file}`).then(r => Object.values(r)[0]);
if (Router.prototype instanceof FrameworkRouter) {
const router = new Router(subpath ? '/' + subpath : '');
this.routes = new Map([...this.routes, ...router.getRoutes()]);
} else {
console.warn(`[🔄🟡 ] ${subpath}/${file} is not a valid router on module ${this.constructor.name} \n It must extend the FrameworkRouter class instead of ${Router.prototype}`);
continue;
}
} else if (fs.lstatSync(path.join(this.path, 'routes', subpath, file)).isDirectory()) {
await this.registerRoutes(path.join(subpath, file));
}
}
}

getRoutes(): Map<string, (req: Request, res: Response) => void> {
return this.routes;
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { TranslationManager } from './services/TranslationManager.js';
import { ZumitoFramework } from './ZumitoFramework.js';
import * as discord from 'discord.js';
import { EventParameters } from './definitions/parameters/EventParameters.js';
import { FrameworkRouter } from './definitions/FrameworkRouter.js';

export {
ZumitoFramework,
Expand Down Expand Up @@ -54,4 +55,5 @@ export {
StatusManagerOptions,
discord,
EventParameters,
FrameworkRouter,
};
6 changes: 1 addition & 5 deletions src/services/ModuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,8 @@ export class ModuleManager {
this.framework.models.push(model);
});

/*

// Register module routes
this.routes = new Map([...this.routes, ...moduleInstance.getRoutes()]);

*/
this.framework.routes = new Map([...this.framework.routes, ...module.getRoutes()] as any);
}

async instanceModule(module: any, rootPath: string, name?: string) {
Expand Down
Loading