2020-11-11 10:16:18 +00:00
|
|
|
import Koa from 'koa';
|
|
|
|
import Database from '@nocobase/database';
|
2020-11-12 03:48:27 +00:00
|
|
|
import Resourcer from '@nocobase/resourcer';
|
|
|
|
|
|
|
|
export interface ApplicationOptions {
|
|
|
|
database: any;
|
|
|
|
resourcer: any;
|
|
|
|
}
|
2020-11-11 10:16:18 +00:00
|
|
|
|
|
|
|
export class Application extends Koa {
|
|
|
|
|
2020-11-12 03:48:27 +00:00
|
|
|
public readonly database: Database;
|
2020-11-11 10:16:18 +00:00
|
|
|
|
2020-11-12 03:48:27 +00:00
|
|
|
public readonly resourcer: Resourcer;
|
|
|
|
|
|
|
|
constructor(options: ApplicationOptions) {
|
|
|
|
super();
|
|
|
|
this.database = new Database(options.database);
|
|
|
|
this.resourcer = new Resourcer();
|
|
|
|
}
|
2020-11-11 10:16:18 +00:00
|
|
|
|
|
|
|
async plugins(plugins: any[]) {
|
|
|
|
for (const pluginOption of plugins) {
|
|
|
|
let plugin: Function;
|
|
|
|
let options = {};
|
|
|
|
if (Array.isArray(pluginOption)) {
|
|
|
|
plugin = pluginOption.shift();
|
|
|
|
options = pluginOption.shift()||{};
|
|
|
|
if (typeof plugin === 'function') {
|
|
|
|
plugin = plugin.bind(this);
|
|
|
|
} else if (typeof plugin === 'string') {
|
|
|
|
const libDir = __filename.endsWith('.ts') ? 'src' : 'lib';
|
|
|
|
plugin = require(`${plugin}/${libDir}/server`).default;
|
|
|
|
plugin = plugin.bind(this);
|
|
|
|
}
|
|
|
|
} else if (typeof pluginOption === 'function') {
|
|
|
|
plugin = pluginOption.bind(this);
|
|
|
|
}
|
|
|
|
await plugin(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Application;
|