Move app routes into separate function

Reduce the _startupInternal complexity by moving out the express app
route logic into a separate function.
This commit is contained in:
Hornwitser committed 2021-05-12 20:42:52 +02:00
1 parent d3ea85d5ac
commit 8b857224dc
1 file changed
+31 -27
+31 -27
View File
@@ -170,33 +170,7 @@ class Master {
);
}
this.app.use((req, res, next) => {
hitCounter.inc();
next();
});
this.app.use(expressFileupload());
this.app.use(compression());
// Set folder to serve static content from (the website)
this.app.use(express.static(path.join(__dirname, "..", "dist", "web")));
this.app.use(express.static("static")); // Used for data export files
// Add API routes
routes.addRouteHandlers(this.app);
// Add routes for the web interface
for (let route of routes.webRoutes) {
this.app.get(route, Master.serveWeb(route));
}
for (let pluginInfo of this.pluginInfos) {
for (let route of pluginInfo.routes || []) {
this.app.get(route, Master.serveWeb(route));
}
let pluginPackagePath = require.resolve(path.posix.join(pluginInfo.requirePath, "package.json"));
let webPath = path.join(path.dirname(pluginPackagePath), "dist", "web");
this.app.use(`/plugins/${pluginInfo.name}`, express.static(webPath));
}
Master.addAppRoutes(this.app, this.pluginInfos);
// Load plugins
await this.loadPlugins();
@@ -352,6 +326,36 @@ class Master {
await fs.outputFile(filePath, JSON.stringify(serialized, null, 4));
}
static addAppRoutes(app, pluginInfos) {
app.use((req, res, next) => {
hitCounter.inc();
next();
});
app.use(expressFileupload());
app.use(compression());
// Set folder to serve static content from (the website)
app.use(express.static(path.join(__dirname, "..", "dist", "web")));
app.use(express.static("static")); // Used for data export files
// Add API routes
routes.addRouteHandlers(app);
// Add routes for the web interface
for (let route of routes.webRoutes) {
app.get(route, Master.serveWeb(route));
}
for (let pluginInfo of pluginInfos) {
for (let route of pluginInfo.routes || []) {
app.get(route, Master.serveWeb(route));
}
let pluginPackagePath = require.resolve(path.posix.join(pluginInfo.requirePath, "package.json"));
let webPath = path.join(path.dirname(pluginPackagePath), "dist", "web");
app.use(`/plugins/${pluginInfo.name}`, express.static(webPath));
}
}
async loadPlugins() {
for (let pluginInfo of this.pluginInfos) {
if (!this.config.group(pluginInfo.name).get("load_plugin")) {