From 03458fe00cf23a3a2db8539c9c1dbc178ad937b0 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Thu, 16 Sep 2021 18:14:49 +0700 Subject: [PATCH] Add express server from module and updated the server and config --- .gitmodules | 3 ++ package.json | 7 ++-- packages/cubetiq-express-server | 1 + packages/ts-common | 2 +- src/app.ts | 28 +++++++++++-- src/application.ts | 54 -------------------------- src/constants/metadata.keys.ts | 4 -- src/controller/index.controller.ts | 2 +- src/controller/person.controller.ts | 7 +++- src/decorators/controller.decorator.ts | 7 ---- src/decorators/handlers.decorator.ts | 47 ---------------------- src/decorators/index.ts | 2 - src/server.ts | 32 ++++----------- 13 files changed, 48 insertions(+), 148 deletions(-) create mode 160000 packages/cubetiq-express-server delete mode 100644 src/application.ts delete mode 100644 src/constants/metadata.keys.ts delete mode 100644 src/decorators/controller.decorator.ts delete mode 100644 src/decorators/handlers.decorator.ts delete mode 100644 src/decorators/index.ts diff --git a/.gitmodules b/.gitmodules index df4de5e..2e0d4be 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "packages/ts-common"] path = packages/ts-common url = https://git.cubetiqs.com/CUBETIQ/ts-common.git +[submodule "packages/cubetiq-express-server"] + path = packages/cubetiq-express-server + url = https://git.cubetiqs.com/CUBETIQ/cubetiq-express-server.git diff --git a/package.json b/package.json index 5dfde25..d5af305 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ }, "keywords": [ "Express", - "Nodejs" + "Nodejs", + "Typescript" ], "author": "Sambo Chea ", "license": "ISC", @@ -41,8 +42,8 @@ }, "dependencies": { "@cubetiq/ts-common": "1.0.0", - "express": "^4.17.1", - "reflect-metadata": "^0.1.13" + "@cubetiq/express-server": "1.0.0", + "express": "^4.17.1" }, "lint-staged": { "**/*": "prettier --write --ignore-unknown" diff --git a/packages/cubetiq-express-server b/packages/cubetiq-express-server new file mode 160000 index 0000000..558626d --- /dev/null +++ b/packages/cubetiq-express-server @@ -0,0 +1 @@ +Subproject commit 558626d0e4d955d0b702365b507d2697ef83196d diff --git a/packages/ts-common b/packages/ts-common index c429644..d011f45 160000 --- a/packages/ts-common +++ b/packages/ts-common @@ -1 +1 @@ -Subproject commit c42964407eaa35f0beea28926d234f74678b6cfc +Subproject commit d011f4547ebe4f1b36dc12fbb06b626e7345dc65 diff --git a/src/app.ts b/src/app.ts index 135f264..4bb566b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,8 +1,28 @@ // app config import "./dotenv" -// reflection metadata for decorator -import "reflect-metadata" +import { createServer } from "http" +import { SERVER_PORT } from "./app.config" +import { info } from "@cubetiq/ts-common/dist/log" +import application from "./server" -// core app -import "./server" +// get current host id +const hostId = `${require("os").hostname()}#${process.pid}` +const startedAt = new Date() + +const app = application.instance +const httpServer = createServer(app) + +info( + `Application server running on: ${hostId} at port: ${SERVER_PORT} and started at: ${startedAt}` +) +httpServer.listen(SERVER_PORT) + +app.get("/info", (req, res) => { + res.type("json") + res.send({ + startedAt, + message: `Instance id: ${hostId}`, + status: "OK", + }) +}) diff --git a/src/application.ts b/src/application.ts deleted file mode 100644 index 272bdd6..0000000 --- a/src/application.ts +++ /dev/null @@ -1,54 +0,0 @@ -import express, { Application as ExpressApp, Handler } from "express" -import { controllers } from "./controller" -import { RouteHandler } from "./decorators/handlers.decorator" -import { MetadataKeys } from "./constants/metadata.keys" - -class Application { - private readonly _instance: ExpressApp - - get instance(): ExpressApp { - return this._instance - } - - constructor() { - this._instance = express() - this._instance.use(express.json()) - this._instance.use(express.urlencoded({ extended: false })) - this.registerRoutes() - } - - private registerRoutes(): void { - const info: Array<{ api: string; handler: string }> = [] - - controllers.forEach((controller) => { - const controllerInstance: { [handlerName: string]: Handler } = - new controller() as any - const basePath: string = Reflect.getMetadata( - MetadataKeys.BASE_PATH, - controller - ) - const routers: RouteHandler[] = Reflect.getMetadata( - MetadataKeys.ROUTERS, - controller - ) - 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: `${controller.name}.${String(handlerName)}`, - }) - }) - - this._instance.use(basePath, exRouter) - }) - - console.table(info) - } -} - -export default new Application() diff --git a/src/constants/metadata.keys.ts b/src/constants/metadata.keys.ts deleted file mode 100644 index 8a02767..0000000 --- a/src/constants/metadata.keys.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum MetadataKeys { - BASE_PATH = "base_path", - ROUTERS = "routers", -} diff --git a/src/controller/index.controller.ts b/src/controller/index.controller.ts index 6d8d6c4..dfbd586 100644 --- a/src/controller/index.controller.ts +++ b/src/controller/index.controller.ts @@ -1,5 +1,5 @@ import { Request, Response } from "express" -import { Controller, Get } from "../decorators" +import { Controller, Get } from "@cubetiq/express-server/dist/decorators" @Controller("/hello") export default class IndexController { diff --git a/src/controller/person.controller.ts b/src/controller/person.controller.ts index b3c8be1..515111c 100644 --- a/src/controller/person.controller.ts +++ b/src/controller/person.controller.ts @@ -1,5 +1,10 @@ import { Request, Response } from "express" -import { Controller, Delete, Get, Post } from "../decorators" +import { + Controller, + Delete, + Get, + Post, +} from "@cubetiq/express-server/dist/decorators" const persons: Array = [ { diff --git a/src/decorators/controller.decorator.ts b/src/decorators/controller.decorator.ts deleted file mode 100644 index c267e2c..0000000 --- a/src/decorators/controller.decorator.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { MetadataKeys } from "../constants/metadata.keys" - -export const Controller = (basePath: string): ClassDecorator => { - return (target: any) => { - Reflect.defineMetadata(MetadataKeys.BASE_PATH, basePath, target) - } -} diff --git a/src/decorators/handlers.decorator.ts b/src/decorators/handlers.decorator.ts deleted file mode 100644 index 358a356..0000000 --- a/src/decorators/handlers.decorator.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { MetadataKeys } from "../constants/metadata.keys" - -export enum Method { - GET = "get", - POST = "post", - PUT = "put", - DELETE = "delete", - PATCH = "patch", -} - -export interface RouteHandler { - method: Method - path: string - handlerName: string | symbol -} - -const methodDecoratorFactory = (method: Method) => { - return (path: string = ""): MethodDecorator => { - return (target: any, propertyKey: string | symbol): void => { - const controllerClass = target.constructor - const routers: RouteHandler[] = Reflect.hasMetadata( - MetadataKeys.ROUTERS, - controllerClass - ) - ? Reflect.getMetadata(MetadataKeys.ROUTERS, controllerClass) - : [] - - routers.push({ - method, - path, - handlerName: propertyKey, - }) - - Reflect.defineMetadata( - MetadataKeys.ROUTERS, - routers, - controllerClass - ) - } - } -} - -export const Get = methodDecoratorFactory(Method.GET) -export const Post = methodDecoratorFactory(Method.POST) -export const Put = methodDecoratorFactory(Method.PUT) -export const Delete = methodDecoratorFactory(Method.DELETE) -export const Patch = methodDecoratorFactory(Method.PATCH) diff --git a/src/decorators/index.ts b/src/decorators/index.ts deleted file mode 100644 index dd19214..0000000 --- a/src/decorators/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./controller.decorator" -export * from "./handlers.decorator" diff --git a/src/server.ts b/src/server.ts index 1d1847c..a693cd5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,25 +1,9 @@ -import { createServer } from "http" -import { SERVER_PORT } from "./app.config" -import { info } from "@cubetiq/ts-common/dist/log" -import application from "./application" +import Application from "@cubetiq/express-server/dist" +import { controllers as AllControllers } from "./controller" +class Server extends Application { + get controllers(): any[] { + return AllControllers + } +} -// get current host id -const hostId = `${require("os").hostname()}#${process.pid}` -const startedAt = new Date() - -const app = application.instance -const httpServer = createServer(app) - -info( - `Application server running on: ${hostId} at port: ${SERVER_PORT} and started at: ${startedAt}` -) -httpServer.listen(SERVER_PORT) - -app.get("/info", (req, res) => { - res.type("json") - res.send({ - startedAt, - message: `Instance id: ${hostId}`, - status: "OK", - }) -}) +export default new Server()