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