2022-04-30 23:41:01 +08:00
|
|
|
import http, { IncomingMessage, ServerResponse } from 'http';
|
2023-03-19 23:40:42 +08:00
|
|
|
import Application 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
|
|
|
|
2023-03-19 23:40:42 +08:00
|
|
|
export class AppManager {
|
2022-03-28 22:01:10 +08:00
|
|
|
public applications: Map<string, Application> = new Map<string, Application>();
|
2023-03-14 13:10:15 +08:00
|
|
|
public app: Application;
|
2022-03-28 22:01:10 +08:00
|
|
|
|
2023-03-14 13:10:15 +08:00
|
|
|
constructor(app: Application) {
|
|
|
|
this.bindMainApplication(app);
|
|
|
|
}
|
2022-03-28 22:01:10 +08:00
|
|
|
|
2023-03-14 13:10:15 +08:00
|
|
|
bindMainApplication(mainApp: Application) {
|
|
|
|
this.app = mainApp;
|
2023-03-10 19:16:00 +08:00
|
|
|
const passEventToSubApps = (eventName, method) => {
|
2023-03-14 13:10:15 +08:00
|
|
|
mainApp.on(eventName, async (mainApp, options) => {
|
2023-03-10 19:16:00 +08:00
|
|
|
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');
|
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
|
|
|
|
2023-03-19 23:40:42 +08:00
|
|
|
addSubApp(application): Application {
|
|
|
|
this.applications.set(application.name, application);
|
|
|
|
this.app.emit('afterSubAppAdded', application);
|
2022-03-28 22:01:10 +08:00
|
|
|
return application;
|
|
|
|
}
|
|
|
|
|
|
|
|
async removeApplication(name: string) {
|
|
|
|
const application = this.applications.get(name);
|
|
|
|
if (!application) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await application.destroy();
|
|
|
|
|
2023-03-19 23:40:42 +08:00
|
|
|
console.log(`remove application ${name}`);
|
2022-03-28 22:01:10 +08:00
|
|
|
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> {
|
2023-03-19 23:40:42 +08:00
|
|
|
await this.app.emitAsync('beforeGetApplication', {
|
2022-03-28 22:01:10 +08:00
|
|
|
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
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2023-03-19 23:40:42 +08:00
|
|
|
|
|
|
|
if (handleApp.stopped) await handleApp.start();
|
2022-03-28 22:01:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleApp.callback()(req, res);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|