Compare commits

..

No commits in common. "master" and "v0.6.9" have entirely different histories.

12 changed files with 63 additions and 195 deletions

134
README.md
View File

@ -1,137 +1,11 @@
# `cc-server` # `cc-server-parent`
> 一个简易的 IOC 注入 Web 开发框架 仿照 SpringBoot 使用 TypeScript 开发 > TODO: description
## Usage ## Usage
> 当前包尚未发布到 NPM 中央仓库
> 如需使用 请自行配制 repo `https://repo.yumc.pw/repository/npm/`
### Simple Example
```TypeScript
import { CcServerBoot, express } from './index'
import './function/http'; // => WebServer
import './function/websocket'; // => Socket.IO
import { DBClient } from '@cc-server/db';
import { MongoClient } from 'mongodb';
import { MongoCollection } from '@cc-server/db-mongo';
let boot = new CcServerBoot().static('public');
MongoClient.connect("mongodb://192.168.2.5:27017", { useNewUrlParser: true }, (error, client) => {
if (error) {
console.log(error)
} else {
boot.container.bind(DBClient).toConstantValue(new MongoCollection(client.db("faas").collection("users"))).whenTargetNamed("users")
boot.build().listen();
}
})
``` ```
const ccServerDbMongo = require('cc-server-parent');
### WebServer // TODO: DEMONSTRATE API
```TypeScript
import { DBClient } from '@cc-server/db'
import { inject, named } from '@cc-server/ioc';
import { Vaild, NotBlank, NotNull, controller, requestBody, get, post, requestParam } from '@cc-server/binding'
import '@cc-server/db-mongo'
class ExampleModel {
_id: string;
@NotBlank("username must not be blank!")
username: string;
password: string;
@NotNull()
age: number;
email: string;
}
@controller('/example')
export class Controller {
@inject(DBClient)
@named("users")
private client: DBClient
@get('/')
public async list(): Promise<ExampleModel[]> {
return this.client.find({});
}
@get('/:id')
public async get(
@requestParam('id') id: string
): Promise<ExampleModel> {
return this.client.findOneById(id);
}
@post('/')
public async create(
@Vaild() @requestBody() model: ExampleModel
): Promise<ExampleModel> {
return model;
}
@post('/:id')
public async update(
@requestParam('id') id: string,
@Vaild() @requestBody() model: ExampleModel
): Promise<boolean> {
return this.client.updateById(id, model);
}
}
```
### Socket.IO
```TypeScript
import { lazyInjectNamed } from '@cc-server/ioc'
import { controller, httpPost, requestBody } from '@cc-server/binding';
import { namespace, listener, interfaces, BroadcastMessage, io, TYPE } from '@cc-server/ws'
@namespace('/', (socket: io.Socket, next: (err?: any) => void) => {
console.log(socket.nsp.name, socket.id, 'before connection');
next();
})
export class Namespace extends interfaces.Namespace {
private cache: { [key: string]: string } = {};
public async connection(socket: io.Socket) {
console.log(this.nsp.name, socket.id, 'connection');
this.defer(socket, socket => console.log(this.nsp.name, socket.id, 'defer', this))
return `Welcome to Websocket Chat Room Now: ${Date.now()} Your ID: ${socket.id}! \r\n`;
}
public async disconnect(socket: io.Socket) {
console.log(this.nsp.name, socket.id, 'disconnect');
}
@listener('message', (socket: io.Socket, packet: io.Packet, next: (err?: any) => void) => {
console.log(socket.nsp.name, socket.id, 'listener middleware', [...packet]);
next();
})
public async message(socket: io.Socket, data: any) {
console.log(this.nsp.name, socket.id, 'message', data)
this.cache[socket.id] = (this.cache[socket.id] || '') + data;
if (data == '\r' && this.cache[socket.id] !== "") {
let result = new BroadcastMessage(this.cache[socket.id] + '\n')
return delete this.cache[socket.id] && result;
}
return data;
}
}
@controller('/websocket')
export class WebSocketController {
@lazyInjectNamed(TYPE.Namespace, Namespace.name)
private root: Namespace;
@httpPost('/')
public async create(
@requestBody() model: Object
): Promise<Object> {
this.root.nsp.send(JSON.stringify(model));
return model;
}
}
``` ```

