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 |
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="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: ":333".to_string(), href: "/3".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 {
@for _ in 1..101 {
p { "paws...." }
}
}
}
}
// TODOOOO: about me
// TODO: idea: heart rate monitor
// TODO: idea: view counter
// TODO: idea: svg -> neocat generator
fn breadsite() -> Markup {
html! {
main {
h1 { "Hiiii" }
}
}
}
fn boop() -> Markup {
html! {
main {
h1 { "Boop me" }
div class="boop timestamp" { (format!("boops since {}:",Local::now().format("%d/%m/%Y %H:%M UTC%Z"))) }
div #boop-counter hx-get="http://localhost:5050/boop" hx-trigger="load" {}
div class="boop stack" {
img class="unbooped" src="./static/neodog.png" {}
button class="boop" hx-post="http://localhost:5050/boop" hx-trigger="mousedown throttle:500ms" hx-swap="swap:500ms" hx-target="#boop-counter" {}
}
}
}
}
fn layout(page_title: &str, current: &str,site: Markup) -> Markup {
html! {
(head(page_title))
body {
(header(current))
(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("/3")]
async fn breadsite_3() -> AwResult<Markup> {
Ok(html! {
(layout(":333", "/3", breadsite()))
})
}
#[actix_web::main]
async fn main() -> io::Result<()> {
HttpServer::new(|| App::new()
.service(breadsite_index)
.service(breadsite_boop)
.service(breadsite_3)
.service(Files::new("/static", "./static"))
)
.bind(("localhost", 8080))?
.run()
.await
}
|