From cb4e152800fb62ce6099e6b3deef65ed6a311c47 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Thu, 16 Jan 2020 17:28:10 +0800 Subject: [PATCH] feat: add plugin controller Signed-off-by: MiaoWoo --- packages/manager/package.json | 12 ++++++-- packages/manager/src/controller/Index.ts | 37 ++++++++++++++++++++++++ packages/manager/src/index.ts | 22 ++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 packages/manager/src/controller/Index.ts diff --git a/packages/manager/package.json b/packages/manager/package.json index 4e3b502a..d7a3fe16 100644 --- a/packages/manager/package.json +++ b/packages/manager/package.json @@ -16,15 +16,21 @@ "registry": "https://repo.yumc.pw/repository/npm-hosted/" }, "scripts": { + "dev": "npx ts-node-dev --respawn --debounce=1500 src/index.ts", "clean": "rimraf dist", "watch": "npx tsc --watch", "build": "yarn clean && npx tsc", "test": "echo \"Error: run tests from root\" && exit 1" }, + "dependencies": { + "@cc-server/core": "^0.7.0", + "mongodb": "^3.4.1" + }, "devDependencies": { + "@types/mongodb": "^3.3.14", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.0", + "ts-node-dev": "^1.0.0-pre.44", "typescript": "^3.7.2" - }, - "dependencies": {} -} \ No newline at end of file + } +} diff --git a/packages/manager/src/controller/Index.ts b/packages/manager/src/controller/Index.ts new file mode 100644 index 00000000..521b13b6 --- /dev/null +++ b/packages/manager/src/controller/Index.ts @@ -0,0 +1,37 @@ +import { DBClient } from '@cc-server/db' +import { lazyInjectNamed } from '@cc-server/ioc' +import { controller, get, post, requestParam, requestBody, Vaild, NotBlank } from '@cc-server/binding' + +class Plugins { + @NotBlank() + name?: string; + author?: string; + version?: string; + source?: string; + type?: string; + dist?: string; +} + +type distType = 'npm' | 'git' | 'src' + +@controller('/plugin') +class PluginController { + @lazyInjectNamed(DBClient, Plugins.name.toLocaleLowerCase()) + private client: DBClient + + @get('/') + index() { + return this.client.find({}); + } + + @get('/:id') + details(@requestParam("id") id: string) { + return this.client.findOneById(id); + } + + @post('/') + add(@requestBody() @Vaild() model: Plugins) { + return this.client.insertOne(model); + } + +} \ No newline at end of file diff --git a/packages/manager/src/index.ts b/packages/manager/src/index.ts index e69de29b..df54c78c 100644 --- a/packages/manager/src/index.ts +++ b/packages/manager/src/index.ts @@ -0,0 +1,22 @@ +import * as path from 'path' +import { Db, MongoClient } from 'mongodb' +import { DBClient } from '@cc-server/db' +import { interfaces } from '@cc-server/ioc' +import { CcServerBoot } from '@cc-server/core' +import { MongoCollection, TYPE } from '@cc-server/db-mongo'; + +async function main() { + let server = new CcServerBoot(); + let collectionCache = {}; + server.container.bind(DBClient).toDynamicValue((context: interfaces.Context) => { + let name = context.currentRequest.target.getNamedTag().value; + if (!name) { return null } + if (!collectionCache[name]) { collectionCache[name] = new MongoCollection(context.container.get(TYPE.DB).collection(name)) } + return collectionCache[name]; + }) + let client = await MongoClient.connect("mongodb://192.168.2.5:27017", { useNewUrlParser: true, connectTimeoutMS: 10000 }) + server.container.bind("MONGO_DB").toConstantValue(client.db("mspc")); + server.scan(path.join(__dirname, "controller")).start() +} + +main()