From dc82b04345e93e3565bc26d1f616c0bf50ac77a9 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Wed, 27 Dec 2023 11:08:16 +0300 Subject: [PATCH] Make warp work --- src/cube/cube.css | 2 +- src/main.rs | 6 +++++- src/page.rs | 2 +- src/routes/_404/mod.rs | 19 ++++++++++++------- src/routes/assets.rs | 19 +++++-------------- src/routes/index/mod.rs | 23 ++++++++++++++--------- src/routes/mod.rs | 12 +++--------- 7 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/cube/cube.css b/src/cube/cube.css index 9563083..6bc620b 100644 --- a/src/cube/cube.css +++ b/src/cube/cube.css @@ -1,7 +1,7 @@ @font-face { font-family: "Bai Jamjuree"; font-weight: 700; - src: url("assets/BaiJamjuree700.woff2") format("woff2"); + src: url("/assets/BaiJamjuree700.woff2") format("woff2"); } body, diff --git a/src/main.rs b/src/main.rs index d2e8050..840142f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,8 @@ mod routes; use constants::*; use env_logger::Target; use log::LevelFilter; +use routes::*; +use warp::Filter; #[tokio::main] async fn main() -> anyhow::Result<()> { @@ -17,7 +19,9 @@ async fn main() -> anyhow::Result<()> { .target(Target::Stdout) .init(); - warp::serve(routes::filter()).run(([0, 0, 0, 0], 80)).await; + let routes = index::filter().or(assets::filter()).or(_404::filter()); + + warp::serve(routes).run(([0, 0, 0, 0], 80)).await; Ok(()) } diff --git a/src/page.rs b/src/page.rs index 7efecf7..39bbdb8 100644 --- a/src/page.rs +++ b/src/page.rs @@ -45,7 +45,7 @@ pub fn create(head: Markup, body: Markup) -> Markup { (pname("description", description)) (property("og:description", description)) - link rel="icon" href="assets/icon.gif" type="image/gif"; + link rel="icon" href="/assets/icon.gif" type="image/gif"; (property("og:image", "thumbnail.png")) (property("og:image:type", "image/png")) diff --git a/src/routes/_404/mod.rs b/src/routes/_404/mod.rs index da9b4dd..b4b25ce 100644 --- a/src/routes/_404/mod.rs +++ b/src/routes/_404/mod.rs @@ -1,20 +1,24 @@ use std::{ array, + convert::Infallible, sync::LazyLock, }; -use maud::{ - html, - Markup, +use maud::html; +use warp::{ + reply::{ + self, + Html, + }, + Filter, }; -use warp::Filter; use crate::{ cube, minify, }; -static PAGE: LazyLock = LazyLock::new(|| { +static PAGE: LazyLock = LazyLock::new(|| { cube::create( minify::css(embed::string!("404.css")), array::from_fn(|_| { @@ -28,8 +32,9 @@ static PAGE: LazyLock = LazyLock::new(|| { .clone() }), ) + .into_string() }); -pub fn filter() -> impl Filter { - warp::any().map(|| &*PAGE) +pub fn filter() -> impl Filter,), Error = Infallible> + Clone { + warp::any().map(|| reply::html(PAGE.as_str())) } diff --git a/src/routes/assets.rs b/src/routes/assets.rs index 2c9032b..1c5683e 100644 --- a/src/routes/assets.rs +++ b/src/routes/assets.rs @@ -9,11 +9,12 @@ use std::{ use tar::Archive; use warp::{ - filters::path::FullPath, + filters::fs::File, + reject::Rejection, Filter, }; -static ASSETS: LazyLock>> = LazyLock::new(|| { +static _ASSETS: LazyLock>> = LazyLock::new(|| { let contents = embed::bytes!("../../assets.tar"); let mut archive = Archive::new(Cursor::new(contents.as_ref())); @@ -38,16 +39,6 @@ static ASSETS: LazyLock>> = LazyLock::new(|| { assets }); -pub fn filter() -> impl Filter { - warp::path!("assets" / ..) - .and(warp::path::full()) - .map(|path: FullPath| { - println!("{}", path.as_str()); - - if let Some(asset) = ASSETS.get(path.as_str()) { - } - else { - warp::reject::not_found() - } - }) +pub fn filter() -> impl Filter + Clone { + warp::path("assets").and(warp::fs::dir("assets")) } diff --git a/src/routes/index/mod.rs b/src/routes/index/mod.rs index 8e43d09..5c68feb 100644 --- a/src/routes/index/mod.rs +++ b/src/routes/index/mod.rs @@ -1,29 +1,33 @@ use std::sync::LazyLock; -use maud::{ - html, - Markup, +use maud::html; +use warp::{ + reject::Rejection, + reply::{ + self, + Html, + }, + Filter, }; -use warp::Filter; use crate::{ cube, minify, }; -static PAGE: LazyLock = LazyLock::new(|| { +static PAGE: LazyLock = LazyLock::new(|| { cube::create( minify::css(embed::string!("index.css")), [ html! { - a href="contact" { + a href="/contact" { div class="frame" { "contact" } } }, html! { - a href="github" { + a href="/github" { div class="frame" { "github" } @@ -35,8 +39,9 @@ static PAGE: LazyLock = LazyLock::new(|| { html! {}, ], ) + .into_string() }); -pub fn filter() -> impl Filter { - warp::any().map(|| &*PAGE) +pub fn filter() -> impl Filter,), Error = Rejection> + Clone { + warp::path!().map(|| reply::html(PAGE.as_str())) } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index b9462c1..fa8c6d7 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,9 +1,3 @@ -use warp::Filter; - -mod _404; -mod assets; -mod index; - -pub fn filter() -> impl Filter { - warp::get().and(index::filter().or(assets::filter()).or(_404::filter())) -} +pub mod _404; +pub mod assets; +pub mod index;