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 /src/source.rs | |
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.
Diffstat (limited to 'src/source.rs')
-rw-r--r-- | src/source.rs | 81 |
1 files changed, 81 insertions, 0 deletions
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 } }, + }) +} |