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.

102 lines (101 loc) 4.98 kB
import { DITokens } from "../container.tokens.js"; import { AppConstants } from "../server.constants.js"; import { ServerTasks } from "../tasks.js"; //#region src/tasks/boot.task.ts var BootTask = class BootTask { logger; constructor(loggerFactory, taskManagerService, settingsStore, multerService, printerSocketStore, permissionService, roleService, userService, floorStore, configService, typeormService, printerThumbnailCache, printFileDownloaderService, fileStorageService) { this.taskManagerService = taskManagerService; this.settingsStore = settingsStore; this.multerService = multerService; this.printerSocketStore = printerSocketStore; this.permissionService = permissionService; this.roleService = roleService; this.userService = userService; this.floorStore = floorStore; this.configService = configService; this.typeormService = typeormService; this.printerThumbnailCache = printerThumbnailCache; this.printFileDownloaderService = printFileDownloaderService; this.fileStorageService = fileStorageService; this.logger = loggerFactory(BootTask.name); } async runOnce() { this.taskManagerService.registerJobOrTask(ServerTasks.SERVER_BOOT_TASK); this.logger.log("Running boot task once."); await this.run(); } async run() { await this.typeormService.createConnection(); this.logger.log("Ensuring file storage directories exist"); await this.fileStorageService.ensureStorageDirectories(); this.logger.log("Loading and synchronizing Server Settings"); await this.settingsStore.loadSettings(); this.logger.log("Synchronizing user permission and roles definition"); await this.permissionService.syncPermissions(); await this.roleService.syncRoles(); if (this.configService.isDemoMode()) { this.logger.warn(`Starting in demo mode due to ${AppConstants.OVERRIDE_IS_DEMO_MODE}`); await this.createOrUpdateDemoAccount(); this.logger.warn(`Setting loginRequired=true and registration=false due to ${AppConstants.OVERRIDE_IS_DEMO_MODE}`); await this.settingsStore.setLoginRequired(true); await this.settingsStore.setRegistrationEnabled(false); } else { const loginRequired = this.configService.get(AppConstants.OVERRIDE_LOGIN_REQUIRED, null); if (loginRequired !== null) { this.logger.warn(`Setting login required due to ${AppConstants.OVERRIDE_LOGIN_REQUIRED}`); await this.settingsStore.setLoginRequired(loginRequired === "true"); } const registrationEnabled = this.configService.get(AppConstants.OVERRIDE_REGISTRATION_ENABLED, null); if (registrationEnabled !== null) { this.logger.warn(`Setting registration enabled due to ${AppConstants.OVERRIDE_REGISTRATION_ENABLED}`); await this.settingsStore.setRegistrationEnabled(registrationEnabled === "true"); } } const overrideJwtSecret = this.configService.get(AppConstants.OVERRIDE_JWT_SECRET); const overrideJwtExpiresIn = this.configService.get(AppConstants.OVERRIDE_JWT_EXPIRES_IN); await this.settingsStore.persistOptionalCredentialSettings(overrideJwtSecret, overrideJwtExpiresIn); this.logger.log("Clearing upload folder"); this.multerService.clearUploadsFolder(); this.logger.log("Loading printer sockets"); await this.printerSocketStore.loadPrinterSockets(); this.logger.log("Loading floor store"); await this.floorStore.loadStore(); this.logger.log("Loading printer thumbnail cache"); await this.printerThumbnailCache.loadCache(); const length = await this.printerThumbnailCache.getAllValues(); this.logger.log(`Loaded ${length.length} thumbnail(s)`); if (process.env.SAFEMODE_ENABLED === "true") this.logger.warn("Starting in safe mode due to SAFEMODE_ENABLED"); else ServerTasks.BOOT_TASKS.forEach((task) => { this.taskManagerService.registerJobOrTask(task); }); this.taskManagerService.disableJob(DITokens.bootTask, false); } async createOrUpdateDemoAccount() { const demoUsername = this.configService.get(AppConstants.OVERRIDE_DEMO_USERNAME, AppConstants.DEFAULT_DEMO_USERNAME); const demoPassword = this.configService.get(AppConstants.OVERRIDE_DEMO_PASSWORD, AppConstants.DEFAULT_DEMO_PASSWORD); const demoRole = this.configService.get(AppConstants.OVERRIDE_DEMO_ROLE, AppConstants.DEFAULT_DEMO_ROLE); const demoUserId = await this.userService.getDemoUserId(); if (!demoUserId) { await this.userService.register({ username: demoUsername, password: demoPassword, isDemoUser: true, isVerified: true, isRootUser: false, needsPasswordChange: false, roles: [demoRole] }); this.logger.log("Created demo account"); } else { await this.userService.setVerifiedById(demoUserId, true); await this.userService.setIsRootUserById(demoUserId, false); await this.userService.updatePasswordUnsafeByUsername(demoUsername, demoPassword); await this.userService.setUserRoles(demoUserId, [demoRole]); this.logger.log("Updated demo account"); } } }; //#endregion export { BootTask }; //# sourceMappingURL=boot.task.js.map