jevhooks.git / web / src / log.rs
log.rsannotatedlog.rssource80 lines · 2.3 KB · raw

What the Worker writes about a request: a route from a closed list, a method word, a status and a time. There is no field a visitor's address, query or headers could be put in.

4pub use aldebaran_log::{Method, Served};
6use crate::content::{CARD_PATH, FILES};

Every kind of thing the site answers. A path is turned into one of these before anything is logged, so a made-up path is Other, never its own text.

10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub enum Route {
12    Page,
13    Script,
14    Style,
15    Font,
16    Icon,
17    Shot,
18    Card,
19    Robots,
20    Other,
21}
23impl Route {
24    pub fn of(path: &str) -> Route {
25        match path {
26            "/" => Route::Page,
27            "/ui.js" => Route::Script,
28            "/ui.css" => Route::Style,
29            p if p.starts_with("/fonts/") => Route::Font,
30            "/favicon.svg" => Route::Icon,
31            p if FILES.find(p).is_some() => Route::Shot,
32            CARD_PATH => Route::Card,
33            "/robots.txt" => Route::Robots,
34            _ => Route::Other,
35        }
36    }
37}
38
39impl aldebaran_log::Route for Route {
40    fn name(self) -> &'static str {
41        match self {
42            Route::Page => "page",
43            Route::Script => "script",
44            Route::Style => "style",
45            Route::Font => "font",
46            Route::Icon => "icon",
47            Route::Shot => "shot",
48            Route::Card => "card",
49            Route::Robots => "robots",
50            Route::Other => "other",
51        }
52    }

A missing card is the build's fault, never a visitor's: say what to run.

55    fn note(self, status: u16) -> Option<&'static str> {
56        (self == Route::Card && status == 404).then_some("the link preview's card is not in the assets (mise run web:build)")
57    }
58}
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use crate::content::SHOTS;

The log names a route, never a path: anything not on the list is one word.

66    #[test]
67    fn a_path_nobody_serves_is_other() {
68        for path in ["/wp-login.php", "/shots/nope.svg", "/?x=1", "//", "/preview/other.png"] {
69            assert_eq!(Route::of(path), Route::Other, "{path}");
70        }
71    }
73    #[test]
74    fn every_picture_is_a_shot() {
75        for shot in SHOTS {
76            assert_eq!(Route::of(shot.asset.path), Route::Shot);
77        }
78        assert_eq!(Route::of(CARD_PATH), Route::Card);
79    }
80}