feat: refactor cc-server-core and fix binding registry

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-06-20 19:59:27 +08:00
parent fd6c0e74cb
commit 8bde967d81
5 changed files with 46 additions and 25 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@
/dist
/package-lock.json
/yarn.lock
/*.log

View File

@ -7,7 +7,7 @@
"license": "ISC",
"main": "dist/index.js",
"publishConfig": {
"registry": "https://repo.yumc.pw/repository/npm/"
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
},
"repository": {
"type": "git",

View File

@ -10,7 +10,7 @@
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
},
"scripts": {
"dev": "npx ts-node-dev --respawn --prefer-ts --debounce=1500 src/index.ts",
"dev": "npx ts-node-dev --respawn --prefer-ts --debounce=1500 src/server.ts",
"watch": "npx tsc --watch",
"build": "rimraf dist && npx tsc",
"test": "echo \"Error: run tests from root\" && exit 1"

View File

@ -1,33 +1,45 @@
import 'reflect-metadata';
import { InversifyExpressServer, getRouteInfo } from 'inversify-express-utils';
import * as express from "express";
import { InversifyExpressServer, interfaces, getRouteInfo } from 'inversify-express-utils';
import * as bodyParser from 'body-parser';
import { rebuildServer } from 'cc-server-binding'
import { container, buildProviderModule } from 'cc-server-ioc';
import * as prettyjson from "prettyjson";
import './function/handle';
export class CcServerBoot {
private server: InversifyExpressServer;
private serverInstance: express.Application;
constructor() {
container.load(buildProviderModule());
// start the server
let server = new InversifyExpressServer(container);
server.setConfig((app) => {
this.server = new InversifyExpressServer(container);
this.server.setConfig((app) => {
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(bodyParser.raw());
});
}
let serverInstance = server.build();
public setConfig(fn: interfaces.ConfigFunction) {
this.server.setConfig(fn)
}
public setErrorConfig(fn: interfaces.ConfigFunction) {
this.server.setErrorConfig(fn)
}
public build() {
this.serverInstance = this.server.build();
rebuildServer(container);
return this.serverInstance;
}
public start() {
const routeInfo = getRouteInfo(container);
console.log(prettyjson.render({ routes: routeInfo }));
serverInstance.listen(80);
this.serverInstance.listen(80);
console.log('Server started on port 80 :)');
}
}

View File

@ -0,0 +1,8 @@
import { CcServerBoot } from './index'
import './function/handle';
let server = new CcServerBoot();
server.build();
server.start();