feat: add lazy inject and websocket debug
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
21
packages/core/cc-server.http
Normal file
21
packages/core/cc-server.http
Normal file
@@ -0,0 +1,21 @@
|
||||
@url=https://faas.n.yumc.pw
|
||||
|
||||
###
|
||||
GET {{url}}/example
|
||||
|
||||
###
|
||||
POST {{url}}/example
|
||||
|
||||
{
|
||||
"username": 1
|
||||
}
|
||||
|
||||
###
|
||||
@websocket={{url}}/websocket
|
||||
|
||||
###
|
||||
POST {{websocket}}
|
||||
|
||||
{
|
||||
"name":"Socket.IO"
|
||||
}
|
||||
@@ -25,6 +25,7 @@
|
||||
"@cc-server/ioc": "^0.4.0",
|
||||
"@cc-server/ws": "^0.4.0",
|
||||
"body-parser": "^1.19.0",
|
||||
"globby": "^9.2.0",
|
||||
"inversify": "^5.0.1",
|
||||
"inversify-express-utils": "^6.3.2",
|
||||
"prettyjson": "^1.2.1",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { TYPE, io } from '@cc-server/ws'
|
||||
import { DBClient } from '@cc-server/db'
|
||||
import { inject, postConstruct } from '@cc-server/ioc';
|
||||
import { Vaild, NotBlank, NotNull, controller, requestBody, httpGet, httpPost, requestParam } from '@cc-server/binding'
|
||||
import { DBClient } from '@cc-server/db'
|
||||
import '@cc-server/db-mongo'
|
||||
|
||||
//process.env.FAAS_MONGO_URL = 'mongodb://192.168.0.2:27017';
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { namespace, listener, interfaces, io } from '@cc-server/ws'
|
||||
import { controller, httpPost, requestBody } from '@cc-server/binding';
|
||||
import { namespace, listener, interfaces, io, TYPE } from '@cc-server/ws'
|
||||
import { lazyInjectNamed } from '@cc-server/ioc'
|
||||
|
||||
@namespace('/', (socket: io.Socket, next: (err?: any) => void) => {
|
||||
console.log(socket.nsp.name, socket.id, 'before connection');
|
||||
@@ -31,3 +33,17 @@ export class Namespace extends interfaces.Namespace {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
@controller('/websocket')
|
||||
export class WebSocketController {
|
||||
@lazyInjectNamed(TYPE.Namespace, Namespace.name)
|
||||
private root: Namespace;
|
||||
|
||||
@httpPost('/')
|
||||
public async create(
|
||||
@requestBody() model: Object
|
||||
): Promise<Object> {
|
||||
this.root.nsp.send(JSON.stringify(model));
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +1,82 @@
|
||||
import 'reflect-metadata';
|
||||
import * as http from 'http'
|
||||
import * as fs from 'fs';
|
||||
import * as http from 'http';
|
||||
import * as globby from "globby";
|
||||
import * as express from "express";
|
||||
import * as prettyjson from "prettyjson";
|
||||
import * as bodyParser from 'body-parser';
|
||||
import { buildWebSocket, io } from '@cc-server/ws'
|
||||
import { buildProviderModule, Container } from '@cc-server/ioc';
|
||||
import { InversifyExpressServer, interfaces, getRouteInfo, rebuildServer } from '@cc-server/binding'
|
||||
import { buildWebSocket, io, getNamespaceInfo } from '@cc-server/ws'
|
||||
import { buildProviderModule, Container, initContainer, getContainer } from '@cc-server/ioc';
|
||||
import { InversifyExpressServer, getRouteInfo, rebuildServer, controller, httpGet } from '@cc-server/binding';
|
||||
|
||||
export { io, http, express }
|
||||
export { http, express }
|
||||
|
||||
@controller('/actuator')
|
||||
class Actuator {
|
||||
@httpGet('/mappings')
|
||||
private mappings() {
|
||||
return {
|
||||
http: getRouteInfo(getContainer()),
|
||||
websocket: getNamespaceInfo()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class CcServerBoot {
|
||||
private _container: Container;
|
||||
private _server: http.Server;
|
||||
private _serverInstance: express.Application;
|
||||
private _serverInversify: InversifyExpressServer;
|
||||
private _wsServer: io.Server;
|
||||
private _serverWebsocket: io.Server;
|
||||
private _beforeUse: express.RequestHandler[] = [];
|
||||
private _afterUse: express.ErrorRequestHandler[] = [];
|
||||
|
||||
constructor(container?: Container) {
|
||||
this._container = container || new Container();
|
||||
this._serverInstance = express();
|
||||
this._server = http.createServer(this._serverInstance);
|
||||
// start the server
|
||||
this._serverInversify = new InversifyExpressServer(this._container, null, null, this._serverInstance);
|
||||
this._serverInversify.setConfig((app) => {
|
||||
app.use(bodyParser.urlencoded({
|
||||
extended: true
|
||||
}));
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.raw());
|
||||
});
|
||||
this._wsServer = io(this._server, {
|
||||
this._container = container || this.$createContainer();
|
||||
this._serverInstance = this.$createExpressServer();
|
||||
this._server = this.$createServer();
|
||||
this._serverInversify = this.$createInversifyServer();
|
||||
this._serverWebsocket = this.$createWebsocketServer();
|
||||
initContainer(this._container);
|
||||
}
|
||||
|
||||
protected $createContainer(): Container {
|
||||
return new Container();
|
||||
}
|
||||
|
||||
protected $createServer(): http.Server {
|
||||
return http.createServer(this._serverInstance);
|
||||
}
|
||||
|
||||
protected $createExpressServer(): express.Application {
|
||||
return express();
|
||||
}
|
||||
|
||||
protected $createInversifyServer(): InversifyExpressServer {
|
||||
return new InversifyExpressServer(this._container, null, null, this._serverInstance);
|
||||
}
|
||||
|
||||
protected $createWebsocketServer(): io.Server {
|
||||
return io(this._server, {
|
||||
path: '/ws',
|
||||
serveClient: false,
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
protected use(middleware: express.RequestHandler) {
|
||||
this._beforeUse.push(middleware)
|
||||
return this;
|
||||
}
|
||||
|
||||
protected error(middleware: express.ErrorRequestHandler) {
|
||||
this._afterUse.push(middleware)
|
||||
return this;
|
||||
}
|
||||
|
||||
protected $onMountingMiddlewares() {
|
||||
this.use(bodyParser.urlencoded({ extended: true }))
|
||||
.use(bodyParser.json())
|
||||
.use(bodyParser.raw());
|
||||
}
|
||||
|
||||
get server() {
|
||||
@@ -48,23 +92,34 @@ export class CcServerBoot {
|
||||
}
|
||||
|
||||
get websocket() {
|
||||
return this._wsServer;
|
||||
return this._serverWebsocket;
|
||||
}
|
||||
|
||||
public setConfig(fn: interfaces.ConfigFunction) {
|
||||
this._serverInversify.setConfig(fn)
|
||||
public static(root: string = 'public') {
|
||||
this.express.use(express.static(root));
|
||||
return this;
|
||||
}
|
||||
|
||||
public setErrorConfig(fn: interfaces.ConfigFunction) {
|
||||
this._serverInversify.setErrorConfig(fn)
|
||||
public scan(path: fs.PathLike) {
|
||||
let files = fs.readdirSync(path);
|
||||
for (const file of files) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public build() {
|
||||
this.$onMountingMiddlewares();
|
||||
this._serverInversify.setConfig((app) => {
|
||||
this._beforeUse.every(m => app.use(m))
|
||||
})
|
||||
this._serverInversify.setErrorConfig((app) => {
|
||||
this._afterUse.every(m => app.use(m))
|
||||
})
|
||||
this._container.load(buildProviderModule());
|
||||
this._serverInstance = this._serverInversify.build();
|
||||
rebuildServer(this._container);
|
||||
buildWebSocket(this._container, this._wsServer);
|
||||
return this._serverInstance;
|
||||
buildWebSocket(this._container, this._serverWebsocket);
|
||||
return this;
|
||||
}
|
||||
|
||||
public start(port: number = 80) {
|
||||
|
||||
@@ -3,9 +3,4 @@ import { CcServerBoot, express } from './index'
|
||||
import './function/http';
|
||||
import './function/websocket';
|
||||
|
||||
let server = new CcServerBoot();
|
||||
|
||||
server.express.use(express.static('public'));
|
||||
|
||||
server.build();
|
||||
server.start();
|
||||
new CcServerBoot().static('public').build().start();
|
||||
|
||||
Reference in New Issue
Block a user