1
0
Fork 0

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

Signed-off-by: MiaoWoo <admin@yumc.pw>
master
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
this.server = new InversifyExpressServer(container);
this.server.setConfig((app) => {
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(bodyParser.raw());
});
}
container.load(buildProviderModule());
public setConfig(fn: interfaces.ConfigFunction) {
this.server.setConfig(fn)
}
// start the server
let server = new InversifyExpressServer(container);
public setErrorConfig(fn: interfaces.ConfigFunction) {
this.server.setErrorConfig(fn)
}
server.setConfig((app) => {
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(bodyParser.raw());
});
public build() {
this.serverInstance = this.server.build();
rebuildServer(container);
return this.serverInstance;
}
let serverInstance = server.build();
rebuildServer(container);
const routeInfo = getRouteInfo(container);
console.log(prettyjson.render({ routes: routeInfo }));
serverInstance.listen(80);
console.log('Server started on port 80 :)');
public start() {
const routeInfo = getRouteInfo(container);
console.log(prettyjson.render({ routes: routeInfo }));
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();