UNPKG

@ticatec/common-express-server

Version:

A comprehensive TypeScript library providing common classes, controllers, and middleware for building scalable Express.js applications with multi-tenant support.

126 lines (125 loc) 4.1 kB
import { Express } from 'express'; import CommonRouterHelper from "./CommonRouterHelper"; import http from "http"; import { Logger } from "log4js"; /** * Function signature for module loader */ export type moduleLoader = () => Promise<any>; /** * Abstract base server class providing common functionality for Express servers * @template T The type of CommonRouterHelper this server uses */ export default abstract class BaseServer<T extends CommonRouterHelper> { /** Logger instance for this server */ protected logger: Logger; /** Router helper instance */ protected helper: T; /** Context root path for the server */ protected contextRoot: string; /** * Protected constructor for base server * @protected */ protected constructor(); /** * Gets the router helper instance for this server * @returns RouterHelper instance * @protected * @abstract */ protected abstract getHelper(): T; /** * Loads configuration file * @returns Promise that resolves when configuration is loaded * @protected * @abstract */ protected abstract loadConfigFile(): Promise<void>; /** * Writes the listening port to check.dat file * @param port The port number to write * @param fileName The file name to write to (default: './check.dat') * @protected */ protected writeCheckFile(port: number, fileName?: string): void; /** * Starts up the server * @returns Promise that resolves when server startup is complete */ startup(): Promise<void>; /** * Interceptor function called after web server is created * @param server The HTTP server instance * @returns Promise that resolves when post-creation setup is complete * @protected */ protected postServerCreated(server: http.Server): Promise<void>; /** * Gets web configuration * @returns Web configuration object * @protected * @abstract */ protected abstract getWebConf(): any; /** * Interceptor function called before startup * @returns Promise that resolves when pre-startup setup is complete * @protected */ protected beforeStart(): Promise<void>; /** * Gets the health check endpoint path * @returns The health check path * @protected */ protected getHealthCheckPath(): string; /** * Adds health check endpoint to the Express app * @param app Express application instance * @protected */ protected addHealthCheck(app: Express): void; /** * Starts the web server with given configuration * @param webConf Web server configuration * @returns Promise that resolves to the HTTP server instance * @protected */ protected startWebServer(webConf: any): Promise<unknown>; /** * Binds static site resources * @param app Express application instance * @returns Promise that resolves when static binding is complete * @protected */ protected bindStaticSite(app: Express): Promise<void>; /** * Initializes Express application * @param app Express application instance * @protected */ protected setupExpress(app: Express): void; /** * Binds a route to the Express application * @param app Express application instance * @param path The route path * @param loader Module loader function that returns the route class * @returns Promise that resolves when route binding is complete * @protected */ protected bindRoutes(app: Express, path: string, loader: moduleLoader): Promise<void>; /** * Sets up routes for the application * @param app Express application instance * @returns Promise that resolves when all routes are set up * @protected * @abstract */ protected abstract setupRoutes(app: Express): Promise<void>; /** * Static method to start a server instance * @param server The server instance to start */ static startup(server: BaseServer<any>): void; }