( port: number, endpoint: string, handlers: RequestHandlers )
| 113 | * Creates a base HTTP server with common functionality |
| 114 | */ |
| 115 | export function createBaseHttpServer( |
| 116 | port: number, |
| 117 | endpoint: string, |
| 118 | handlers: RequestHandlers |
| 119 | ): http.Server { |
| 120 | const httpServer = http.createServer(async (req, res) => { |
| 121 | // Handle CORS for all requests |
| 122 | handleCORS(req, res); |
| 123 | |
| 124 | // Handle OPTIONS requests |
| 125 | if (req.method === 'OPTIONS') { |
| 126 | res.writeHead(204).end(); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | // Handle common endpoints like health and ping |
| 131 | if (handleCommonEndpoints(req, res)) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | // Pass remaining requests to the specific handler |
| 136 | try { |
| 137 | await handlers.handleRequest(req, res); |
| 138 | } catch (error) { |
| 139 | console.error(`Error in ${handlers.serverType} request handler:`, error); |
| 140 | res.writeHead(500).end('Internal Server Error'); |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | // Set up cleanup handlers |
| 145 | setupCleanupHandlers(httpServer, handlers.cleanup); |
| 146 | |
| 147 | // Start listening and log server info |
| 148 | httpServer.listen(port, () => { |
| 149 | logServerStartup(handlers.serverType, port, endpoint); |
| 150 | }); |
| 151 | |
| 152 | return httpServer; |
| 153 | } |
no test coverage detected