@fdm-monster/server
Version:
FDM Monster is a bulk OctoPrint manager to set up, configure and monitor 3D printers. Our aim is to provide extremely optimized websocket performance and reliability.
119 lines (118 loc) • 5.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "ServerHost", {
enumerable: true,
get: function() {
return ServerHost;
}
});
const _express = /*#__PURE__*/ _interop_require_default(require("express"));
const _mongoose = /*#__PURE__*/ _interop_require_default(require("mongoose"));
const _connecthistoryapifallback = /*#__PURE__*/ _interop_require_default(require("connect-history-api-fallback"));
const _path = require("path");
const _exceptionfilter = require("./middleware/exception.filter");
const _serverenv = require("./server.env");
const _runtimeexceptions = require("./exceptions/runtime.exceptions");
const _serverconstants = require("./server.constants");
const _fsutils = require("./utils/fs.utils");
const _envutils = require("./utils/env.utils");
const _loadcontrollers = require("./shared/load-controllers");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
class ServerHost {
configService;
isTypeormMode;
settingsStore;
bootTask;
socketIoGateway;
typeormService;
logger;
constructor(loggerFactory, configService, isTypeormMode, settingsStore, bootTask, socketIoGateway, typeormService){
this.configService = configService;
this.isTypeormMode = isTypeormMode;
this.settingsStore = settingsStore;
this.bootTask = bootTask;
this.socketIoGateway = socketIoGateway;
this.typeormService = typeormService;
this.logger = loggerFactory(ServerHost.name);
}
async boot(app, quick_boot = false, listenRequests = true) {
if (!this.isTypeormMode) {
_mongoose.default.set("strictQuery", true);
}
this.serveControllerRoutes(app);
if (!quick_boot) {
await this.bootTask.runOnce();
}
if (listenRequests) return this.httpListen(app);
}
hasConnected() {
if (this.isTypeormMode) {
return this.typeormService.hasConnected();
} else {
return _mongoose.default.connections[0].readyState;
}
}
serveControllerRoutes(app) {
app.use((req, res, next)=>{
if (!req.originalUrl.startsWith("/metrics") && !req.originalUrl.startsWith("/api") && !req.originalUrl.startsWith("/socket.io")) {
(0, _connecthistoryapifallback.default)()(req, res, next);
} else {
next();
}
}).use((0, _loadcontrollers.loadControllersFunc)());
const nextClientPath = (0, _path.join)((0, _fsutils.superRootPath)(), "node_modules", _serverconstants.AppConstants.clientNextPackageName, "dist");
const bundleDistPath = (0, _path.join)((0, _fsutils.superRootPath)(), _serverconstants.AppConstants.defaultClientBundleStorage, "dist");
const backupClientPath = (0, _path.join)((0, _fsutils.superRootPath)(), "node_modules", _serverconstants.AppConstants.clientPackageName, "dist");
app.use((req, res, next)=>{
if (this.isClientNextEnabled()) {
_express.default.static(nextClientPath)(req, res, next);
} else {
next();
}
});
app.use(_express.default.static(bundleDistPath));
app.use(_express.default.static(backupClientPath));
app.get("*", (req, _)=>{
const path = req.originalUrl;
let resource = "MVC";
if (path.startsWith("/socket.io") || path.startsWith("/api") || path.startsWith("/metrics")) {
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 _runtimeexceptions.NotFoundException(`${resource} resource was not found`, path);
}
});
app.use(_exceptionfilter.exceptionFilter);
}
async httpListen(app) {
const port = (0, _serverenv.fetchServerPort)();
if (!(0, _envutils.isProductionEnvironment)() && this.configService.get(_serverconstants.AppConstants.debugRoutesKey, "false") === "true") {
const expressListRoutes = require("express-list-routes");
expressListRoutes(app, {
prefix: "/"
});
}
if (!port || Number.isNaN(parseInt(port))) {
throw new Error("The FDM Server requires a numeric port input argument to run");
}
const hostOrFqdn = "0.0.0.0";
const server = app.listen(parseInt(port), hostOrFqdn, ()=>{
this.logger.log(`Server started... open it at http://127.0.0.1:${port}`);
});
this.socketIoGateway.attachServer(server);
}
isClientNextEnabled() {
const settings = this.settingsStore.getServerSettings();
return settings.experimentalClientSupport;
}
}
//# sourceMappingURL=server.host.js.map