Compare commits

..

7 Commits

Author SHA1 Message Date
c999b4a8f6 Fixed dockerfile build missing yarn
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-16 18:25:35 +07:00
7a2c3dec97 Fixed lerna build
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-16 18:25:11 +07:00
9ef8db077f Forgot yarn command
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-16 18:20:52 +07:00
da0235c0b3 Fixed drone ci yarn command failed
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-16 18:20:09 +07:00
0378f270fc Updated readme
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-16 18:18:13 +07:00
03458fe00c Add express server from module and updated the server and config
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-16 18:14:49 +07:00
c41002a96b Add signed fringerprint for contr 2021-09-16 10:07:36 +07:00
16 changed files with 55 additions and 159 deletions

View File

@@ -11,6 +11,4 @@ steps:
image: d.ctdn.net/node image: d.ctdn.net/node
commands: commands:
- yarn config set registry https://r.ctdn.net - yarn config set registry https://r.ctdn.net
- cd packages/ts-common && yarn && yarn build - yarn && yarn build
- cd ../../ && yarn
- yarn build

3
.gitmodules vendored
View File

@@ -1,3 +1,6 @@
[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

View File

@@ -14,9 +14,7 @@ WORKDIR /app
COPY . /app COPY . /app
RUN yarn config set registry https://r.ctdn.net RUN yarn config set registry https://r.ctdn.net
RUN cd packages/ts-common && yarn && yarn build RUN yarn && yarn build
RUN cd ../../ && yarn
RUN yarn build
EXPOSE 3000 EXPOSE 3000
CMD [ "yarn" , "serve"] CMD [ "yarn" , "serve"]

View File

@@ -27,13 +27,13 @@ git submodule update --init --remote
- Add Packages - Add Packages
```shell ```shell
mkdir packages && git submodule add https://git.cubetiqs.com/CUBETIQ/ts-common.git packages/ts-common mkdir packages && git submodule add https://git.cubetiqs.com/CUBETIQ/ts-common.git packages/ts-common && git submodule add https://git.cubetiqs.com/CUBETIQ/cubetiq-express-server.git packages/cubetiq-express-server
``` ```
- Install - Install and Build Packages
```shell ```shell
yarn yarn && npx lerna run build
``` ```
- Start - Start
@@ -136,4 +136,4 @@ export const controllers = [IndexController, PersonController, HomeController]
### Contributors ### Contributors
- Sambo Chea <sombochea@cubetiqs.com> - Sambo Chea <sombochea@cubetiqs.com> [4099C19544D3FF79]

View File

@@ -6,7 +6,7 @@
"private": true, "private": true,
"scripts": { "scripts": {
"start": "ts-node-dev --respawn --transpile-only src/app.ts", "start": "ts-node-dev --respawn --transpile-only src/app.ts",
"build": "rm -rf dist && tsc", "build": "lerna run build && rm -rf dist && tsc",
"serve": "yarn build && node dist/app.js", "serve": "yarn build && node dist/app.js",
"bootstrap": "lerna bootstrap", "bootstrap": "lerna bootstrap",
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
@@ -18,7 +18,8 @@
}, },
"keywords": [ "keywords": [
"Express", "Express",
"Nodejs" "Nodejs",
"Typescript"
], ],
"author": "Sambo Chea <sombochea@cubetiqs.com>", "author": "Sambo Chea <sombochea@cubetiqs.com>",
"license": "ISC", "license": "ISC",
@@ -41,8 +42,8 @@
}, },
"dependencies": { "dependencies": {
"@cubetiq/ts-common": "1.0.0", "@cubetiq/ts-common": "1.0.0",
"express": "^4.17.1", "@cubetiq/express-server": "1.0.0",
"reflect-metadata": "^0.1.13" "express": "^4.17.1"
}, },
"lint-staged": { "lint-staged": {
"**/*": "prettier --write --ignore-unknown" "**/*": "prettier --write --ignore-unknown"

View File

@@ -1,8 +1,28 @@
// app config // app config
import "./dotenv" import "./dotenv"
// reflection metadata for decorator import { createServer } from "http"
import "reflect-metadata" import { SERVER_PORT } from "./app.config"
import { info } from "@cubetiq/ts-common/dist/log"
import application from "./server"
// core app // get current host id
import "./server" 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",
})
})

View File

@@ -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()

View File

@@ -1,4 +0,0 @@
export enum MetadataKeys {
BASE_PATH = "base_path",
ROUTERS = "routers",
}

View File

@@ -1,5 +1,5 @@
import { Request, Response } from "express" import { Request, Response } from "express"
import { Controller, Get } from "../decorators" import { Controller, Get } from "@cubetiq/express-server/dist/decorators"
@Controller("/hello") @Controller("/hello")
export default class IndexController { export default class IndexController {

View File

@@ -1,5 +1,10 @@
import { Request, Response } from "express" 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<any> = [ const persons: Array<any> = [
{ {

View File

@@ -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)
}
}

View File

@@ -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)

View File

@@ -1,2 +0,0 @@
export * from "./controller.decorator"
export * from "./handlers.decorator"

View File

@@ -1,25 +1,9 @@
import { createServer } from "http" import Application from "@cubetiq/express-server/dist"
import { SERVER_PORT } from "./app.config" import { controllers as AllControllers } from "./controller"
import { info } from "@cubetiq/ts-common/dist/log" class Server extends Application {
import application from "./application" get controllers(): any[] {
return AllControllers
}
}
// get current host id export default new Server()
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",
})
})