diff options
Diffstat (limited to 'src/frontend')
-rw-r--r-- | src/frontend/pages/home.rs | 4 | ||||
-rw-r--r-- | src/frontend/pages/layout.rs | 10 | ||||
-rw-r--r-- | src/frontend/pages/mod.rs | 61 | ||||
-rw-r--r-- | src/frontend/pages/node.rs | 46 | ||||
-rw-r--r-- | src/frontend/style/layout.css | 35 | ||||
-rw-r--r-- | src/frontend/style/master.css | 0 | ||||
-rw-r--r-- | src/frontend/style/mod.rs | 2 |
7 files changed, 150 insertions, 8 deletions
diff --git a/src/frontend/pages/home.rs b/src/frontend/pages/home.rs index a694c3c..bb0eb59 100644 --- a/src/frontend/pages/home.rs +++ b/src/frontend/pages/home.rs @@ -5,9 +5,9 @@ use crate::{ use actix_web::{get, web::Data, Responder}; #[get("/")] -async fn page_home(state: Data<AppState>) -> impl Responder { +pub async fn page_home(state: Data<AppState>) -> impl Responder { HtmlTemplate(Layout { - title: "Home - Jellything", + title: String::from("Home"), main: markup::new! { h1 { "It works!" } }, diff --git a/src/frontend/pages/layout.rs b/src/frontend/pages/layout.rs index e02336d..5654d3b 100644 --- a/src/frontend/pages/layout.rs +++ b/src/frontend/pages/layout.rs @@ -1,17 +1,19 @@ use markup::Render; markup::define! { - Layout<'a, Main: Render>(title: &'a str, main: Main) { + Layout<Main: Render>(title: String, main: Main) { @markup::doctype() html { head { - title { @title } + title { @title " - Jellything" } link[rel="stylesheet", href="/assets/style.css"]; } body { - header { "Grain" } + nav { + h1 { "Jellything" } + + } #main { @main } - footer { span { "jellything" } } } } } diff --git a/src/frontend/pages/mod.rs b/src/frontend/pages/mod.rs index 08e0e77..6e7bd87 100644 --- a/src/frontend/pages/mod.rs +++ b/src/frontend/pages/mod.rs @@ -1,7 +1,17 @@ -use actix_web::{body::BoxBody, http::StatusCode, HttpResponseBuilder, Responder}; +use std::{error::Error, fmt::Display}; + +use actix_web::{ + body::BoxBody, + http::{ + header::{HeaderName, HeaderValue}, + StatusCode, + }, + HttpResponseBuilder, Responder, +}; pub mod home; pub mod layout; +pub mod node; struct HtmlTemplate<T>(pub T); @@ -17,3 +27,52 @@ impl<T: markup::Render> Responder for HtmlTemplate<T> { .respond_to(req) } } + +pub struct ContentType<T>(pub &'static str, pub T); + +impl<T: Responder> Responder for ContentType<T> { + type Body = T::Body; + + fn respond_to(self, req: &actix_web::HttpRequest) -> actix_web::HttpResponse<Self::Body> { + let mut r = self.1.respond_to(req); + r.headers_mut().insert( + HeaderName::from_static("content-type"), + HeaderValue::from_static(self.0), + ); + r + } +} + +pub type MyResult<T> = actix_web::Result<T, MyError>; + +#[derive(Debug)] +pub struct MyError(anyhow::Error); + +impl Responder for MyError { + type Body = BoxBody; + fn respond_to(self, req: &actix_web::HttpRequest) -> actix_web::HttpResponse<Self::Body> { + HttpResponseBuilder::new(StatusCode::BAD_REQUEST) + .body(format!("error: {}", self.0)) + .respond_to(req) + } +} +impl actix_web::error::ResponseError for MyError { + fn status_code(&self) -> StatusCode { + StatusCode::BAD_REQUEST + } +} +impl Display for MyError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} +impl From<anyhow::Error> for MyError { + fn from(err: anyhow::Error) -> MyError { + MyError(err) + } +} +impl From<std::fmt::Error> for MyError { + fn from(err: std::fmt::Error) -> MyError { + MyError(anyhow::anyhow!("{err}")) + } +} diff --git a/src/frontend/pages/node.rs b/src/frontend/pages/node.rs new file mode 100644 index 0000000..c3f7791 --- /dev/null +++ b/src/frontend/pages/node.rs @@ -0,0 +1,46 @@ +use super::layout::Layout; +use crate::{ + frontend::pages::{HtmlTemplate, MyError, MyResult}, + library::{LibDirectory, LibItem, LibNode}, + AppState, +}; +use actix_web::{get, web, Responder}; +use actix_web_lab::respond::Html; +use log::debug; +use markup::Render; +use std::{ops::Deref, sync::Arc}; + +#[get("/library/{path:.*}")] +pub async fn page_library_node( + state: web::Data<AppState>, + params: web::Path<(String,)>, +) -> MyResult<impl Responder> { + debug!("request: {:?}", params.0); + let node = state.library.nested(¶ms.0)?; + let mut out = String::new(); + match node.deref() { + LibNode::Directory(dir) => Layout { + title: format!( + "{} - Library", + dir.path.file_name().unwrap().to_str().unwrap() + ), + main: Directory { dir: dir.clone() }, + } + .render(&mut out)?, + LibNode::Item(item) => Layout { + title: "".to_string(), + main: Item { item: item.clone() }, + } + .render(&mut out)?, + }; + Ok(Html(out)) +} + +markup::define! { + Directory(dir: Arc<LibDirectory>) { + h1 { @dir.data.name } + } + Item(item: Arc<LibItem>) { + h1 { "thats an item" } + } +} diff --git a/src/frontend/style/layout.css b/src/frontend/style/layout.css new file mode 100644 index 0000000..d6196b0 --- /dev/null +++ b/src/frontend/style/layout.css @@ -0,0 +1,35 @@ +@import url("https://s.metamuffin.org/static/font-ubuntu/include.css"); + +* { + color: white; + font-family: "Ubuntu", sans-serif; + font-weight: 300; + margin: 0px; + padding: 0px; +} + +body { + background-color: #1a1a1a; +} + +nav { + position: absolute; + top: 0px; + left: 0px; + padding: 1em; + width: 100vw; + height: 2em; + background-color: #41414144; +} + +nav h1 { + margin: 0px; + font-size: 1.5em; +} + +#main { + margin-top: 5em; + padding: 1em; + padding-left: 3em; + padding-right: 3em; +} diff --git a/src/frontend/style/master.css b/src/frontend/style/master.css deleted file mode 100644 index e69de29..0000000 --- a/src/frontend/style/master.css +++ /dev/null diff --git a/src/frontend/style/mod.rs b/src/frontend/style/mod.rs index 180fe0e..9d0729e 100644 --- a/src/frontend/style/mod.rs +++ b/src/frontend/style/mod.rs @@ -1,2 +1,2 @@ -pub const CSS_BUNDLE: &'static str = include_str!("master.css"); +pub const CSS_BUNDLE: &'static str = include_str!("layout.css"); |