refactor: rename package
This commit is contained in:
63
packages/core/src/function/handle.ts
Normal file
63
packages/core/src/function/handle.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import {
|
||||
controller, response, requestBody, httpGet, httpPost, queryParam, requestParam
|
||||
} from 'inversify-express-utils';
|
||||
import { inject, postConstruct } from 'inversify';
|
||||
import { Vaild, NotBlank, NotNull } 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';
|
||||
//process.env.FAAS_MONGO_DB = "faas";
|
||||
|
||||
const TABLE = 'users'
|
||||
|
||||
class ExampleModel {
|
||||
_id: string;
|
||||
@NotBlank("username must not be blank!")
|
||||
username: string;
|
||||
password: string;
|
||||
@NotNull()
|
||||
age: number;
|
||||
email: string;
|
||||
}
|
||||
|
||||
type Model = ExampleModel
|
||||
|
||||
@controller('')
|
||||
export class Controller {
|
||||
@inject(DBClient)
|
||||
private client: DBClient
|
||||
|
||||
@postConstruct()
|
||||
private init(): void {
|
||||
this.client.setTable(TABLE);
|
||||
}
|
||||
|
||||
@httpGet('/')
|
||||
public async list(): Promise<Model[]> {
|
||||
return this.client.find({});
|
||||
}
|
||||
|
||||
@httpGet('/:id')
|
||||
public async get(
|
||||
@requestParam('id') id: string
|
||||
): Promise<Model> {
|
||||
return this.client.findOneById(id);
|
||||
}
|
||||
|
||||
@httpPost('/')
|
||||
public async create(
|
||||
@Vaild() @requestBody() model: ExampleModel
|
||||
): Promise<ExampleModel> {
|
||||
return model;
|
||||
//return this.client.insertOne(model);
|
||||
}
|
||||
|
||||
@httpPost('/:id')
|
||||
public async update(
|
||||
@requestParam('id') id: string,
|
||||
@requestBody() model: Model
|
||||
): Promise<boolean> {
|
||||
return this.client.updateById(id, model);
|
||||
}
|
||||
}
|
||||
48
packages/core/src/index.ts
Normal file
48
packages/core/src/index.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
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 * as prettyjson from "prettyjson";
|
||||
import { Container } from 'inversify';
|
||||
|
||||
export class CcServerBoot {
|
||||
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(this._container);
|
||||
this._server.setConfig((app) => {
|
||||
app.use(bodyParser.urlencoded({
|
||||
extended: true
|
||||
}));
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.raw());
|
||||
});
|
||||
}
|
||||
|
||||
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(this._container);
|
||||
return this._serverInstance;
|
||||
}
|
||||
|
||||
public start(port: number = 80) {
|
||||
const routeInfo = getRouteInfo(this._container);
|
||||
console.log(prettyjson.render({ routes: routeInfo }));
|
||||
this._serverInstance.listen(port);
|
||||
console.log(`Server started on port ${port} :)`);
|
||||
}
|
||||
}
|
||||
8
packages/core/src/server.ts
Normal file
8
packages/core/src/server.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { CcServerBoot } from './index'
|
||||
|
||||
import './function/handle';
|
||||
|
||||
let server = new CcServerBoot();
|
||||
|
||||
server.build();
|
||||
server.start();
|
||||
Reference in New Issue
Block a user