2022-02-11 23:59:03 +08:00
|
|
|
import { ACL } from '@nocobase/acl';
|
2021-09-16 00:38:48 +08:00
|
|
|
import { registerActions } from '@nocobase/actions';
|
2022-02-15 00:20:25 +08:00
|
|
|
import Database, { CleanOptions, CollectionOptions, IDatabaseOptions, SyncOptions } from '@nocobase/database';
|
2022-01-30 11:11:36 +08:00
|
|
|
import Resourcer, { ResourceOptions } from '@nocobase/resourcer';
|
|
|
|
import { applyMixins, AsyncEmitter } from '@nocobase/utils';
|
|
|
|
import { Command, CommandOptions } from 'commander';
|
|
|
|
import { Server } from 'http';
|
2021-11-08 19:32:59 +08:00
|
|
|
import { i18n, InitOptions } from 'i18next';
|
2022-01-30 11:11:36 +08:00
|
|
|
import Koa from 'koa';
|
|
|
|
import { isBoolean } from 'lodash';
|
2022-02-11 23:59:03 +08:00
|
|
|
import { createACL } from './acl';
|
2022-01-30 11:11:36 +08:00
|
|
|
import { createCli, createDatabase, createI18n, createResourcer, registerMiddlewares } from './helper';
|
|
|
|
import { Plugin } from './plugin';
|
2022-02-28 21:49:50 +08:00
|
|
|
import { PluginManager, InstallOptions } from './plugin-manager';
|
2021-09-09 13:05:33 +08:00
|
|
|
|
|
|
|
export interface ResourcerOptions {
|
|
|
|
prefix?: string;
|
|
|
|
}
|
2020-11-12 11:48:27 +08:00
|
|
|
|
|
|
|
export interface ApplicationOptions {
|
2022-02-15 00:20:25 +08:00
|
|
|
database?: IDatabaseOptions | Database;
|
2021-09-09 13:05:33 +08:00
|
|
|
resourcer?: ResourcerOptions;
|
|
|
|
bodyParser?: any;
|
|
|
|
cors?: any;
|
|
|
|
dataWrapping?: boolean;
|
2021-10-18 12:49:37 +08:00
|
|
|
registerActions?: boolean;
|
2021-11-08 19:32:59 +08:00
|
|
|
i18n?: i18n | InitOptions;
|
2020-11-12 11:48:27 +08:00
|
|
|
}
|
2020-11-11 18:16:18 +08:00
|
|
|
|
2021-09-16 15:57:21 +08:00
|
|
|
interface DefaultState {
|
|
|
|
currentUser?: any;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DefaultContext {
|
|
|
|
db: Database;
|
|
|
|
resourcer: Resourcer;
|
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MiddlewareOptions {
|
|
|
|
name?: string;
|
|
|
|
resourceName?: string;
|
|
|
|
resourceNames?: string[];
|
|
|
|
insertBefore?: string;
|
|
|
|
insertAfter?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ActionsOptions {
|
|
|
|
resourceName?: string;
|
|
|
|
resourceNames?: string[];
|
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
interface ListenOptions {
|
|
|
|
port?: number | undefined;
|
|
|
|
host?: string | undefined;
|
|
|
|
backlog?: number | undefined;
|
|
|
|
path?: string | undefined;
|
|
|
|
exclusive?: boolean | undefined;
|
|
|
|
readableAll?: boolean | undefined;
|
|
|
|
writableAll?: boolean | undefined;
|
|
|
|
/**
|
|
|
|
* @default false
|
|
|
|
*/
|
|
|
|
ipv6Only?: boolean | undefined;
|
|
|
|
signal?: AbortSignal | undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StartOptions {
|
2022-02-07 01:14:00 +08:00
|
|
|
cliArgs?: any[];
|
2022-01-30 11:11:36 +08:00
|
|
|
listen?: ListenOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Application<StateT = DefaultState, ContextT = DefaultContext> extends Koa implements AsyncEmitter {
|
2021-09-14 11:09:26 +08:00
|
|
|
public readonly db: Database;
|
2021-01-13 16:23:15 +08:00
|
|
|
|
|
|
|
public readonly resourcer: Resourcer;
|
2020-11-11 18:16:18 +08:00
|
|
|
|
2021-09-05 23:59:38 +08:00
|
|
|
public readonly cli: Command;
|
|
|
|
|
2021-11-08 19:32:59 +08:00
|
|
|
public readonly i18n: i18n;
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
public readonly pm: PluginManager;
|
|
|
|
|
2022-02-11 23:59:03 +08:00
|
|
|
public readonly acl: ACL;
|
|
|
|
|
2021-09-14 11:09:26 +08:00
|
|
|
protected plugins = new Map<string, Plugin>();
|
2020-11-12 11:48:27 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
public listenServer: Server;
|
|
|
|
|
2021-01-13 16:23:15 +08:00
|
|
|
constructor(options: ApplicationOptions) {
|
|
|
|
super();
|
2021-09-09 13:05:33 +08:00
|
|
|
|
2022-02-11 23:59:03 +08:00
|
|
|
this.acl = createACL();
|
2021-10-18 12:49:37 +08:00
|
|
|
this.db = createDatabase(options);
|
|
|
|
this.resourcer = createResourcer(options);
|
|
|
|
this.cli = createCli(this, options);
|
2021-11-08 19:32:59 +08:00
|
|
|
this.i18n = createI18n(options);
|
2021-09-05 23:59:38 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
this.pm = new PluginManager({
|
|
|
|
app: this,
|
|
|
|
});
|
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
registerMiddlewares(this, options);
|
2022-03-11 10:10:57 +08:00
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
if (options.registerActions !== false) {
|
|
|
|
registerActions(this);
|
2021-09-09 13:05:33 +08:00
|
|
|
}
|
2020-12-18 19:54:53 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
plugin<O = any>(pluginClass: any, options?: O): Plugin<O> {
|
|
|
|
return this.pm.add(pluginClass, options);
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:57:21 +08:00
|
|
|
use<NewStateT = {}, NewContextT = {}>(
|
|
|
|
middleware: Koa.Middleware<StateT & NewStateT, ContextT & NewContextT>,
|
|
|
|
options?: MiddlewareOptions,
|
2021-09-23 00:16:04 +08:00
|
|
|
) {
|
2021-09-16 15:57:21 +08:00
|
|
|
// @ts-ignore
|
|
|
|
return super.use(middleware);
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:23:34 +08:00
|
|
|
collection(options: CollectionOptions) {
|
|
|
|
return this.db.collection(options);
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
resource(options: ResourceOptions) {
|
|
|
|
return this.resourcer.define(options);
|
|
|
|
}
|
|
|
|
|
2021-09-16 15:57:21 +08:00
|
|
|
actions(handlers: any, options?: ActionsOptions) {
|
2021-09-16 00:38:48 +08:00
|
|
|
return this.resourcer.registerActions(handlers);
|
|
|
|
}
|
|
|
|
|
2022-02-07 21:54:07 +08:00
|
|
|
command(name: string, desc?: string, opts?: CommandOptions): Command {
|
|
|
|
return this.cli.command(name, desc, opts);
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
findCommand(name: string): Command {
|
2022-02-10 18:38:05 +08:00
|
|
|
return (<any>this.cli)._findCommand(name);
|
2021-10-18 12:49:37 +08:00
|
|
|
}
|
|
|
|
|
2021-09-14 11:09:26 +08:00
|
|
|
async load() {
|
2022-01-30 11:11:36 +08:00
|
|
|
await this.pm.load();
|
2021-09-14 11:09:26 +08:00
|
|
|
}
|
|
|
|
|
2022-01-24 14:10:35 +08:00
|
|
|
getPlugin<P extends Plugin>(name: string) {
|
2022-01-30 11:11:36 +08:00
|
|
|
return this.pm.get(name) as P;
|
2022-01-24 14:10:35 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
async parse(argv = process.argv) {
|
|
|
|
await this.load();
|
|
|
|
return this.cli.parseAsync(argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
async start(options?: StartOptions) {
|
|
|
|
// reconnect database
|
|
|
|
if (this.db.closed()) {
|
|
|
|
await this.db.reconnect();
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
2022-01-30 11:11:36 +08:00
|
|
|
|
|
|
|
await this.emitAsync('beforeStart', this, options);
|
|
|
|
|
|
|
|
if (options?.listen?.port) {
|
|
|
|
const listen = () =>
|
|
|
|
new Promise((resolve) => {
|
|
|
|
const Server = this.listen(options?.listen, () => {
|
|
|
|
resolve(Server);
|
|
|
|
});
|
2021-12-06 21:23:34 +08:00
|
|
|
});
|
2022-01-30 11:11:36 +08:00
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
this.listenServer = await listen();
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
await this.emitAsync('afterStart', this, options);
|
2021-12-06 21:23:34 +08:00
|
|
|
}
|
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
async stop(options?: any) {
|
|
|
|
await this.emitAsync('beforeStop', this, options);
|
2022-01-30 11:11:36 +08:00
|
|
|
|
|
|
|
// close database connection
|
|
|
|
await this.db.close();
|
|
|
|
|
|
|
|
// close http server
|
|
|
|
if (this.listenServer) {
|
|
|
|
const closeServer = () =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
this.listenServer.close((err) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
this.listenServer = null;
|
|
|
|
resolve(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
await closeServer();
|
|
|
|
}
|
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
await this.emitAsync('afterStop', this, options);
|
2020-12-18 19:54:53 +08:00
|
|
|
}
|
|
|
|
|
2022-02-07 01:14:00 +08:00
|
|
|
async destroy(options?: any) {
|
|
|
|
await this.emitAsync('beforeDestroy', this, options);
|
|
|
|
await this.stop(options);
|
|
|
|
await this.emitAsync('afterDestroy', this, options);
|
2022-01-30 11:11:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async install(options?: InstallOptions) {
|
2022-02-07 10:07:39 +08:00
|
|
|
await this.emitAsync('beforeInstall', this, options);
|
2022-02-28 21:49:50 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
if (options?.clean) {
|
|
|
|
await this.db.clean(isBoolean(options.clean) ? { drop: options.clean } : options.clean);
|
|
|
|
}
|
2022-02-28 21:49:50 +08:00
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
await this.db.sync(options?.sync);
|
2022-02-28 21:49:50 +08:00
|
|
|
await this.pm.install(options);
|
2022-01-30 11:11:36 +08:00
|
|
|
await this.emitAsync('afterInstall', this, options);
|
2021-09-09 13:05:33 +08:00
|
|
|
}
|
2022-01-30 11:11:36 +08:00
|
|
|
|
|
|
|
emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
2020-11-11 18:16:18 +08:00
|
|
|
}
|
|
|
|
|
2022-01-30 11:11:36 +08:00
|
|
|
applyMixins(Application, [AsyncEmitter]);
|
|
|
|
|
2020-11-11 18:16:18 +08:00
|
|
|
export default Application;
|