refactor: rename package
This commit is contained in:
4
packages/core/.gitignore
vendored
Normal file
4
packages/core/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
/node_modules
|
||||
/dist
|
||||
/package-lock.json
|
||||
/yarn.lock
|
||||
22
packages/core/.npmignore
Normal file
22
packages/core/.npmignore
Normal file
@@ -0,0 +1,22 @@
|
||||
src
|
||||
test
|
||||
typings
|
||||
bundled
|
||||
build
|
||||
coverage
|
||||
docs
|
||||
wiki
|
||||
gulpfile.js
|
||||
bower.json
|
||||
karma.conf.js
|
||||
tsconfig.json
|
||||
typings.json
|
||||
CONTRIBUTING.md
|
||||
ISSUE_TEMPLATE.md
|
||||
PULL_REQUEST_TEMPLATE.md
|
||||
tslint.json
|
||||
wallaby.js
|
||||
.travis.yml
|
||||
.gitignore
|
||||
.vscode
|
||||
type_definitions
|
||||
11
packages/core/README.md
Normal file
11
packages/core/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# `cc-server-core`
|
||||
|
||||
> TODO: description
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
const ccServerCore = require('cc-server-core');
|
||||
|
||||
// TODO: DEMONSTRATE API
|
||||
```
|
||||
43
packages/core/package.json
Normal file
43
packages/core/package.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "@cc-server/core",
|
||||
"version": "0.3.1",
|
||||
"description": "> TODO: description",
|
||||
"author": "MiaoWoo <admin@yumc.pw>",
|
||||
"homepage": "https://github.com/502647092/cc-server-parent#readme",
|
||||
"license": "ISC",
|
||||
"main": "dist/index.js",
|
||||
"publishConfig": {
|
||||
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||
},
|
||||
"scripts": {
|
||||
"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"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/502647092/cc-server-parent.git"
|
||||
},
|
||||
"dependencies": {
|
||||
"@cc-server/binding": "^0.3.1",
|
||||
"@cc-server/db-mongo": "^0.3.1",
|
||||
"@cc-server/ioc": "^0.3.1",
|
||||
"body-parser": "^1.19.0",
|
||||
"inversify": "^5.0.1",
|
||||
"inversify-express-utils": "^6.3.2",
|
||||
"prettyjson": "^1.2.1",
|
||||
"reflect-metadata": "^0.1.13"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/body-parser": "^1.17.0",
|
||||
"@types/express": "^4.17.0",
|
||||
"@types/prettyjson": "^0.0.29",
|
||||
"mocha": "^6.1.4",
|
||||
"rimraf": "^2.6.3",
|
||||
"ts-node": "^8.2.0",
|
||||
"ts-node-dev": "^1.0.0-pre.40",
|
||||
"typescript": "^3.5.1"
|
||||
},
|
||||
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
|
||||
}
|
||||
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();
|
||||
7
packages/core/tsconfig.json
Normal file
7
packages/core/tsconfig.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"baseUrl": "src",
|
||||
"outDir": "dist"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user