()
| 344 | }); |
| 345 | |
| 346 | const main = async () => { |
| 347 | logger.info("Type System initialized"); |
| 348 | |
| 349 | // Request ID generator |
| 350 | const nanoid = customAlphabet( |
| 351 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", |
| 352 | 14, |
| 353 | ); |
| 354 | |
| 355 | // Configure the StatsD client for reporting metrics |
| 356 | let statsd: StatsD | undefined; |
| 357 | try { |
| 358 | if (isStatsDEnabled) { |
| 359 | const statsdHost = process.env.STATSD_HOST; |
| 360 | const statsdPort = Number.parseInt(process.env.STATSD_PORT || "8125", 10); |
| 361 | await waitOnResource(`tcp:${statsdHost}:${statsdPort}`, logger); |
| 362 | |
| 363 | statsd = new StatsD({ |
| 364 | host: statsdHost, |
| 365 | port: statsdPort, |
| 366 | }); |
| 367 | shutdown.addCleanup("StatsD", async () => { |
| 368 | // eslint-disable-next-line @typescript-eslint/unbound-method |
| 369 | await promisify((statsd as StatsD).close).bind(statsd)(); |
| 370 | }); |
| 371 | } |
| 372 | } catch (error) { |
| 373 | logger.error("Could not start StatsD client", { error }); |
| 374 | } |
| 375 | |
| 376 | app.use(cors(CORS_CONFIG)); |
| 377 | |
| 378 | if (isProdEnv && !isSelfHostedInstance) { |
| 379 | /** |
| 380 | * In production, hosted HASH, take the client IP from the Cloudflare-set header. |
| 381 | */ |
| 382 | Object.defineProperty(app.request, "ip", { |
| 383 | configurable: true, |
| 384 | enumerable: true, |
| 385 | get() { |
| 386 | return this.get("cf-connecting-ip"); |
| 387 | }, |
| 388 | }); |
| 389 | } |
| 390 | |
| 391 | // Add logging of requests. /graphql is logged at the operation level |
| 392 | // (query/mutation name) by the Apollo plugin in `create-apollo-server.ts`, |
| 393 | // so logging it here would be a less informative duplicate. |
| 394 | app.use((req, res, next) => { |
| 395 | const requestId = nanoid(); |
| 396 | res.set("x-hash-request-id", requestId); |
| 397 | |
| 398 | if (req.path !== "/graphql") { |
| 399 | logger.info(`${req.method} ${req.path}`, { |
| 400 | requestId, |
| 401 | origin: req.headers.origin, |
| 402 | ip: req.ip, |
| 403 | userAgent: req.headers["user-agent"], |
no test coverage detected