Compare commits
No commits in common. "03458fe00cf23a3a2db8539c9c1dbc178ad937b0" and "00eedce6ea3f4fffc0e4d39f94f572f648b83f1a" have entirely different histories.
03458fe00c
...
00eedce6ea
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,6 +1,3 @@
|
|||||||
[submodule "packages/ts-common"]
|
[submodule "packages/ts-common"]
|
||||||
path = packages/ts-common
|
path = packages/ts-common
|
||||||
url = https://git.cubetiqs.com/CUBETIQ/ts-common.git
|
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
|
|
||||||
|
@ -136,4 +136,4 @@ export const controllers = [IndexController, PersonController, HomeController]
|
|||||||
|
|
||||||
### Contributors
|
### Contributors
|
||||||
|
|
||||||
- Sambo Chea <sombochea@cubetiqs.com> [4099C19544D3FF79]
|
- Sambo Chea <sombochea@cubetiqs.com>
|
||||||
|
@ -18,8 +18,7 @@
|
|||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"Express",
|
"Express",
|
||||||
"Nodejs",
|
"Nodejs"
|
||||||
"Typescript"
|
|
||||||
],
|
],
|
||||||
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
"author": "Sambo Chea <sombochea@cubetiqs.com>",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
@ -42,8 +41,8 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cubetiq/ts-common": "1.0.0",
|
"@cubetiq/ts-common": "1.0.0",
|
||||||
"@cubetiq/express-server": "1.0.0",
|
"express": "^4.17.1",
|
||||||
"express": "^4.17.1"
|
"reflect-metadata": "^0.1.13"
|
||||||
},
|
},
|
||||||
"lint-staged": {
|
"lint-staged": {
|
||||||
"**/*": "prettier --write --ignore-unknown"
|
"**/*": "prettier --write --ignore-unknown"
|
||||||
|
@ -1 +0,0 @@
|
|||||||
Subproject commit 558626d0e4d955d0b702365b507d2697ef83196d
|
|
@ -1 +1 @@
|
|||||||
Subproject commit d011f4547ebe4f1b36dc12fbb06b626e7345dc65
|
Subproject commit c42964407eaa35f0beea28926d234f74678b6cfc
|
28
src/app.ts
28
src/app.ts
@ -1,28 +1,8 @@
|
|||||||
// app config
|
// app config
|
||||||
import "./dotenv"
|
import "./dotenv"
|
||||||
|
|
||||||
import { createServer } from "http"
|
// reflection metadata for decorator
|
||||||
import { SERVER_PORT } from "./app.config"
|
import "reflect-metadata"
|
||||||
import { info } from "@cubetiq/ts-common/dist/log"
|
|
||||||
import application from "./server"
|
|
||||||
|
|
||||||
// get current host id
|
// core app
|
||||||
const hostId = `${require("os").hostname()}#${process.pid}`
|
import "./server"
|
||||||
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",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
54
src/application.ts
Normal file
54
src/application.ts
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
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()
|
4
src/constants/metadata.keys.ts
Normal file
4
src/constants/metadata.keys.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export enum MetadataKeys {
|
||||||
|
BASE_PATH = "base_path",
|
||||||
|
ROUTERS = "routers",
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import { Request, Response } from "express"
|
import { Request, Response } from "express"
|
||||||
import { Controller, Get } from "@cubetiq/express-server/dist/decorators"
|
import { Controller, Get } from "../decorators"
|
||||||
|
|
||||||
@Controller("/hello")
|
@Controller("/hello")
|
||||||
export default class IndexController {
|
export default class IndexController {
|
||||||
|
@ -1,10 +1,5 @@
|
|||||||
import { Request, Response } from "express"
|
import { Request, Response } from "express"
|
||||||
import {
|
import { Controller, Delete, Get, Post } from "../decorators"
|
||||||
Controller,
|
|
||||||
Delete,
|
|
||||||
Get,
|
|
||||||
Post,
|
|
||||||
} from "@cubetiq/express-server/dist/decorators"
|
|
||||||
|
|
||||||
const persons: Array<any> = [
|
const persons: Array<any> = [
|
||||||
{
|
{
|
||||||
|
7
src/decorators/controller.decorator.ts
Normal file
7
src/decorators/controller.decorator.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { MetadataKeys } from "../constants/metadata.keys"
|
||||||
|
|
||||||
|
export const Controller = (basePath: string): ClassDecorator => {
|
||||||
|
return (target: any) => {
|
||||||
|
Reflect.defineMetadata(MetadataKeys.BASE_PATH, basePath, target)
|
||||||
|
}
|
||||||
|
}
|
47
src/decorators/handlers.decorator.ts
Normal file
47
src/decorators/handlers.decorator.ts
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
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)
|
2
src/decorators/index.ts
Normal file
2
src/decorators/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export * from "./controller.decorator"
|
||||||
|
export * from "./handlers.decorator"
|
@ -1,9 +1,25 @@
|
|||||||
import Application from "@cubetiq/express-server/dist"
|
import { createServer } from "http"
|
||||||
import { controllers as AllControllers } from "./controller"
|
import { SERVER_PORT } from "./app.config"
|
||||||
class Server extends Application {
|
import { info } from "@cubetiq/ts-common/dist/log"
|
||||||
get controllers(): any[] {
|
import application from "./application"
|
||||||
return AllControllers
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default new 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",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user