commit 1746ffa7735b15632e74e2609cc456966d2acb06 Author: MiaoWoo Date: Mon Jun 10 18:56:29 2019 +0800 Init Project... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d856d44 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/node_modules +/dist +/package-lock.json +/yarn.lock diff --git a/lerna.json b/lerna.json new file mode 100644 index 0000000..5da27fe --- /dev/null +++ b/lerna.json @@ -0,0 +1,8 @@ +{ + "npmClient": "yarn", + "useWorkspaces": true, + "packages": [ + "packages/*" + ], + "version": "0.0.1" +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..34850b0 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "private": true, + "name": "cc-server-parent", + "version": "1.0.0", + "main": "index.js", + "author": "MiaoWoo ", + "license": "MIT", + "workspaces": [ + "packages/*" + ], + "devDependencies": { + "lerna": "^3.14.1" + } +} \ No newline at end of file diff --git a/packages/cc-server-core/.gitignore b/packages/cc-server-core/.gitignore new file mode 100644 index 0000000..d856d44 --- /dev/null +++ b/packages/cc-server-core/.gitignore @@ -0,0 +1,4 @@ +/node_modules +/dist +/package-lock.json +/yarn.lock diff --git a/packages/cc-server-core/README.md b/packages/cc-server-core/README.md new file mode 100644 index 0000000..063e15f --- /dev/null +++ b/packages/cc-server-core/README.md @@ -0,0 +1,11 @@ +# `cc-server-core` + +> TODO: description + +## Usage + +``` +const ccServerCore = require('cc-server-core'); + +// TODO: DEMONSTRATE API +``` diff --git a/packages/cc-server-core/package.json b/packages/cc-server-core/package.json new file mode 100644 index 0000000..f16d222 --- /dev/null +++ b/packages/cc-server-core/package.json @@ -0,0 +1,29 @@ +{ + "name": "cc-server-core", + "version": "0.0.1", + "description": "> TODO: description", + "author": "MiaoWoo ", + "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" + } +} diff --git a/packages/cc-server-core/src/function/handle.ts b/packages/cc-server-core/src/function/handle.ts new file mode 100644 index 0000000..3487b51 --- /dev/null +++ b/packages/cc-server-core/src/function/handle.ts @@ -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 { + return [] + } + + @httpPost('/') + public async newUser(): Promise { + return {} + } +} diff --git a/packages/cc-server-core/src/function/tsconfig.json b/packages/cc-server-core/src/function/tsconfig.json new file mode 100644 index 0000000..b07591d --- /dev/null +++ b/packages/cc-server-core/src/function/tsconfig.json @@ -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" + ] +} \ No newline at end of file diff --git a/packages/cc-server-core/src/index.ts b/packages/cc-server-core/src/index.ts new file mode 100644 index 0000000..2678d96 --- /dev/null +++ b/packages/cc-server-core/src/index.ts @@ -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 :)'); diff --git a/packages/cc-server-core/tsconfig.json b/packages/cc-server-core/tsconfig.json new file mode 100644 index 0000000..7aae5d2 --- /dev/null +++ b/packages/cc-server-core/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": "src", + "outDir": "dist" + } +} \ No newline at end of file diff --git a/packages/cc-server-db-mongo/.gitignore b/packages/cc-server-db-mongo/.gitignore new file mode 100644 index 0000000..d856d44 --- /dev/null +++ b/packages/cc-server-db-mongo/.gitignore @@ -0,0 +1,4 @@ +/node_modules +/dist +/package-lock.json +/yarn.lock diff --git a/packages/cc-server-db-mongo/README.md b/packages/cc-server-db-mongo/README.md new file mode 100644 index 0000000..cbbca85 --- /dev/null +++ b/packages/cc-server-db-mongo/README.md @@ -0,0 +1,11 @@ +# `cc-server-db-mongo` + +> TODO: description + +## Usage + +``` +const ccServerDbMongo = require('cc-server-db-mongo'); + +// TODO: DEMONSTRATE API +``` diff --git a/packages/cc-server-db-mongo/package.json b/packages/cc-server-db-mongo/package.json new file mode 100644 index 0000000..d4b6777 --- /dev/null +++ b/packages/cc-server-db-mongo/package.json @@ -0,0 +1,24 @@ +{ + "name": "cc-server-db-mongo", + "version": "0.0.1", + "description": "> TODO: description", + "author": "MiaoWoo ", + "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" + } +} \ No newline at end of file diff --git a/packages/cc-server-db-mongo/src/client.ts b/packages/cc-server-db-mongo/src/client.ts new file mode 100644 index 0000000..ddec809 --- /dev/null +++ b/packages/cc-server-db-mongo/src/client.ts @@ -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 { + find(table: string, where: object): T +} + +export const NAME: string = 'MongoDBClient' + +@service() +@injectable() +export class MongoDBClient implements DBClient { + public db: Db; + + constructor() { + MongoDBConnection.getConnection((connection) => { + this.db = connection; + }); + } + + public find(collection: string, filter: object): Promise { + return this.db.collection(collection).find(filter).toArray(); + } + + public async findOne(collection: string, filter: Object): Promise { + let result = await this.db.collection(collection).find(filter).limit(1).toArray(); + return result[0]; + } + + public async findOneById(collection: string, objectId: string): Promise { + return this.findOne(collection, { _id: new ObjectID(objectId) }) + } + + public async insertOne(collection: string, model: T): Promise { + var insert = await this.db.collection(collection).insertOne(model); + return insert.ops[0]; + } + + public updateOne(collection: string, where: any, model: any): Promise { + return this.db.collection(collection).updateOne(where, { $set: model }); + } + + public updateById(collection: string, objectId: string, model: any): Promise { + return this.updateOne(collection, { _id: new ObjectID(objectId) }, { $set: model }) + } + + public async deleteOne(collection: string, where: any): Promise { + 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 { + return this.deleteOne(collection, { _id: new ObjectID(objectId) }); + } +} diff --git a/packages/cc-server-db-mongo/src/connection.ts b/packages/cc-server-db-mongo/src/connection.ts new file mode 100644 index 0000000..94e8894 --- /dev/null +++ b/packages/cc-server-db-mongo/src/connection.ts @@ -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); + }); + } +} diff --git a/packages/cc-server-db-mongo/src/index.ts b/packages/cc-server-db-mongo/src/index.ts new file mode 100644 index 0000000..7eec814 --- /dev/null +++ b/packages/cc-server-db-mongo/src/index.ts @@ -0,0 +1 @@ +export * from './client' \ No newline at end of file diff --git a/packages/cc-server-db-mongo/tsconfig.json b/packages/cc-server-db-mongo/tsconfig.json new file mode 100644 index 0000000..7aae5d2 --- /dev/null +++ b/packages/cc-server-db-mongo/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": "src", + "outDir": "dist" + } +} \ No newline at end of file diff --git a/packages/cc-server-ioc/.gitignore b/packages/cc-server-ioc/.gitignore new file mode 100644 index 0000000..d856d44 --- /dev/null +++ b/packages/cc-server-ioc/.gitignore @@ -0,0 +1,4 @@ +/node_modules +/dist +/package-lock.json +/yarn.lock diff --git a/packages/cc-server-ioc/README.md b/packages/cc-server-ioc/README.md new file mode 100644 index 0000000..d84b8c9 --- /dev/null +++ b/packages/cc-server-ioc/README.md @@ -0,0 +1,11 @@ +# `cc-server-ioc` + +> TODO: description + +## Usage + +``` +const ccServerIoc = require('cc-server-ioc'); + +// TODO: DEMONSTRATE API +``` diff --git a/packages/cc-server-ioc/package.json b/packages/cc-server-ioc/package.json new file mode 100644 index 0000000..bf53c69 --- /dev/null +++ b/packages/cc-server-ioc/package.json @@ -0,0 +1,20 @@ +{ + "name": "cc-server-ioc", + "version": "0.0.1", + "description": "> TODO: description", + "author": "MiaoWoo ", + "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": {} +} \ No newline at end of file diff --git a/packages/cc-server-ioc/src/constant/types.ts b/packages/cc-server-ioc/src/constant/types.ts new file mode 100644 index 0000000..522d60b --- /dev/null +++ b/packages/cc-server-ioc/src/constant/types.ts @@ -0,0 +1,4 @@ +export const METADATA_KEY = { + container: "cc-server-core:container", + service: "cc-server-core:service" +}; \ No newline at end of file diff --git a/packages/cc-server-ioc/src/decorators.ts b/packages/cc-server-ioc/src/decorators.ts new file mode 100644 index 0000000..2cfedec --- /dev/null +++ b/packages/cc-server-ioc/src/decorators.ts @@ -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 } diff --git a/packages/cc-server-ioc/src/index.ts b/packages/cc-server-ioc/src/index.ts new file mode 100644 index 0000000..196ac11 --- /dev/null +++ b/packages/cc-server-ioc/src/index.ts @@ -0,0 +1,2 @@ +export * from './constant/types' +export * from './decorators' \ No newline at end of file diff --git a/packages/cc-server-ioc/tsconfig.json b/packages/cc-server-ioc/tsconfig.json new file mode 100644 index 0000000..7aae5d2 --- /dev/null +++ b/packages/cc-server-ioc/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": "src", + "outDir": "dist" + } +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7d7d212 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "es6", + "module": "commonjs", + "sourceMap": true, + "allowUnreachableCode": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true + } +} \ No newline at end of file