Files
tsview/ts/server/classes.viewserver.ts

58 lines
1.6 KiB
TypeScript

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<void> {
// Create typed server with bundled content
this.typedServer = new plugins.typedserver.TypedServer({
cors: true,
port: this.port,
bundledContent: bundledUiFiles,
spaFallback: true,
noCache: true,
});
// Register API handlers directly to server's router
if (this.tsview.config.hasS3()) {
await registerS3Handlers(this.typedServer.typedrouter, this.tsview);
}
if (this.tsview.config.hasMongo()) {
await registerMongoHandlers(this.typedServer.typedrouter, this.tsview);
}
// Start server
await this.typedServer.start();
}
/**
* Stop the server
*/
public async stop(): Promise<void> {
if (this.typedServer) {
await this.typedServer.stop();
this.typedServer = null;
}
}
}