Init Project...
This commit is contained in:
commit
1746ffa773
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/node_modules
|
||||||
|
/dist
|
||||||
|
/package-lock.json
|
||||||
|
/yarn.lock
|
8
lerna.json
Normal file
8
lerna.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"npmClient": "yarn",
|
||||||
|
"useWorkspaces": true,
|
||||||
|
"packages": [
|
||||||
|
"packages/*"
|
||||||
|
],
|
||||||
|
"version": "0.0.1"
|
||||||
|
}
|
14
package.json
Normal file
14
package.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"private": true,
|
||||||
|
"name": "cc-server-parent",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "index.js",
|
||||||
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
|
"license": "MIT",
|
||||||
|
"workspaces": [
|
||||||
|
"packages/*"
|
||||||
|
],
|
||||||
|
"devDependencies": {
|
||||||
|
"lerna": "^3.14.1"
|
||||||
|
}
|
||||||
|
}
|
4
packages/cc-server-core/.gitignore
vendored
Normal file
4
packages/cc-server-core/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/node_modules
|
||||||
|
/dist
|
||||||
|
/package-lock.json
|
||||||
|
/yarn.lock
|
11
packages/cc-server-core/README.md
Normal file
11
packages/cc-server-core/README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# `cc-server-core`
|
||||||
|
|
||||||
|
> TODO: description
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
const ccServerCore = require('cc-server-core');
|
||||||
|
|
||||||
|
// TODO: DEMONSTRATE API
|
||||||
|
```
|
29
packages/cc-server-core/package.json
Normal file
29
packages/cc-server-core/package.json
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"name": "cc-server-core",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "> TODO: description",
|
||||||
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
|
"homepage": "https://faas.yumc.pw",
|
||||||
|
"license": "ISC",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"publishConfig": {
|
||||||
|
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"dev": "npx ts-node src/index.ts",
|
||||||
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"body-parser": "^1.19.0",
|
||||||
|
"cc-server-db-mongo": "^0.0.1",
|
||||||
|
"cc-server-ioc": "^0.0.1",
|
||||||
|
"inversify": "^5.0.1",
|
||||||
|
"inversify-express-utils": "^6.3.2",
|
||||||
|
"reflect-metadata": "^0.1.13",
|
||||||
|
"typescript": "^3.5.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/body-parser": "^1.17.0",
|
||||||
|
"ts-node": "^8.2.0"
|
||||||
|
}
|
||||||
|
}
|
22
packages/cc-server-core/src/function/handle.ts
Normal file
22
packages/cc-server-core/src/function/handle.ts
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import {
|
||||||
|
controller, httpGet, httpPost, httpPut, httpDelete
|
||||||
|
} from 'inversify-express-utils';
|
||||||
|
import { inject } from 'inversify';
|
||||||
|
import { MongoDBClient } from 'cc-server-db-mongo'
|
||||||
|
|
||||||
|
@controller('/')
|
||||||
|
export class UserController {
|
||||||
|
constructor(
|
||||||
|
@inject(MongoDBClient) private MongoDBClient: MongoDBClient
|
||||||
|
) { }
|
||||||
|
|
||||||
|
@httpGet('/')
|
||||||
|
public async getUsers(): Promise<any[]> {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
@httpPost('/')
|
||||||
|
public async newUser(): Promise<any> {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
}
|
22
packages/cc-server-core/src/function/tsconfig.json
Normal file
22
packages/cc-server-core/src/function/tsconfig.json
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "dist",
|
||||||
|
"sourceRoot": "src",
|
||||||
|
"module": "commonjs",
|
||||||
|
"target": "es5",
|
||||||
|
"lib": [
|
||||||
|
"es6",
|
||||||
|
"dom"
|
||||||
|
],
|
||||||
|
"noImplicitAny": false,
|
||||||
|
"sourceMap": false,
|
||||||
|
"emitDecoratorMetadata": true,
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"noUnusedLocals": true
|
||||||
|
},
|
||||||
|
"compileOnSave": false,
|
||||||
|
"exclude": [
|
||||||
|
"node_modules"
|
||||||
|
]
|
||||||
|
}
|
32
packages/cc-server-core/src/index.ts
Normal file
32
packages/cc-server-core/src/index.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import 'reflect-metadata';
|
||||||
|
import { InversifyExpressServer } from 'inversify-express-utils';
|
||||||
|
import { Container } from 'inversify';
|
||||||
|
import * as bodyParser from 'body-parser';
|
||||||
|
import './function/handle';
|
||||||
|
import { METADATA_KEY } from 'cc-server-ioc';
|
||||||
|
|
||||||
|
// load everything needed to the Container
|
||||||
|
let container = new Container();
|
||||||
|
Reflect.defineMetadata(METADATA_KEY.container, container, Reflect)
|
||||||
|
|
||||||
|
// auto load service
|
||||||
|
let services: Function[] = Reflect.getMetadata(METADATA_KEY.service, Reflect);
|
||||||
|
for (const service of services) {
|
||||||
|
service()
|
||||||
|
}
|
||||||
|
|
||||||
|
// start the server
|
||||||
|
let server = new InversifyExpressServer(container);
|
||||||
|
|
||||||
|
server.setConfig((app) => {
|
||||||
|
app.use(bodyParser.urlencoded({
|
||||||
|
extended: true
|
||||||
|
}));
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
app.use(bodyParser.raw())
|
||||||
|
});
|
||||||
|
|
||||||
|
let serverInstance = server.build();
|
||||||
|
serverInstance.listen(80);
|
||||||
|
|
||||||
|
console.log('Server started on port 80 :)');
|
7
packages/cc-server-core/tsconfig.json
Normal file
7
packages/cc-server-core/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"baseUrl": "src",
|
||||||
|
"outDir": "dist"
|
||||||
|
}
|
||||||
|
}
|
4
packages/cc-server-db-mongo/.gitignore
vendored
Normal file
4
packages/cc-server-db-mongo/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/node_modules
|
||||||
|
/dist
|
||||||
|
/package-lock.json
|
||||||
|
/yarn.lock
|
11
packages/cc-server-db-mongo/README.md
Normal file
11
packages/cc-server-db-mongo/README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# `cc-server-db-mongo`
|
||||||
|
|
||||||
|
> TODO: description
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
const ccServerDbMongo = require('cc-server-db-mongo');
|
||||||
|
|
||||||
|
// TODO: DEMONSTRATE API
|
||||||
|
```
|
24
packages/cc-server-db-mongo/package.json
Normal file
24
packages/cc-server-db-mongo/package.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"name": "cc-server-db-mongo",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "> TODO: description",
|
||||||
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
|
"homepage": "https://faas.yumc.pw",
|
||||||
|
"license": "ISC",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"publishConfig": {
|
||||||
|
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"cc-server-ioc": "^0.0.1",
|
||||||
|
"inversify": "^5.0.1",
|
||||||
|
"mongodb": "^3.2.7",
|
||||||
|
"reflect-metadata": "^0.1.13"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/mongodb": "^3.1.28"
|
||||||
|
}
|
||||||
|
}
|
57
packages/cc-server-db-mongo/src/client.ts
Normal file
57
packages/cc-server-db-mongo/src/client.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import { Db, ObjectID, UpdateWriteOpResult } from 'mongodb';
|
||||||
|
import { injectable } from 'inversify';
|
||||||
|
import { MongoDBConnection } from './connection';
|
||||||
|
import { service } from 'cc-server-ioc'
|
||||||
|
|
||||||
|
interface DBClient<T = any> {
|
||||||
|
find(table: string, where: object): T
|
||||||
|
}
|
||||||
|
|
||||||
|
export const NAME: string = 'MongoDBClient'
|
||||||
|
|
||||||
|
@service()
|
||||||
|
@injectable()
|
||||||
|
export class MongoDBClient<T = any> implements DBClient {
|
||||||
|
public db: Db;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
MongoDBConnection.getConnection((connection) => {
|
||||||
|
this.db = connection;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public find(collection: string, filter: object): Promise<T[]> {
|
||||||
|
return this.db.collection(collection).find(filter).toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async findOne(collection: string, filter: Object): Promise<T> {
|
||||||
|
let result = await this.db.collection(collection).find(filter).limit(1).toArray();
|
||||||
|
return result[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public async findOneById(collection: string, objectId: string): Promise<T> {
|
||||||
|
return this.findOne(collection, { _id: new ObjectID(objectId) })
|
||||||
|
}
|
||||||
|
|
||||||
|
public async insertOne(collection: string, model: T): Promise<T> {
|
||||||
|
var insert = await this.db.collection(collection).insertOne(model);
|
||||||
|
return insert.ops[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateOne(collection: string, where: any, model: any): Promise<UpdateWriteOpResult> {
|
||||||
|
return this.db.collection(collection).updateOne(where, { $set: model });
|
||||||
|
}
|
||||||
|
|
||||||
|
public updateById(collection: string, objectId: string, model: any): Promise<UpdateWriteOpResult> {
|
||||||
|
return this.updateOne(collection, { _id: new ObjectID(objectId) }, { $set: model })
|
||||||
|
}
|
||||||
|
|
||||||
|
public async deleteOne(collection: string, where: any): Promise<boolean> {
|
||||||
|
let result = await this.db.collection(collection).deleteOne(where);
|
||||||
|
return result.result.ok === 1 && result.result.n > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
public async deleteById(collection: string, objectId: string): Promise<boolean> {
|
||||||
|
return this.deleteOne(collection, { _id: new ObjectID(objectId) });
|
||||||
|
}
|
||||||
|
}
|
27
packages/cc-server-db-mongo/src/connection.ts
Normal file
27
packages/cc-server-db-mongo/src/connection.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { Db, MongoClient } from 'mongodb';
|
||||||
|
|
||||||
|
const connStr = process.env.MONGO_URL || 'mongodb://192.168.0.2:27017';
|
||||||
|
const dbName = process.env.MONGO_DB || "frppool";
|
||||||
|
|
||||||
|
export class MongoDBConnection {
|
||||||
|
private static isConnected: boolean = false;
|
||||||
|
private static db: Db;
|
||||||
|
|
||||||
|
public static getConnection(result: (connection) => void) {
|
||||||
|
if (this.isConnected) {
|
||||||
|
return result(this.db);
|
||||||
|
} else {
|
||||||
|
this.connect((error, db: Db) => {
|
||||||
|
return result(this.db);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static connect(result: (error, db: Db) => void) {
|
||||||
|
MongoClient.connect(connStr, { useNewUrlParser: true }, (err, client) => {
|
||||||
|
this.db = client.db(dbName);
|
||||||
|
this.isConnected = true;
|
||||||
|
return result(err, this.db);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
1
packages/cc-server-db-mongo/src/index.ts
Normal file
1
packages/cc-server-db-mongo/src/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './client'
|
7
packages/cc-server-db-mongo/tsconfig.json
Normal file
7
packages/cc-server-db-mongo/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"baseUrl": "src",
|
||||||
|
"outDir": "dist"
|
||||||
|
}
|
||||||
|
}
|
4
packages/cc-server-ioc/.gitignore
vendored
Normal file
4
packages/cc-server-ioc/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
/node_modules
|
||||||
|
/dist
|
||||||
|
/package-lock.json
|
||||||
|
/yarn.lock
|
11
packages/cc-server-ioc/README.md
Normal file
11
packages/cc-server-ioc/README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# `cc-server-ioc`
|
||||||
|
|
||||||
|
> TODO: description
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
const ccServerIoc = require('cc-server-ioc');
|
||||||
|
|
||||||
|
// TODO: DEMONSTRATE API
|
||||||
|
```
|
20
packages/cc-server-ioc/package.json
Normal file
20
packages/cc-server-ioc/package.json
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "cc-server-ioc",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "> TODO: description",
|
||||||
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
|
"homepage": "https://faas.yumc.pw",
|
||||||
|
"license": "ISC",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"publishConfig": {
|
||||||
|
"registry": "https://repo.yumc.pw/repository/npm-hosted/"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: run tests from root\" && exit 1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"inversify": "^5.0.1",
|
||||||
|
"reflect-metadata": "^0.1.13"
|
||||||
|
},
|
||||||
|
"devDependencies": {}
|
||||||
|
}
|
4
packages/cc-server-ioc/src/constant/types.ts
Normal file
4
packages/cc-server-ioc/src/constant/types.ts
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
export const METADATA_KEY = {
|
||||||
|
container: "cc-server-core:container",
|
||||||
|
service: "cc-server-core:service"
|
||||||
|
};
|
16
packages/cc-server-ioc/src/decorators.ts
Normal file
16
packages/cc-server-ioc/src/decorators.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import 'reflect-metadata'
|
||||||
|
import { Container } from 'inversify';
|
||||||
|
import { METADATA_KEY } from './constant/types';
|
||||||
|
|
||||||
|
function service(name: string | symbol) {
|
||||||
|
return function(target: any) {
|
||||||
|
let services = Reflect.getMetadata(METADATA_KEY.service, Reflect) || []
|
||||||
|
services = [() => {
|
||||||
|
let container: Container = Reflect.getMetadata(METADATA_KEY.container, Reflect)
|
||||||
|
container.bind(name || target).to(target);
|
||||||
|
}, ...services]
|
||||||
|
Reflect.defineMetadata(METADATA_KEY.service, services, Reflect)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { service }
|
2
packages/cc-server-ioc/src/index.ts
Normal file
2
packages/cc-server-ioc/src/index.ts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
export * from './constant/types'
|
||||||
|
export * from './decorators'
|
7
packages/cc-server-ioc/tsconfig.json
Normal file
7
packages/cc-server-ioc/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"baseUrl": "src",
|
||||||
|
"outDir": "dist"
|
||||||
|
}
|
||||||
|
}
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es6",
|
||||||
|
"module": "commonjs",
|
||||||
|
"sourceMap": true,
|
||||||
|
"allowUnreachableCode": true,
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"emitDecoratorMetadata": true
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user