feat: add plugin controller

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-01-16 17:28:10 +08:00
parent fb83acfcc8
commit cb4e152800
3 changed files with 68 additions and 3 deletions

View File

@ -16,15 +16,21 @@
"registry": "https://repo.yumc.pw/repository/npm-hosted/" "registry": "https://repo.yumc.pw/repository/npm-hosted/"
}, },
"scripts": { "scripts": {
"dev": "npx ts-node-dev --respawn --debounce=1500 src/index.ts",
"clean": "rimraf dist", "clean": "rimraf dist",
"watch": "npx tsc --watch", "watch": "npx tsc --watch",
"build": "yarn clean && npx tsc", "build": "yarn clean && npx tsc",
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"
}, },
"dependencies": {
"@cc-server/core": "^0.7.0",
"mongodb": "^3.4.1"
},
"devDependencies": { "devDependencies": {
"@types/mongodb": "^3.3.14",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rimraf": "^3.0.0", "rimraf": "^3.0.0",
"ts-node-dev": "^1.0.0-pre.44",
"typescript": "^3.7.2" "typescript": "^3.7.2"
}, }
"dependencies": {}
} }

View File

@ -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<Plugins>
@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);
}
}

View File

@ -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<Db>(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()