import * as plugins from '../plugins.js'; import type { TsView } from '../tsview.classes.tsview.js'; import { registerS3Handlers } from '../api/handlers.s3.js'; import { registerMongoHandlers } from '../api/handlers.mongodb.js'; import { files as bundledUiFiles } from '../bundled_ui.js'; /** * Web server for TsView that serves the bundled UI and API endpoints. */ export class ViewServer { private tsview: TsView; private port: number; private typedServer: plugins.typedserver.TypedServer | null = null; public typedrouter: plugins.typedrequest.TypedRouter; constructor(tsview: TsView, port: number) { this.tsview = tsview; this.port = port; this.typedrouter = new plugins.typedrequest.TypedRouter(); } /** * Start the server */ public async start(): Promise { // Register API handlers if (this.tsview.config.hasS3()) { await registerS3Handlers(this.typedrouter, this.tsview); } if (this.tsview.config.hasMongo()) { await registerMongoHandlers(this.typedrouter, this.tsview); } // Create typed server with bundled content this.typedServer = new plugins.typedserver.TypedServer({ cors: true, port: this.port, bundledContent: bundledUiFiles, spaFallback: true, }); // Add the router this.typedServer.typedrouter.addTypedRouter(this.typedrouter); // Start server await this.typedServer.start(); } /** * Stop the server */ public async stop(): Promise { if (this.typedServer) { await this.typedServer.stop(); this.typedServer = null; } } }