🧚 A practical web framework for Gleam

Log requests

+34 -5
-1
README.md
··· 4 4 5 5 ## TODO 6 6 7 - - Log when HTTP requests are received. 8 7 - Read database file location from environment. 9 8 - Store answer from form in database. 10 9 - TODO: And also write tests!
+6 -1
action/src/action/web.gleam
··· 9 9 10 10 pub fn middleware(req: Request, service: fn(Request) -> Response) -> Response { 11 11 let req = framework.method_override(req) 12 + use <- framework.log_requests(req) 12 13 use <- serve_default_responses 13 14 use <- framework.rescue_crashes 14 - 15 15 service(req) 16 16 } 17 17 ··· 37 37 38 38 413 -> 39 39 h("h1", [], [text("Request entity too large")]) 40 + |> htmb.render_page(doctype: "html") 41 + |> framework.html_body(response, _) 42 + 43 + 500 -> 44 + h("h1", [], [text("Internal server error")]) 40 45 |> htmb.render_page(doctype: "html") 41 46 |> framework.html_body(response, _) 42 47
+2 -2
action/test/action/feature/applications_test.gleam
··· 1 1 import gleeunit/should 2 - import action/feature/applications.{NextStep} 3 - import action/feature/applications/form 2 + import action/applications.{NextStep} 3 + import action/applications/form 4 4 5 5 pub fn next_ready_test() { 6 6 applications.step_initial
+26 -1
framework/src/framework.gleam
··· 32 32 import gleam/result 33 33 import gleam/string 34 34 import gleam/uri 35 + import gleam/io 36 + import gleam/int 35 37 import mist 36 38 37 39 // ··· 338 340 // Middleware 339 341 // 340 342 343 + // TODO: test 344 + // TODO: document 341 345 pub fn rescue_crashes(service: fn() -> Response) -> Response { 342 346 case erlang.rescue(service) { 343 347 Ok(response) -> response 344 - Error(_) -> internal_server_error() 348 + Error(error) -> { 349 + // TODO: log the error 350 + io.debug(error) 351 + internal_server_error() 352 + } 345 353 } 354 + } 355 + 356 + // TODO: test 357 + // TODO: document 358 + // TODO: real implementation that uses the logger 359 + pub fn log_requests(req: Request, service: fn() -> Response) -> Response { 360 + let response = service() 361 + [ 362 + int.to_string(response.status), 363 + " ", 364 + string.uppercase(http.method_to_string(req.method)), 365 + " ", 366 + req.path, 367 + ] 368 + |> string.concat 369 + |> io.println 370 + response 346 371 } 347 372 348 373 //