(enableDashboard: boolean = false)
| 110 | |
| 111 | function createExpressApp( |
| 112 | enableDashboard: boolean = false, |
| 113 | mcpHttpAuthToken?: string |
| 114 | ): express.Application { |
| 115 | const app = express(); |
| 116 | |
| 117 | if (mcpHttpAuthToken) { |
| 118 | app.all('/mcp', createMcpHttpAuthMiddleware(mcpHttpAuthToken)); |
| 119 | } |
| 120 | |
| 121 | app.use(express.json()); |
| 122 | |
| 123 | const env = getEnvironment(); |
| 124 | const normalizeHostForHeader = (host: string): string => { |
| 125 | const trimmedHost = host.trim(); |
| 126 | if (trimmedHost.startsWith('[') && trimmedHost.endsWith(']')) { |
| 127 | return trimmedHost; |
| 128 | } |
| 129 | return isIP(trimmedHost) === 6 ? `[${trimmedHost}]` : trimmedHost; |
| 130 | }; |
| 131 | const loopbackHosts = ['localhost', '127.0.0.1', '[::1]']; |
| 132 | const normalizedHost = normalizeHostForHeader(env.MCP_HOST); |
| 133 | const allowList = loopbackHosts.includes(normalizedHost) ? loopbackHosts : [normalizedHost]; |
| 134 | if (env.MCP_HOST !== '0.0.0.0' && env.MCP_HOST !== '::') { |
| 135 | app.use(hostHeaderValidation(allowList)); |
| 136 | } |
| 137 | |
| 138 | // Health check endpoint |
| 139 | app.get('/health', async (req, res) => { |
| 140 | try { |
| 141 | // Perform health check |
| 142 | const healthCheckResult = await healthCheck(client); |
| 143 | |
| 144 | // Check if Document Engine API is operational |
| 145 | const isHealthy = !healthCheckResult.markdown.includes('❌ Error'); |
| 146 | |
| 147 | // Return appropriate status code based on health check result |
| 148 | if (isHealthy) { |
| 149 | res.status(200).json({ |
| 150 | status: 'operational', |
| 151 | message: 'All systems operational', |
| 152 | }); |
| 153 | } else { |
| 154 | res.status(503).json({ |
| 155 | status: 'degraded', |
| 156 | message: 'Document Engine API connection error', |
| 157 | }); |
| 158 | } |
| 159 | } catch (error) { |
| 160 | logger.error(null, 'Health check endpoint error', { error }); |
| 161 | res.status(500).json({ |
| 162 | status: 'error', |
| 163 | message: error instanceof Error ? error.message : 'Unknown error', |
| 164 | }); |
| 165 | } |
| 166 | }); |
| 167 | |
| 168 | // Mount dashboard routes only if enabled |
| 169 | if (enableDashboard) { |
no test coverage detected