Skip to content

Instantly share code, notes, and snippets.

@hoangsetup
Last active August 14, 2021 03:20
Show Gist options
  • Save hoangsetup/fc113a3ab2174035eea6ee8262f03a49 to your computer and use it in GitHub Desktop.
Save hoangsetup/fc113a3ab2174035eea6ee8262f03a49 to your computer and use it in GitHub Desktop.
diff --git a/src/application.ts b/src/application.ts
index e9b2e72..4eb841f 100644
--- a/src/application.ts
+++ b/src/application.ts
@@ -1,4 +1,7 @@
-import express, { Application as ExApplication } from 'express';
+import express, { Application as ExApplication, Handler } from 'express';
+import { controllers } from './controllers';
+import { MetadataKeys } from './utils/metadata.keys';
+import { IRouter } from './utils/decorators/handlers.decorator';
class Application {
private readonly _instance: ExApplication;
@@ -9,6 +12,7 @@ class Application {
constructor() {
this._instance = express();
this.registerRouters();
}
@@ -17,7 +21,29 @@ class Application {
res.json({ message: 'Hello World!' });
});
- // TODO: register routers
+ const info: Array<{ api: string, handler: string }> = [];
+
+ controllers.forEach((controllerClass) => {
+ const controllerInstance: { [handleName: string]: Handler } = new controllerClass() as any;
+
+ const basePath: string = Reflect.getMetadata(MetadataKeys.BASE_PATH, controllerClass);
+ const routers: IRouter[] = Reflect.getMetadata(MetadataKeys.ROUTERS, controllerClass);
+
+ const exRouter = express.Router();
+
+ routers.forEach(({ method, path, handlerName}) => {
+ exRouter[method](path, controllerInstance[String(handlerName)].bind(controllerInstance));
+
+ info.push({
+ api: `${method.toLocaleUpperCase()} ${basePath + path}`,
+ handler: `${controllerClass.name}.${String(handlerName)}`,
+ });
+ });
+
+ this._instance.use(basePath, exRouter);
+ });
+
+ console.table(info);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment