2021-09-16 00:38:48 +08:00
|
|
|
import { registerActions } from '@nocobase/actions';
|
2022-01-30 11:11:36 +08:00
|
|
|
import Database, { CleanOptions, CollectionOptions, DatabaseOptions, SyncOptions } from '@nocobase/database';
|
|
|
|
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';
|
|
|
|
import { createCli, createDatabase, createI18n, createResourcer, registerMiddlewares } from './helper';
|
|
|
|
import { Plugin } from './plugin';
|
|
|
|
import { PluginManager } 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 {
|
2021-09-09 13:05:33 +08:00
|
|
|
database?: DatabaseOptions;
|
|
|
|
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 {
|
|
|
|
listen?: ListenOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface InstallOptions {
|
|
|
|
clean?: CleanOptions | boolean;
|
|
|
|
sync?: SyncOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-14 11:09:26 +08:00
|
|
|
command(nameAndArgs: string, opts?: CommandOptions) {
|
|
|
|
return this.cli.command(nameAndArgs, opts);
|
|
|
|
}
|
|
|
|
|
2021-10-18 12:49:37 +08:00
|
|
|
findCommand(name: string): Command {
|
|
|
|
return (this.cli as any)._findCommand(name);
|
|
|
|
}
|
|
|
|
|
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-01-30 11:11:36 +08:00
|
|
|
async stop() {
|
|
|
|
await this.emitAsync('beforeStop', this);
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.emitAsync('afterStop', this);
|
2020-12-18 19:54:53 +08:00
|
|
|
}
|
|
|
|
|
2021-09-09 13:05:33 +08:00
|
|
|
async destroy() {
|
2022-01-30 11:11:36 +08:00
|
|
|
await this.emitAsync('beforeDestroy', this);
|
|
|
|
await this.stop();
|
|
|
|
await this.emitAsync('afterDestroy', this);
|
|
|
|
}
|
|
|
|
|
|
|
|
async install(options?: InstallOptions) {
|
|
|
|
if (options?.clean) {
|
|
|
|
await this.db.clean(isBoolean(options.clean) ? { drop: options.clean } : options.clean);
|
|
|
|
}
|
|
|
|
await this.db.sync(options?.sync);
|
|
|
|
await this.emitAsync('beforeInstall', this, options);
|
|
|
|
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;
|