all repos — breadsite @ 65ec84fd4e55ea87d7147c6a5717d04c235aae00

Unnamed repository; edit this file 'description' to name the repository.

src/main.rs (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
use actix_web::{get, App, HttpServer, Result as AwResult};
use actix_files::Files;
use maud::{DOCTYPE, html, Markup};
use std::io;
use chrono::Local;

#[derive(Clone)]
struct NavElement {
    name: String,
    href: String,
    class: String
}

fn head(page_title: &str) -> Markup {
    html! {
        (DOCTYPE)
        head {
            meta charset="utf-8";
            title { (page_title) }
            link rel="icon" href="./static/neodog_laptop_notice.png";
            link rel="stylesheet" href="static/style.css";
            script src="https://cdn.jsdelivr.net/npm/htmx.org@2.0.6/dist/htmx.min.js" {}
            script { "htmx.config.selfRequestsOnly = false" }
        }
    }
}

fn nav_bar(mut elements: Vec<NavElement>, current: &str) -> Markup {
    elements.first_mut().map(|e| e.class.push_str(" first"));
    elements.last_mut().map(|e| e.class.push_str(" last"));
    elements.iter_mut().filter(|e| e.href == current).for_each(|e| e.class.push_str(" current"));

    html!(
        nav.navBar {
            @for (i, element) in elements.iter().enumerate() {
                ( nav_element(i, element) )
            }
        }
    )
}

fn nav_element(id: usize, element: &NavElement) -> Markup {
    html!(
        a class={( element.class.to_string() )} id={(format!("navElement-{}", id))} href={( element.href )} {( element.name )}
    )
}


fn header(current: &str) -> Markup{
    html!(
        header {
            (nav_bar(vec![
                NavElement{ name: "breadsite".to_string(), href: "/".to_string(), class: String::from("navElement")},
                NavElement{ name: "boop".to_string(), href: "/boop".to_string(), class: String::from("navElement")},
                NavElement{ name: "waf".to_string(), href: "/waf".to_string(), class: String::from("navElement")},
            ], current))


            div class="htmx-indicator nowPlaying loading" hx-get="http://localhost:5050/now-playing" hx-swap="outerHTML" hx-trigger="load" { ". . ." }
        }
    )
}

// TODOO: pawer 81x31
// TODOO: pawer links
fn pawer() -> Markup {
    html! {
        pawer {
            ul {
                li {"fedi: " a href="https://plasmatrap.com/@bread" {"@plasmatrap.com@bread"}}
                br; br;
                li {sub {"paws...."}}
            }
        }
    }
}

// TODOOOO: about me
// TODO: idea: heart rate monitor
// TODO: idea: svg -> neocat generator
fn breadsite() -> Markup {
    html! {
        h1 { "Hiiii" }
    }
}

fn boop() -> Markup {
    html! {
        h1 { "Boop me" }
        div class="boop timestamp" { (format!("boops since {}:",Local::now().format("%d/%m/%Y %H:%M UTC%Z"))) }
        div class="boop counter" #boop-counter hx-get="http://localhost:5050/boop" hx-trigger="load" {}
        div class="boop stack" {
            img class="boop unboop" src="./static/neodog.png" {}
            button class="boop button" hx-post="http://localhost:5050/boop" hx-trigger="mousedown throttle:300ms" hx-swap="swap:275ms" hx-target="#boop-counter" {}
        }
    }
}

fn waf() -> Markup {
    html! {
        h1 { "waf :3" }
    }
}


fn layout(page_title: &str, current: &str,site: Markup) -> Markup {
    html! {
        (head(page_title))
        body class={(page_title)} {
            (header(current))
            main {(site)}
            (pawer())
        }
    }
}

#[get("/")]
async fn breadsite_index() -> AwResult<Markup> {
    Ok(html! {
        (layout("breadsite", "/", breadsite()))
    })
}

#[get("/boop")]
async fn breadsite_boop() -> AwResult<Markup> {
    Ok(html! {
        (layout("boop", "/boop", boop()))
    })
}

#[get("/waf")]
async fn breadsite_waf() -> AwResult<Markup> {
    Ok(html! {
        (layout("waf", "/waf", waf()))
    })
}

#[actix_web::main]
async fn main() -> io::Result<()> {
    HttpServer::new(|| App::new()
        .service(breadsite_index)
        .service(breadsite_boop)
        .service(breadsite_waf)
        .service(Files::new("/static", "./static"))
    )
        .bind(("localhost", 8080))?
        .run()
        .await
}