parent
204a516165
commit
1023e317f3
@ -28,12 +28,12 @@ describe('middleware', () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
database = new Database({
|
database = new Database({
|
||||||
username: process.env.TEST_DB_USER,
|
username: process.env.DB_USER,
|
||||||
password: process.env.TEST_DB_PASSWORD,
|
password: process.env.DB_PASSWORD,
|
||||||
database: process.env.TEST_DB_DATABASE,
|
database: process.env.DB_DATABASE,
|
||||||
host: process.env.TEST_DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
port: process.env.TEST_DB_PORT as any,
|
port: process.env.DB_PORT as any,
|
||||||
dialect: process.env.TEST_DB_DIALECT as any,
|
dialect: process.env.DB_DIALECT as any,
|
||||||
dialectOptions: {
|
dialectOptions: {
|
||||||
charset: 'utf8mb4',
|
charset: 'utf8mb4',
|
||||||
collate: 'utf8mb4_unicode_ci',
|
collate: 'utf8mb4_unicode_ci',
|
||||||
|
@ -1,12 +1,23 @@
|
|||||||
import Koa from 'koa';
|
import Koa from 'koa';
|
||||||
import Database from '@nocobase/database';
|
import Database from '@nocobase/database';
|
||||||
import Resourcer, { Action, ParsedParams } from '@nocobase/resourcer';
|
import Resourcer from '@nocobase/resourcer';
|
||||||
|
|
||||||
|
export interface ApplicationOptions {
|
||||||
|
database: any;
|
||||||
|
resourcer: any;
|
||||||
|
}
|
||||||
|
|
||||||
export class Application extends Koa {
|
export class Application extends Koa {
|
||||||
|
|
||||||
database: Database;
|
public readonly database: Database;
|
||||||
|
|
||||||
resourcer: Resourcer;
|
public readonly resourcer: Resourcer;
|
||||||
|
|
||||||
|
constructor(options: ApplicationOptions) {
|
||||||
|
super();
|
||||||
|
this.database = new Database(options.database);
|
||||||
|
this.resourcer = new Resourcer();
|
||||||
|
}
|
||||||
|
|
||||||
async plugins(plugins: any[]) {
|
async plugins(plugins: any[]) {
|
||||||
for (const pluginOption of plugins) {
|
for (const pluginOption of plugins) {
|
@ -1,30 +1,34 @@
|
|||||||
import Database from '@nocobase/database';
|
import Database from '@nocobase/database';
|
||||||
import Resourcer, { Action } from '@nocobase/resourcer';
|
import Resourcer, { Action } from '@nocobase/resourcer';
|
||||||
import actions from '@nocobase/actions';
|
import actions from '@nocobase/actions';
|
||||||
import Application from './applicatiion';
|
import Application from './application';
|
||||||
import bodyParser from 'koa-bodyparser';
|
import bodyParser from 'koa-bodyparser';
|
||||||
import cors from '@koa/cors';
|
import cors from '@koa/cors';
|
||||||
import middleware from './middleware';
|
import middleware from './middleware';
|
||||||
|
|
||||||
|
export * from './application';
|
||||||
|
export * from './middleware';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
/**
|
||||||
|
* 这部分还比较杂,细节待改进
|
||||||
|
*
|
||||||
|
* @param options
|
||||||
|
*/
|
||||||
create(options: any): Application {
|
create(options: any): Application {
|
||||||
console.log(options);
|
console.log(options);
|
||||||
|
|
||||||
const app = new Application();
|
const app = new Application(options);
|
||||||
const database = new Database(options.database);
|
|
||||||
const resourcer = new Resourcer();
|
|
||||||
|
|
||||||
app.database = database;
|
|
||||||
app.resourcer = resourcer;
|
|
||||||
|
|
||||||
app.use(bodyParser());
|
app.use(bodyParser());
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
resourcer.registerHandlers(actions.common);
|
// 这段代码处理的不完整
|
||||||
|
app.resourcer.registerHandlers(actions.common);
|
||||||
|
|
||||||
app.use(async (ctx, next) => {
|
app.use(async (ctx, next) => {
|
||||||
ctx.db = database;
|
ctx.db = app.database;
|
||||||
ctx.database = database;
|
ctx.database = app.database;
|
||||||
await next();
|
await next();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -50,8 +54,8 @@ export default {
|
|||||||
|
|
||||||
app.use(middleware({
|
app.use(middleware({
|
||||||
prefix: '/api',
|
prefix: '/api',
|
||||||
database,
|
database: app.database,
|
||||||
resourcer,
|
resourcer: app.resourcer,
|
||||||
...(options.resourcer||{}),
|
...(options.resourcer||{}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
@ -8,8 +8,12 @@ interface MiddlewareOptions extends KoaMiddlewareOptions {
|
|||||||
resourcer?: Resourcer;
|
resourcer?: Resourcer;
|
||||||
database?: Database;
|
database?: Database;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
export default function middleware(options: MiddlewareOptions = {}) {
|
* database + resourcer 结合的中间件(暂时不知道起什么名好)
|
||||||
|
*
|
||||||
|
* @param options
|
||||||
|
*/
|
||||||
|
export function middleware(options: MiddlewareOptions = {}) {
|
||||||
const {
|
const {
|
||||||
prefix,
|
prefix,
|
||||||
database,
|
database,
|
||||||
@ -110,4 +114,6 @@ export default function middleware(options: MiddlewareOptions = {}) {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default middleware;
|
||||||
|
Loading…
Reference in New Issue
Block a user