tangled
alpha
login
or
join now
keii.dev
/
wisp
3
fork
atom
🧚 A practical web framework for Gleam
3
fork
atom
overview
issues
pulls
pipelines
Log requests
Louis Pilfold
2 years ago
ab4bb5e0
d3395b76
+34
-5
4 changed files
expand all
collapse all
unified
split
README.md
action
src
action
web.gleam
test
action
feature
applications_test.gleam
framework
src
framework.gleam
-1
README.md
···
4
4
5
5
## TODO
6
6
7
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
12
+
use <- framework.log_requests(req)
12
13
use <- serve_default_responses
13
14
use <- framework.rescue_crashes
14
14
-
15
15
service(req)
16
16
}
17
17
···
37
37
38
38
413 ->
39
39
h("h1", [], [text("Request entity too large")])
40
40
+
|> htmb.render_page(doctype: "html")
41
41
+
|> framework.html_body(response, _)
42
42
+
43
43
+
500 ->
44
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
2
-
import action/feature/applications.{NextStep}
3
3
-
import action/feature/applications/form
2
2
+
import action/applications.{NextStep}
3
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
35
+
import gleam/io
36
36
+
import gleam/int
35
37
import mist
36
38
37
39
//
···
338
340
// Middleware
339
341
//
340
342
343
343
+
// TODO: test
344
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
344
-
Error(_) -> internal_server_error()
348
348
+
Error(error) -> {
349
349
+
// TODO: log the error
350
350
+
io.debug(error)
351
351
+
internal_server_error()
352
352
+
}
345
353
}
354
354
+
}
355
355
+
356
356
+
// TODO: test
357
357
+
// TODO: document
358
358
+
// TODO: real implementation that uses the logger
359
359
+
pub fn log_requests(req: Request, service: fn() -> Response) -> Response {
360
360
+
let response = service()
361
361
+
[
362
362
+
int.to_string(response.status),
363
363
+
" ",
364
364
+
string.uppercase(http.method_to_string(req.method)),
365
365
+
" ",
366
366
+
req.path,
367
367
+
]
368
368
+
|> string.concat
369
369
+
|> io.println
370
370
+
response
346
371
}
347
372
348
373
//