refactor: optimize framework

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2019-06-21 12:30:30 +08:00
parent b6443f805d
commit dc46ab904a
7 changed files with 57 additions and 55 deletions

View File

@@ -13,7 +13,7 @@ const TABLE = 'users'
class ExampleModel {
_id: string;
@NotBlank("用户名不得为空!")
@NotBlank("username must not be blank!")
username: string;
password: string;
@NotNull()
@@ -49,8 +49,8 @@ export class Controller {
public async create(
@Vaild() @requestBody() model: ExampleModel
): Promise<ExampleModel> {
return model;
//return this.client.insertOne(model);
return this.client.findOneById('5d0af7c039179a28de618cb8');
}
@httpPost('/:id')

View File

@@ -2,18 +2,21 @@ import 'reflect-metadata';
import * as express from "express";
import { InversifyExpressServer, interfaces, getRouteInfo } from 'inversify-express-utils';
import * as bodyParser from 'body-parser';
import { buildProviderModule } from 'cc-server-ioc';
import { rebuildServer } from 'cc-server-binding'
import { container, buildProviderModule } from 'cc-server-ioc';
import * as prettyjson from "prettyjson";
import { Container } from 'inversify';
export class CcServerBoot {
private server: InversifyExpressServer;
private serverInstance: express.Application;
constructor() {
container.load(buildProviderModule());
private _container: Container;
private _server: InversifyExpressServer;
private _serverInstance: express.Application;
constructor(container?: Container) {
this._container = container || new Container();
this._container.load(buildProviderModule());
// start the server
this.server = new InversifyExpressServer(container);
this.server.setConfig((app) => {
this._server = new InversifyExpressServer(this._container);
this._server.setConfig((app) => {
app.use(bodyParser.urlencoded({
extended: true
}));
@@ -23,23 +26,23 @@ export class CcServerBoot {
}
public setConfig(fn: interfaces.ConfigFunction) {
this.server.setConfig(fn)
this._server.setConfig(fn)
}
public setErrorConfig(fn: interfaces.ConfigFunction) {
this.server.setErrorConfig(fn)
this._server.setErrorConfig(fn)
}
public build() {
this.serverInstance = this.server.build();
rebuildServer(container);
return this.serverInstance;
this._serverInstance = this._server.build();
rebuildServer(this._container);
return this._serverInstance;
}
public start() {
const routeInfo = getRouteInfo(container);
public start(port: number = 80) {
const routeInfo = getRouteInfo(this._container);
console.log(prettyjson.render({ routes: routeInfo }));
this.serverInstance.listen(80);
console.log('Server started on port 80 :)');
this._serverInstance.listen(port);
console.log(`Server started on port ${port} :)`);
}
}