tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

98 lignes · 2751 octetsBrut · Historique
1/// The access log: one line per request, at a level that tells which
2/// requests to look at.
3///
4/// use <- log.to_console Info
5/// access.logged (access.defaults ()) req fn { route req }
6///
7/// logs `GET /leaderboard 200 12ms client=127.0.0.1`. A 5xx is `Error`, a
8/// 4xx `Warn`, any other answer to an asset path (`/static/…`,
9/// `/favicon.ico`, …) `Debug`, and the rest `Info`: at the default level
10/// a page view is one line, not one per stylesheet and script. A request
11/// from htmx carries `htmx=1`, which tells a fragment from a page.
12///
13/// The time is the handler's: a streamed body is still being sent when
14/// the line is written. A handler that panics writes no line, since a
15/// cleanup cannot log (ADR 0016); the runtime reports the crash.
16
17import client.{type client}
18import log.{type Level, type log, Debug, Error, Info, Warn}
19import reply
20import std/http.{type Request, type Response}
21import std/list
22import std/string
23import std/time.{type clock}
24
25/// What the access log treats as an asset (paths that start with one of
26/// `assets`), and whether a line names the client's address
27/// (`client=`).
28pub type Access { Access(assets: List<String>, clients: Bool) }
29
30/// `/static/`, `/favicon.ico`, `/.well-known/` and `/robots.txt`, with the
31/// client's address.
32pub fn defaults :: -> Access
33{
34 Access(["/static/", "/favicon.ico", "/.well-known/", "/robots.txt"], True)
35}
36
37/// `access` with no client address on its lines, for an app that keeps
38/// addresses out of its logs.
39pub fn without_clients :: access: Access -> Access
40{
41 Access(..access, clients: False)
42}
43
44/// `handler`'s response to `req`, after logging one line for it.
45pub fn logged ::
46 access: Access
47 req: Request
48 handler: fn() -> Response / <client, clock, log | e>
49 -> Response / <client, clock, log | e>
50{
51 let start = time.monotonic ()
52 let resp = handler ()
53 let ms = time.in_milliseconds (time.elapsed (time.monotonic ()) start)
54 log.emit
55 (level access req resp.status)
56 "{req.method} {http.path req} {resp.status} {ms}ms"
57 (fields access req)
58 resp
59}
60
61/// The level of the line for `req` answered with `status`.
62pub fn level :: access: Access req: Request status: Int -> Level
63{
64 if status >= 500
65 {
66 Error
67 }
68 else if status >= 400
69 {
70 Warn
71 }
72 else if asset access (http.path req)
73 {
74 Debug
75 }
76 else
77 {
78 Info
79 }
80}
81
82fn asset :: access: Access path: String -> Bool
83{
84 list.any access.assets fn prefix { string.starts_with path prefix }
85}
86
87fn fields :: access: Access req: Request -> List<(String, String)> / <client>
88{
89 let from = if access.clients { [("client", client.address req)] } else { [] }
90 if reply.from_htmx req
91 {
92 list.append from [("htmx", "1")]
93 }
94 else
95 {
96 from
97 }
98}