Skip to content

Commit

Permalink
Log requests and responses handled by Effect
Browse files Browse the repository at this point in the history
Refs #1834
  • Loading branch information
thewilkybarkid committed Sep 25, 2024
1 parent 8b3e5e0 commit e58d024
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/ExpressHttpApp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type HttpApp, HttpServerRequest, HttpServerResponse } from '@effect/platform'
import { type HttpApp, HttpMiddleware, HttpServerRequest, HttpServerResponse } from '@effect/platform'
import { NodeHttpServerRequest } from '@effect/platform-node'
import { Effect } from 'effect'
import type { ErrorRequestHandler } from 'express'
Expand All @@ -22,6 +22,6 @@ export const ExpressHttpApp: HttpApp.Default<never, Express | HttpServerRequest.

next(error)
}) satisfies ErrorRequestHandler)(nodeRequest, nodeResponse)
})
}).pipe(HttpMiddleware.withLoggerDisabled)
},
)
2 changes: 1 addition & 1 deletion src/Program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export const Program = pipe(
addXRobotsTagHeader,
getLoggedInUser,
Effect.provideService(Locale, DefaultLocale),
HttpServer.serve(annotateLogsWithRequestId),
HttpServer.serve(flow(HttpMiddleware.logger, annotateLogsWithRequestId)),
HttpServer.withLogAddress,
Layer.provide(logStopped),
Layer.provide(Layer.effect(Express, expressServer)),
Expand Down
24 changes: 23 additions & 1 deletion src/Router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Headers, HttpMiddleware, HttpRouter, HttpServerRequest, HttpServerResponse } from '@effect/platform'
import { Effect, identity, Option, pipe, Record } from 'effect'
import { StatusCodes } from 'http-status-codes'
import { Locale, LoggedInUser, Redis } from './Context.js'
import { ExpressConfig, Locale, LoggedInUser, Redis } from './Context.js'
import {
type PageResponse,
type RedirectResponse,
Expand All @@ -13,6 +13,26 @@ import * as Routes from './routes.js'
import { TemplatePage } from './TemplatePage.js'
import * as WriteFeedbackFlow from './WriteFeedbackFlow/index.js'

const logRequest = HttpMiddleware.make(app =>
Effect.gen(function* () {
const request = yield* HttpServerRequest.HttpServerRequest
const { publicUrl } = yield* ExpressConfig

const url = new URL(request.url, publicUrl)

yield* Effect.annotateLogs(Effect.logInfo('Received HTTP request'), {
'http.method': request.method,
'http.url': request.url,
'http.path': url.pathname,
'http.query': Object.fromEntries(url.searchParams),
'http.referrer': Option.getOrUndefined(Headers.get(request.headers, 'Referer')),
'http.userAgent': Option.getOrUndefined(Headers.get(request.headers, 'User-Agent')),
})

return yield* app
}),
)

export const Router = pipe(
HttpRouter.empty,
HttpRouter.get(
Expand Down Expand Up @@ -68,6 +88,7 @@ export const Router = pipe(
Effect.andThen(HttpServerResponse.setHeaders({ 'Cache-Control': 'no-cache, private', Vary: 'Cookie' })),
),
),
HttpRouter.use(logRequest),
HttpRouter.get(
'/health',
Effect.gen(function* () {
Expand Down Expand Up @@ -97,6 +118,7 @@ export const Router = pipe(
return yield* HttpServerResponse.json({ status: 'error' }, { status: StatusCodes.SERVICE_UNAVAILABLE })
}),
),
HttpMiddleware.withLoggerDisabled,
),
),
HttpRouter.get('/robots.txt', HttpServerResponse.text('User-agent: *\nAllow: /')),
Expand Down

0 comments on commit e58d024

Please sign in to comment.