View File

@ -1,7 +1,7 @@
{ {
"npmClient": "yarn", "npmClient": "yarn",
"useWorkspaces": true, "useWorkspaces": true,
"version": "0.7.0", "version": "0.6.9",
"packages": [ "packages": [
"packages/*" "packages/*"
], ],

View File

@ -1,6 +1,6 @@
{ {
"name": "@cc-server/binding", "name": "@cc-server/binding",
"version": "0.7.0", "version": "0.6.9",
"description": "> TODO: description", "description": "> TODO: description",
"author": "MiaoWoo <admin@yumc.pw>", "author": "MiaoWoo <admin@yumc.pw>",
"homepage": "https://github.com/502647092/cc-server-parent#readme", "homepage": "https://github.com/502647092/cc-server-parent#readme",
@ -24,9 +24,9 @@
"reflect-metadata": "^0.1.13" "reflect-metadata": "^0.1.13"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^7.0.0", "mocha": "^6.1.4",
"rimraf": "^3.0.0", "rimraf": "^2.6.3",
"typescript": "^3.7.4" "typescript": "^3.5.1"
}, },
"bugs": { "bugs": {
"url": "https://github.com/502647092/cc-server-parent/issues" "url": "https://github.com/502647092/cc-server-parent/issues"

View File

@ -1,5 +1,5 @@
import { httpGet as get, httpPost as post, httpPut as put, httpPatch as patch, httpDelete as del } from 'inversify-express-utils' import { httpGet as get, httpPost as post } from 'inversify-express-utils'
export * from './decorators' export * from './decorators'
export * from './activation' export * from './activation'
export * from 'inversify-express-utils' export * from 'inversify-express-utils'
export { get, post, put, patch, del } export { get, post }

View File

@ -1,6 +1,6 @@
{ {
"name": "@cc-server/core", "name": "@cc-server/core",
"version": "0.7.0", "version": "0.6.9",
"description": "> TODO: description", "description": "> TODO: description",
"author": "MiaoWoo <admin@yumc.pw>", "author": "MiaoWoo <admin@yumc.pw>",
"homepage": "https://github.com/502647092/cc-server-parent#readme", "homepage": "https://github.com/502647092/cc-server-parent#readme",
@ -21,27 +21,27 @@
"url": "git+https://github.com/502647092/cc-server-parent.git" "url": "git+https://github.com/502647092/cc-server-parent.git"
}, },
"dependencies": { "dependencies": {
"@cc-server/binding": "^0.7.0", "@cc-server/binding": "^0.6.9",
"@cc-server/db-mongo": "^0.7.0", "@cc-server/db-mongo": "^0.6.9",
"@cc-server/ioc": "^0.7.0", "@cc-server/ioc": "^0.6.9",
"@cc-server/ws": "^0.7.0", "@cc-server/ws": "^0.6.9",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"globby": "^10.0.2", "globby": "^9.2.0",
"inversify": "^5.0.1", "inversify": "^5.0.1",
"inversify-express-utils": "^6.3.2", "inversify-express-utils": "^6.3.2",
"prettyjson": "^1.2.1", "prettyjson": "^1.2.1",
"socket.io": "^2.3.0" "socket.io": "^2.2.0"
}, },
"devDependencies": { "devDependencies": {
"@types/body-parser": "^1.17.1", "@types/body-parser": "^1.17.0",
"@types/express": "4.17.0", "@types/express": "^4.17.0",
"@types/prettyjson": "^0.0.29", "@types/prettyjson": "^0.0.29",
"@types/socket.io": "^2.1.4", "@types/socket.io": "^2.1.2",
"mocha": "^7.0.0", "mocha": "^6.1.4",
"rimraf": "^3.0.0", "rimraf": "^2.6.3",
"ts-node": "^8.5.4", "ts-node": "^8.2.0",
"ts-node-dev": "^1.0.0-pre.44", "ts-node-dev": "^1.0.0-pre.40",
"typescript": "^3.7.4" "typescript": "^3.5.1"
}, },
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728" "gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
} }

View File

@ -1,7 +1,6 @@
import 'reflect-metadata'; import 'reflect-metadata';
import * as fs from 'fs'; import * as fs from 'fs';
import * as http from 'http'; import * as http from 'http';
import * as path from 'path';
import * as globby from "globby"; import * as globby from "globby";
import * as express from "express"; import * as express from "express";
import * as prettyjson from "prettyjson"; import * as prettyjson from "prettyjson";
@ -105,18 +104,11 @@ export class CcServerBoot {
return this; return this;
} }
public scan(scanDir: string) { public scan(path: fs.PathLike) {
let files = fs.readdirSync(scanDir); let files = fs.readdirSync(path);
for (let file of files) { for (const file of files) {
let moduleDir = path.join(scanDir, file)
let stat = fs.statSync(moduleDir);
if (stat.isDirectory()) {
this.scan(moduleDir)
} else if (stat.isFile() && (file.endsWith('.js') || file.endsWith('.ts'))) {
require(moduleDir);
}
} }
return this;
} }
public build() { public build() {

View File

@ -1,14 +1,17 @@
import { CcServerBoot } from './index' import { CcServerBoot, express } from './index'
import './function/http';
import './function/websocket';
import { DBClient } from '@cc-server/db'; import { DBClient } from '@cc-server/db';
import { MongoClient } from 'mongodb'; import { MongoClient } from 'mongodb';
import { MongoCollection } from '@cc-server/db-mongo'; import { MongoCollection } from '@cc-server/db-mongo';
let boot = new CcServerBoot().scan(__dirname + '/function'); let boot = new CcServerBoot().static('public');
MongoClient.connect("mongodb://192.168.2.5:27017", { useNewUrlParser: true }, (error, client) => { MongoClient.connect("mongodb://192.168.2.5:27017", { useNewUrlParser: true }, (error, client) => {
if (error) { if (error) {
console.log(error) console.log(error)
} else { } else {
boot.container.bind(DBClient).toConstantValue(new MongoCollection(client.db("faas").collection("users"))).whenTargetNamed("users") boot.container.bind(DBClient).toConstantValue(new MongoCollection(client.db("faas").collection("users"))).whenTargetNamed("users")
boot.start(); boot.build().listen();
} }
}) })

View File

@ -1,6 +1,6 @@
{ {
"name": "@cc-server/db-mongo", "name": "@cc-server/db-mongo",
"version": "0.7.0", "version": "0.6.9",
"description": "> TODO: description", "description": "> TODO: description",
"author": "MiaoWoo <admin@yumc.pw>", "author": "MiaoWoo <admin@yumc.pw>",
"homepage": "https://faas.yumc.pw", "homepage": "https://faas.yumc.pw",
@ -20,17 +20,17 @@
"url": "git+https://github.com/502647092/cc-server-parent.git" "url": "git+https://github.com/502647092/cc-server-parent.git"
}, },
"dependencies": { "dependencies": {
"@cc-server/db": "^0.7.0", "@cc-server/db": "^0.6.9",
"@cc-server/ioc": "^0.7.0", "@cc-server/ioc": "^0.6.9",
"inversify": "^5.0.1", "inversify": "^5.0.1",
"mongodb": "^3.4.1", "mongodb": "^3.2.7",
"reflect-metadata": "^0.1.13" "reflect-metadata": "^0.1.13"
}, },
"devDependencies": { "devDependencies": {
"@types/mongodb": "^3.3.14", "@types/mongodb": "^3.1.28",
"mocha": "^7.0.0", "mocha": "^6.1.4",
"rimraf": "^3.0.0", "rimraf": "^2.6.3",
"typescript": "^3.7.4" "typescript": "^3.5.1"
}, },
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728" "gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
} }

View File

@ -16,7 +16,7 @@ export class MongoCollection<T = any> implements DBClient {
return await this.collection.find(filter).toArray(); return await this.collection.find(filter).toArray();
} }
public async findOne(filter: object): Promise<T> { public async findOne(filter: Object): Promise<T> {
let result = await this.collection.find(filter).limit(1).toArray(); let result = await this.collection.find(filter).limit(1).toArray();
return result[0]; return result[0];
} }
@ -26,21 +26,20 @@ export class MongoCollection<T = any> implements DBClient {
} }
public async insertOne(model: T): Promise<T> { public async insertOne(model: T): Promise<T> {
//@ts-ignore
var insert = await this.collection.insertOne(model); var insert = await this.collection.insertOne(model);
return insert.ops[0]; return insert.ops[0];
} }
public async updateOne(where: any, model: T): Promise<boolean> { public async updateOne(where: any, model: any): Promise<boolean> {
let result = await this.collection.updateOne(where, { $set: model }); let result = await this.collection.updateOne(where, { $set: model });
return result.result.ok == 1 && result.result.n > 0; return result.result.ok == 1 && result.result.n > 0;
} }
public async updateById(objectId: string, model: T): Promise<boolean> { public async updateById(objectId: string, model: any): Promise<boolean> {
return await this.updateOne({ _id: new ObjectID(objectId) }, model) return await this.updateOne({ _id: new ObjectID(objectId) }, model)
} }
public async deleteOne(where: object): Promise<boolean> { public async deleteOne(where: any): Promise<boolean> {
let result = await this.collection.deleteOne(where); let result = await this.collection.deleteOne(where);
return result.result.ok === 1 && result.result.n > 0 return result.result.ok === 1 && result.result.n > 0
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@cc-server/db", "name": "@cc-server/db",
"version": "0.7.0", "version": "0.6.9",
"description": "> TODO: description", "description": "> TODO: description",
"author": "MiaoWoo <admin@yumc.pw>", "author": "MiaoWoo <admin@yumc.pw>",
"homepage": "https://github.com/502647092/cc-server-parent#readme", "homepage": "https://github.com/502647092/cc-server-parent#readme",
@ -23,8 +23,8 @@
"url": "https://github.com/502647092/cc-server-parent/issues" "url": "https://github.com/502647092/cc-server-parent/issues"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^7.0.0", "mocha": "^6.1.4",
"rimraf": "^3.0.0", "rimraf": "^2.6.3",
"typescript": "^3.7.4" "typescript": "^3.5.1"
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@cc-server/ioc", "name": "@cc-server/ioc",
"version": "0.7.0", "version": "0.6.9",
"description": "> TODO: description", "description": "> TODO: description",
"author": "MiaoWoo <admin@yumc.pw>", "author": "MiaoWoo <admin@yumc.pw>",
"homepage": "https://faas.yumc.pw", "homepage": "https://faas.yumc.pw",
@ -25,9 +25,9 @@
"reflect-metadata": "^0.1.13" "reflect-metadata": "^0.1.13"
}, },
"devDependencies": { "devDependencies": {
"mocha": "^7.0.0", "mocha": "^6.1.4",
"rimraf": "^3.0.0", "rimraf": "^2.6.3",
"typescript": "^3.7.4" "typescript": "^3.5.1"
}, },
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728" "gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@cc-server/ws", "name": "@cc-server/ws",
"version": "0.7.0", "version": "0.6.9",
"description": "> TODO: description", "description": "> TODO: description",
"author": "MiaoWoo <admin@yumc.pw>", "author": "MiaoWoo <admin@yumc.pw>",
"homepage": "https://faas.yumc.pw", "homepage": "https://faas.yumc.pw",
@ -20,16 +20,16 @@
"url": "git+https://github.com/502647092/cc-server-parent.git" "url": "git+https://github.com/502647092/cc-server-parent.git"
}, },
"dependencies": { "dependencies": {
"@cc-server/ioc": "^0.7.0", "@cc-server/ioc": "^0.6.9",
"inversify": "^5.0.1", "inversify": "^5.0.1",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"socket.io": "^2.3.0" "socket.io": "^2.2.0"
}, },
"devDependencies": { "devDependencies": {
"@types/socket.io": "^2.1.4", "@types/socket.io": "^2.1.2",
"mocha": "^7.0.0", "mocha": "^6.1.4",
"rimraf": "^3.0.0", "rimraf": "^2.6.3",
"typescript": "^3.7.4" "typescript": "^3.5.1"
}, },
"gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728" "gitHead": "7d84393a3cb6be6be9ed51d71f12677d2d7d0728"
} }