diff options
author | metamuffin <metamuffin@disroot.org> | 2023-02-13 18:00:27 +0100 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2023-02-13 18:00:27 +0100 |
commit | ba4d782687b5eb8d91fd881a7cb9d0adce7dd9f0 (patch) | |
tree | 2e6674a8252993b04756b798ef7742dfbda8ee1a | |
parent | 78ac98926ebd97c5166a6c88b5abd9e85883462b (diff) | |
download | metamuffin-website-ba4d782687b5eb8d91fd881a7cb9d0adce7dd9f0.tar metamuffin-website-ba4d782687b5eb8d91fd881a7cb9d0adce7dd9f0.tar.bz2 metamuffin-website-ba4d782687b5eb8d91fd881a7cb9d0adce7dd9f0.tar.zst |
licences are boring, lets show the source code instead.
-rw-r--r-- | Cargo.lock | 21 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/animation.rs | 50 | ||||
-rw-r--r-- | src/layout.rs | 9 | ||||
-rw-r--r-- | src/main.rs | 7 | ||||
-rw-r--r-- | src/source.rs | 81 |
6 files changed, 112 insertions, 57 deletions
@@ -764,6 +764,26 @@ dependencies = [ ] [[package]] +name = "include_dir" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e" +dependencies = [ + "glob", + "include_dir_macros", +] + +[[package]] +name = "include_dir_macros" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] name = "indexmap" version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1853,6 +1873,7 @@ version = "0.1.0" dependencies = [ "async-std", "env_logger", + "include_dir", "log", "markup", "rocket", @@ -10,3 +10,4 @@ log = "0.4.17" env_logger = "0.10.0" async-std = "1.12.0" markup = "0.13.1" +include_dir = { version = "0.7.3", features = ["glob"] } diff --git a/src/animation.rs b/src/animation.rs deleted file mode 100644 index bfb17a4..0000000 --- a/src/animation.rs +++ /dev/null @@ -1,50 +0,0 @@ -use rocket::{ - get, - http::{Cookie, CookieJar, Header}, - response::{self, Responder}, - Request, -}; - -pub struct Reload<T>(f64, pub T); - -#[rocket::async_trait] -impl<'r, T: Responder<'r, 'static>> Responder<'r, 'static> for Reload<T> { - fn respond_to(self, request: &'r Request<'_>) -> response::Result<'static> { - let mut resp = self.1.respond_to(request); - if let Ok(resp) = &mut resp { - resp.set_header(Header::new("refresh", format!("{}", self.0))); - } - resp - } -} - -const TEXT: &'static str = include_str!("../COPYING"); -const END_FRAME: isize = TEXT.len() as isize / 100 + 5; - -#[get("/wubbel")] -pub fn r_wubbel(jar: &CookieJar) -> Reload<String> { - let frame = jar - .get("frame") - .and_then(|c| c.value().parse().ok()) - .unwrap_or(0isize); - jar.add(Cookie::build("frame", format!("{}", (frame + 1) % (END_FRAME + 1))).finish()); - - let text = TEXT - .chars() - .enumerate() - .map(|(i, e)| { - if e == '\n' || frag(i as isize, frame) { - e - } else { - ' ' - } - }) - .collect::<String>(); - - Reload(if frame >= END_FRAME { 60.0 } else { 0.1 }, text) -} - -#[inline] -fn frag(i: isize, frame: isize) -> bool { - i % 5 < (frame - i / 100) -} diff --git a/src/layout.rs b/src/layout.rs index b5ad682..c553990 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -4,8 +4,8 @@ Copyright (C) 2023 metamuffin <metamuffin.org> */ use crate::pages::*; +use crate::source::*; use crate::uri; -use crate::animation::*; use markup::Render; use rocket::{ http::ContentType, @@ -15,14 +15,14 @@ use rocket::{ use std::io::Cursor; markup::define! { - Layout<Main: Render>(title: String, main: Main) { + Layout<Main: Render>(title: String, main: Main, noimg: bool) { @markup::doctype() html { head { title { @title " - " "metamuffin's website" } } body { - img[src="https://s.metamuffin.org/avatar/default-512.webp", align="left", height=80, hspace=10]; + @if !noimg { img[src="https://s.metamuffin.org/avatar/default-512.webp", align="left", height=80, hspace=10]; } h1 { "metamuffin's personal website" } nav { a[href=uri!(r_about())] { "About" } " " @@ -30,7 +30,7 @@ markup::define! { a[href=uri!(r_contact())] { "Contact" } " " a[href="https://codeberg.org/metamuffin"] { "Codeberg" } " " a[href=uri!(r_pgp_key())] { "PGP-Key" } " " - a[href=uri!(r_wubbel())] { i {"wubbel"} } " " + a[href=uri!(r_source())] { "Sources" } " " } hr; section { @main } @@ -58,6 +58,7 @@ impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage<Main> { let mut out = String::new(); Layout { main: self.content, + noimg: self.title == "Source", title: self.title, } .render(&mut out) diff --git a/src/main.rs b/src/main.rs index 86e427d..b0822e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,14 +3,14 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2023 metamuffin <metamuffin.org> */ -pub mod animation; pub mod layout; pub mod pages; +pub mod source; pub mod wellknown; -use animation::*; use pages::*; use rocket::{catchers, fairing::AdHoc, http::Header, routes}; +use source::*; use wellknown::*; #[tokio::main] @@ -22,6 +22,7 @@ async fn main() { res.set_header(Header::new("server", "blub")); Box::pin(async {}) })) + .manage(prepare_source()) .mount( "/", routes![ @@ -30,7 +31,7 @@ async fn main() { r_contact, r_projects, r_pgp_key, - r_wubbel, + r_source, r_wellknown_security, r_wellknown_matrix_server, r_wellknown_matrix_client, diff --git a/src/source.rs b/src/source.rs new file mode 100644 index 0000000..ef76512 --- /dev/null +++ b/src/source.rs @@ -0,0 +1,81 @@ +use rocket::{ + get, + http::Header, + response::{self, Responder}, + Request, State, +}; +use std::time::{Duration, SystemTime, UNIX_EPOCH}; + +use crate::layout::{DynLayoutPage, LayoutPage}; + +pub struct Reload<T>(pub T); + +#[rocket::async_trait] +impl<'r, T: Responder<'r, 'static>> Responder<'r, 'static> for Reload<T> { + fn respond_to(self, request: &'r Request<'_>) -> response::Result<'static> { + let mut resp = self.0.respond_to(request); + if let Ok(resp) = &mut resp { + resp.set_header(Header::new("refresh", "0")); + } + resp + } +} + +const SOURCE_DIR: include_dir::Dir = include_dir::include_dir!("$CARGO_MANIFEST_DIR/src"); + +pub struct SourceWrap(Vec<String>); + +pub fn prepare_source() -> SourceWrap { + SourceWrap( + SOURCE_DIR + .find("**/*.rs") + .unwrap() + .map(|f| { + format!( + " +====================================== + Contents of {:?} +====================================== +{} +", + f.path(), + f.as_file().unwrap().contents_utf8().unwrap() + ) + }) + .collect::<Vec<_>>() + .join("\n") + .split("\n") + .map(String::from) + .collect(), + ) +} + +#[get("/source")] +pub async fn r_source(text: &State<SourceWrap>) -> Reload<DynLayoutPage> { + let mspf = 100u128; + + let ts = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_millis(); + + let frame = ts / mspf; + let frame_off = ts % mspf; + + tokio::time::sleep(Duration::from_millis((mspf - frame_off) as u64)).await; + + let mut out = String::new(); + out += &format!( + "About {} milliseconds have passed since midnight of the january the first in 1970.\n", + ts + ); + out += "------------------------------------------------------\n"; + for i in frame..frame + 30 { + out += &text.0[(i % text.0.len() as u128) as usize]; + out += "\n"; + } + Reload(LayoutPage { + title: "Source".to_string(), + content: markup::new! { pre { @out } }, + }) +} |