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 /dist
/package-lock.json /package-lock.json
/yarn.lock /yarn.lock
/*.log

View File

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

View File

@ -10,7 +10,7 @@
"registry": "https://repo.yumc.pw/repository/npm-hosted/" "registry": "https://repo.yumc.pw/repository/npm-hosted/"
}, },
"scripts": { "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", "watch": "npx tsc --watch",
"build": "rimraf dist && npx tsc", "build": "rimraf dist && npx tsc",
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"

View File

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