2022-03-28 22:01:10 +08:00
|
|
|
import { applyMixins, AsyncEmitter } from '@nocobase/utils';
|
2022-04-29 00:09:40 +08:00
|
|
|
import EventEmitter from 'events';
|
2022-04-30 23:41:01 +08:00
|
|
|
import http, { IncomingMessage, ServerResponse } from 'http';
|
2022-04-29 00:09:40 +08:00
|
|
|
import Application, { ApplicationOptions } from './application';
|
2022-03-28 22:01:10 +08:00
|
|
|
|
2023-03-10 19:16:00 +08:00
|
|
|
type AppSelectorReturn = Application | string | undefined | null;
|
|
|
|
|
|
|
|
type AppSelector = (req: IncomingMessage) => AppSelectorReturn | Promise<AppSelectorReturn>;
|
2022-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
export class AppManager extends EventEmitter {
|
|
|
|
public applications: Map<string, Application> = new Map<string, Application>();
|
|
|
|
|
2023-02-14 15:30:58 +08:00
|
|
|
constructor(public app: Application) {
|
2022-03-28 22:01:10 +08:00
|
|
|
super();
|
|
|
|
|
2023-03-10 19:16:00 +08:00
|
|
|
const passEventToSubApps = (eventName, method) => {
|
|
|
|
app.on(eventName, async (mainApp, options) => {
|
|
|
|
console.log(`receive event ${eventName} from ${mainApp.name}`);
|
|
|
|
for (const application of this.applications.values()) {
|
|
|
|
console.log(`pass ${eventName} to ${application.name} `);
|
|
|
|
await application[method](options);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2022-03-28 22:01:10 +08:00
|
|
|
|
2023-03-10 19:16:00 +08:00
|
|
|
passEventToSubApps('beforeDestroy', 'destroy');
|
|
|
|
passEventToSubApps('beforeStop', 'stop');
|
|
|
|
passEventToSubApps('afterUpgrade', 'upgrade');
|
|
|
|
passEventToSubApps('afterReload', 'reload');
|
2022-03-28 22:01:10 +08:00
|
|
|
}
|
|
|
|
|
2023-03-10 19:16:00 +08:00
|
|
|
appSelector: AppSelector = async (req: IncomingMessage) => this.app;
|
2022-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
createApplication(name: string, options: ApplicationOptions): Application {
|
2023-03-10 19:16:00 +08:00
|
|
|
const application = new Application({
|
|
|
|
...options,
|
|
|
|
name,
|
|
|
|
});
|
|
|
|
|
2022-03-28 22:01:10 +08:00
|
|
|
this.applications.set(name, application);
|
|
|
|
return application;
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeApplication(name: string) {
|
|
|
|
const application = this.applications.get(name);
|
|
|
|
if (!application) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await application.destroy();
|
|
|
|
|
|
|
|
this.applications.delete(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
setAppSelector(selector: AppSelector) {
|
|
|
|
this.appSelector = selector;
|
|
|
|
}
|
|
|
|
|
|
|
|
listen(...args) {
|
|
|
|
const server = http.createServer(this.callback());
|
|
|
|
return server.listen(...args);
|
|
|
|
}
|
|
|
|
|
2023-02-18 09:27:54 +08:00
|
|
|
async getApplication(appName: string, options = {}): Promise<null | Application> {
|
2022-03-28 22:01:10 +08:00
|
|
|
await this.emitAsync('beforeGetApplication', {
|
|
|
|
appManager: this,
|
|
|
|
name: appName,
|
2023-02-18 09:27:54 +08:00
|
|
|
options,
|
2022-03-28 22:01:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return this.applications.get(appName);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback() {
|
2022-04-30 23:41:01 +08:00
|
|
|
return async (req: IncomingMessage, res: ServerResponse) => {
|
2023-02-14 15:30:58 +08:00
|
|
|
const appManager = this.app.appManager;
|
|
|
|
|
2023-03-10 19:16:00 +08:00
|
|
|
let handleApp: any = (await appManager.appSelector(req)) || appManager.app;
|
2022-03-28 22:01:10 +08:00
|
|
|
|
|
|
|
if (typeof handleApp === 'string') {
|
2023-02-14 15:30:58 +08:00
|
|
|
handleApp = await appManager.getApplication(handleApp);
|
|
|
|
|
2022-04-30 23:41:01 +08:00
|
|
|
if (!handleApp) {
|
|
|
|
res.statusCode = 404;
|
|
|
|
return res.end(
|
|
|
|
JSON.stringify({
|
|
|
|
redirectTo: process.env.APP_NOT_FOUND_REDIRECT_TO,
|
|
|
|
errors: [
|
|
|
|
{
|
2023-02-14 15:30:58 +08:00
|
|
|
message: 'Application Not Found',
|
2022-04-30 23:41:01 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2022-03-28 22:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleApp.callback()(req, res);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-19 00:40:55 +08:00
|
|
|
declare emitAsync: (event: string | symbol, ...args: any[]) => Promise<boolean>;
|
2022-03-28 22:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
applyMixins(AppManager, [AsyncEmitter]);
|