tachybase_todo/packages/core/server/src/app-manager.ts
ChengLei Shao ca95edf295
refactor: multi-app (#1578)
* feat: compact theme

* fix: theme

* fix: styling

* fix: margin

* feat: improve

* fix: remove console.log

* test: enable plugin test

* refactor: multi app

* test: lazy load sync plugin

* test: lazy load test

* fix: beforeGetApplication Event

* feat: loadFromDatabase options in traverseSubApps

* fix: test

* fix: multi app manager test

* chore: test

* test: should upgrade sub apps when main app upgrade

* feat: plugin require check

* chore: yarn.lock

* fix: sql typo

* feat: share collections

* fix: record name

* test: belongs to many repository

* fix: belongs to many with targetKey alias

* fix: extend collection error

* fix: transaction error

* feat: collection graph

* fix: update options in collection

* chore: collections graph

* chore: export uitls

* feat: connected nodes method in collections graph

* feat: exclude params in connected nodes

* chore: sub app collection list params

* fix: collections graph

* feat: syncToApps migration

* fix:  translation

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2023-03-19 23:40:42 +08:00

100 lines
2.7 KiB
TypeScript

import http, { IncomingMessage, ServerResponse } from 'http';
import Application from './application';
type AppSelectorReturn = Application | string | undefined | null;
type AppSelector = (req: IncomingMessage) => AppSelectorReturn | Promise<AppSelectorReturn>;
export class AppManager {
public applications: Map<string, Application> = new Map<string, Application>();
public app: Application;
constructor(app: Application) {
this.bindMainApplication(app);
}
bindMainApplication(mainApp: Application) {
this.app = mainApp;
const passEventToSubApps = (eventName, method) => {
mainApp.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);
}
});
};
passEventToSubApps('beforeDestroy', 'destroy');
passEventToSubApps('beforeStop', 'stop');
}
appSelector: AppSelector = async (req: IncomingMessage) => this.app;
addSubApp(application): Application {
this.applications.set(application.name, application);
this.app.emit('afterSubAppAdded', application);
return application;
}
async removeApplication(name: string) {
const application = this.applications.get(name);
if (!application) {
return;
}
await application.destroy();
console.log(`remove application ${name}`);
this.applications.delete(name);
}
setAppSelector(selector: AppSelector) {
this.appSelector = selector;
}
listen(...args) {
const server = http.createServer(this.callback());
return server.listen(...args);
}
async getApplication(appName: string, options = {}): Promise<null | Application> {
await this.app.emitAsync('beforeGetApplication', {
appManager: this,
name: appName,
options,
});
return this.applications.get(appName);
}
callback() {
return async (req: IncomingMessage, res: ServerResponse) => {
const appManager = this.app.appManager;
let handleApp: any = (await appManager.appSelector(req)) || appManager.app;
if (typeof handleApp === 'string') {
handleApp = await appManager.getApplication(handleApp);
if (!handleApp) {
res.statusCode = 404;
return res.end(
JSON.stringify({
redirectTo: process.env.APP_NOT_FOUND_REDIRECT_TO,
errors: [
{
message: 'Application Not Found',
},
],
}),
);
}
if (handleApp.stopped) await handleApp.start();
}
handleApp.callback()(req, res);
};
}
}