Packages

Wisp integration for palabres, an opinionated logger for Gleam

Current section

Files

Jump to
palabres_wisp src palabres_wisp.erl
Raw

src/palabres_wisp.erl

-module(palabres_wisp).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([log_request/2]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/palabres_wisp.gleam", 17).
?DOC(
" Provides a middleware to display every incoming request for a Wisp server.\\\n"
" Use it in your router to log request with status code, path and method.\n"
"\n"
" ```gleam\n"
" import palabres_wisp\n"
" import wisp\n"
" pub fn router(req: wisp.Request, ctx: context) -> wisp.Response {\n"
" use <- palabres_wisp.log_request(req)\n"
" route_request(req)\n"
" }\n"
" ```\n"
).
-spec log_request(
gleam@http@request:request(wisp@internal:connection()),
fun(() -> gleam@http@response:response(wisp:body()))
) -> gleam@http@response:response(wisp:body()).
log_request(Req, Handler) ->
Response = Handler(),
Method = string:uppercase(
gleam@http:method_to_string(erlang:element(2, Req))
),
_pipe = palabres:notice(<<""/utf8>>),
_pipe@1 = palabres:int(
_pipe,
<<"status"/utf8>>,
erlang:element(2, Response)
),
_pipe@2 = palabres:string(_pipe@1, <<"method"/utf8>>, Method),
_pipe@3 = palabres:string(_pipe@2, <<"where"/utf8>>, erlang:element(8, Req)),
palabres:log(_pipe@3),
Response.