UNPKG

@fdm-monster/server

Version:

FDM Monster is a bulk OctoPrint, Klipper, PrusaLink and BambuLab manager to set up, configure and monitor 3D printers. Our aim is to provide neat overview over your farm.

68 lines (67 loc) 3.31 kB
import { NotFoundException } from "./exceptions/runtime.exceptions.js"; import { AppConstants } from "./server.constants.js"; import { getMediaPath, superRootPath } from "./utils/fs.utils.js"; import { fetchServerPort } from "./server.env.js"; import { loadControllersFunc } from "./shared/load-controllers.js"; import { setupSwagger } from "./utils/swagger/swagger.js"; import { join } from "node:path"; import express from "express"; import history from "connect-history-api-fallback"; //#region src/server.host.ts var ServerHost = class ServerHost { logger; constructor(loggerFactory, configService, bootTask, socketIoGateway, typeormService, exceptionFilter) { this.configService = configService; this.bootTask = bootTask; this.socketIoGateway = socketIoGateway; this.typeormService = typeormService; this.exceptionFilter = exceptionFilter; this.logger = loggerFactory(ServerHost.name); } async boot(app, quick_boot = false, listenRequests = true) { await this.serveControllerRoutes(app); if (!quick_boot) await this.bootTask.runOnce(); if (listenRequests) return this.httpListen(app); } hasConnected() { return this.typeormService.hasConnected(); } async serveControllerRoutes(app) { const swaggerDisabled = process.env[AppConstants.DISABLE_SWAGGER_OPENAPI] === "true"; app.use((req, res, next) => { if (!req.originalUrl.startsWith("/metrics") && !req.originalUrl.startsWith("/api") && !req.originalUrl.startsWith("/api-docs") && !req.originalUrl.startsWith("/socket.io")) history()(req, res, next); else next(); }).use(await loadControllersFunc()); if (swaggerDisabled) this.logger.log("Swagger/OpenAPI documentation disabled"); else { await setupSwagger(app, this.logger); this.logger.log("Swagger/OpenAPI documentation enabled"); } const bundleDistPath = join(getMediaPath(), AppConstants.defaultClientBundleStorage, "dist"); const backupClientPath = join(superRootPath(), "node_modules", AppConstants.clientPackageName, "dist"); app.use(express.static(bundleDistPath)); app.use(express.static(backupClientPath)); app.get("*", (req, _) => { const path = req.originalUrl; let resource = "MVC"; if (path.startsWith("/socket.io") || path.startsWith("/api") || path.startsWith("/metrics") || path.startsWith("/api-docs")) resource = "API"; else if (path.endsWith(".min.js")) resource = "client-bundle"; this.logger.error(`${resource} resource at '${path}' was not found`); if (!path.startsWith("/socket.io")) throw new NotFoundException(`${resource} resource was not found`, path); }); app.use(this.exceptionFilter.handle.bind(this.exceptionFilter)); } async httpListen(app) { const port = fetchServerPort(); if (!port || Number.isNaN(Number.parseInt(port))) throw new Error("The FDM Server requires a numeric port input argument to run"); const swaggerDisabled = process.env[AppConstants.DISABLE_SWAGGER_OPENAPI] === "true"; const server = app.listen(Number.parseInt(port), "0.0.0.0", () => { this.logger.log(`Server started... open it at http://127.0.0.1:${port}`); if (!swaggerDisabled) this.logger.log(`API Documentation available at http://127.0.0.1:${port}/api-docs`); }); this.socketIoGateway.attachServer(server); } }; //#endregion export { ServerHost }; //# sourceMappingURL=server.host.js.map