aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/main.rs1
-rw-r--r--server/src/routes/mod.rs9
-rw-r--r--server/src/routes/ui/account/mod.rs19
-rw-r--r--server/src/routes/ui/error.rs58
-rw-r--r--server/src/routes/ui/home.rs2
-rw-r--r--server/src/routes/ui/layout.rs24
-rw-r--r--server/src/routes/ui/mod.rs2
-rw-r--r--server/src/routes/ui/player.rs9
-rw-r--r--server/src/routes/ui/style/layout.css6
-rw-r--r--server/src/routes/ui/style/player.css7
10 files changed, 81 insertions, 56 deletions
diff --git a/server/src/main.rs b/server/src/main.rs
index 5158b8d..e10302e 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -7,7 +7,6 @@ use library::Library;
use once_cell::sync::Lazy;
use rocket::launch;
use routes::{build_rocket, ui::account::hash_password};
-use std::sync::Arc;
pub mod config;
pub mod database;
diff --git a/server/src/routes/mod.rs b/server/src/routes/mod.rs
index f707a60..0b07728 100644
--- a/server/src/routes/mod.rs
+++ b/server/src/routes/mod.rs
@@ -3,7 +3,10 @@ use jellyremuxer::RemuxerContext;
use rocket::{catchers, config::SecretKey, routes, Build, Config, Rocket};
use stream::r_stream;
use ui::{
- account::{r_account_login, r_account_login_post, r_account_register, r_account_register_post},
+ account::{
+ r_account_login, r_account_login_post, r_account_logout, r_account_logout_post,
+ r_account_register, r_account_register_post,
+ },
error::r_catch,
home::r_home,
node::{r_item_assets, r_library_node},
@@ -16,7 +19,7 @@ pub mod ui;
#[macro_export]
macro_rules! uri {
- ($kk:tt) => {
+ ($kk:stmt) => {
&rocket::uri!($kk).to_string()
};
}
@@ -49,6 +52,8 @@ pub fn build_rocket(
r_account_login_post,
r_account_register,
r_account_register_post,
+ r_account_logout,
+ r_account_logout_post,
r_item_assets,
],
)
diff --git a/server/src/routes/ui/account/mod.rs b/server/src/routes/ui/account/mod.rs
index 74710d9..bdc6062 100644
--- a/server/src/routes/ui/account/mod.rs
+++ b/server/src/routes/ui/account/mod.rs
@@ -74,6 +74,19 @@ pub fn r_account_login() -> DynLayoutPage<'static> {
}
}
+#[get("/account/logout")]
+pub fn r_account_logout() -> DynLayoutPage<'static> {
+ LayoutPage {
+ title: "Log out".to_string(),
+ content: markup::new! {
+ h1 { "Log out" }
+ form[method="POST", action=""] {
+ input[type="submit", value="Log out."];
+ }
+ },
+ }
+}
+
#[post("/account/register", data = "<form>")]
pub fn r_account_register_post<'a>(
database: &'a State<Database>,
@@ -139,6 +152,12 @@ pub fn r_account_login_post(
Ok(Redirect::found(uri!(r_home())))
}
+#[post("/account/logout")]
+pub fn r_account_logout_post(jar: &CookieJar) -> MyResult<Redirect> {
+ jar.remove_private(Cookie::named("user"));
+ Ok(Redirect::found(uri!(r_home())))
+}
+
fn format_form_error<T>(form: Form<Contextual<T>>) -> MyError {
let mut k = String::from("form validation failed:");
for e in form.context.errors() {
diff --git a/server/src/routes/ui/error.rs b/server/src/routes/ui/error.rs
index 011847e..b07ca8b 100644
--- a/server/src/routes/ui/error.rs
+++ b/server/src/routes/ui/error.rs
@@ -1,31 +1,27 @@
+use super::layout::DynLayoutPage;
use super::layout::LayoutPage;
-use super::{layout::Layout, HtmlTemplate};
use crate::routes::ui::account::rocket_uri_macro_r_account_login;
-use markup::Render;
use rocket::http::Status;
use rocket::uri;
use rocket::{
catch,
- http::ContentType,
response::{self, Responder},
- Request, Response,
+ Request,
};
-use std::{fmt::Display, io::Cursor};
+use std::fmt::Display;
#[catch(default)]
-pub fn r_catch<'a>(status: Status, _request: &Request) -> () {
- // HtmlTemplate(box Layout {
- // title: "Not found".to_string(),
- // session: None,
- // main: markup::new! {
- // h2 { "Error" }
- // p { @format!("{status}") }
- // @if status == Status::NotFound {
- // p { "You might need to " a[href=&uri!(r_account_login()).to_string()] { "log in" } ", to see this page" }
- // }
- // },
- // })
- todo!()
+pub fn r_catch<'a>(status: Status, _request: &Request) -> DynLayoutPage<'a> {
+ LayoutPage {
+ title: "Not found".to_string(),
+ content: markup::new! {
+ h2 { "Error" }
+ p { @format!("{status}") }
+ @if status == Status::NotFound {
+ p { "You might need to " a[href=&uri!(r_account_login()).to_string()] { "log in" } ", to see this page" }
+ }
+ },
+ }
}
pub type MyResult<T> = Result<T, MyError>;
@@ -34,23 +30,15 @@ pub type MyResult<T> = Result<T, MyError>;
pub struct MyError(pub anyhow::Error);
impl<'r> Responder<'r, 'static> for MyError {
- fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
- // let mut out = String::new();
- // LayoutPage {
- // title: "Error".to_string(),
- // content: markup::new! {
- // h2 { "An error occured. Nobody is sorry"}
- // pre.error { @format!("{:?}", self.0) }
- // },
- // }
- // .render(&mut out)
- // .unwrap();
- // Response::build()
- // .header(ContentType::HTML)
- // .status(Status::BadRequest)
- // .streamed_body(Cursor::new(out))
- // .ok()
- todo!()
+ fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
+ LayoutPage {
+ title: "Error".to_string(),
+ content: markup::new! {
+ h2 { "An error occured. Nobody is sorry"}
+ pre.error { @format!("{:?}", self.0) }
+ },
+ }
+ .respond_to(req)
}
}
diff --git a/server/src/routes/ui/home.rs b/server/src/routes/ui/home.rs
index 88c6cfb..6e4684c 100644
--- a/server/src/routes/ui/home.rs
+++ b/server/src/routes/ui/home.rs
@@ -6,7 +6,7 @@ use crate::{library::Library, routes::ui::node::NodePage};
use rocket::{get, State};
#[get("/")]
-pub async fn r_home(_sess: Session, library: &State<Library>) -> LayoutPage<markup::DynRender> {
+pub async fn r_home(_sess: Session, library: &State<Library>) -> DynLayoutPage {
LayoutPage {
title: "Home".to_string(),
content: markup::new! {
diff --git a/server/src/routes/ui/layout.rs b/server/src/routes/ui/layout.rs
index c25e644..614aa66 100644
--- a/server/src/routes/ui/layout.rs
+++ b/server/src/routes/ui/layout.rs
@@ -1,15 +1,19 @@
-use super::{account::session::Session, Defer, HtmlTemplate};
-use crate::{uri, CONF};
+use super::account::session::Session;
+use crate::{
+ routes::ui::account::{
+ rocket_uri_macro_r_account_login, rocket_uri_macro_r_account_logout,
+ rocket_uri_macro_r_account_register,
+ },
+ uri, CONF,
+};
use async_std::task::block_on;
use markup::Render;
use rocket::{
http::ContentType,
- request::{FromRequest, Outcome},
response::{self, Responder},
Request, Response,
};
-use std::{convert::Infallible, io::Cursor};
-use tokio::runtime::Handle;
+use std::io::Cursor;
markup::define! {
Layout<Main: Render>(title: String, main: Main, session: Option<Session>) {
@@ -27,10 +31,11 @@ markup::define! {
div.account {
@if let Some(session) = session {
-
+ span { "Logged in as " @session.user.display_name }
+ a[href=uri!(r_account_logout())] { "Log out" }
} else {
- // a[href=uri!(r_account_register())] { "Register" }
- // a[href=uri!(r_account_login())] { "Log in" }
+ a[href=uri!(r_account_register())] { "Register" }
+ a[href=uri!(r_account_login())] { "Log in" }
}
}
}
@@ -49,6 +54,9 @@ pub struct LayoutPage<T> {
impl<'r, Main: Render> Responder<'r, 'static> for LayoutPage<Main> {
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
+ // TODO blocking the event loop here. it seems like there is no other way to
+ // TODO offload this, since the guard references `req` which has a lifetime.
+ // TODO therefore we just block. that is fine since the database is somewhat fast.
let session = block_on(req.guard::<Option<Session>>()).unwrap();
let mut out = String::new();
Layout {
diff --git a/server/src/routes/ui/mod.rs b/server/src/routes/ui/mod.rs
index e062a68..ee2b4f8 100644
--- a/server/src/routes/ui/mod.rs
+++ b/server/src/routes/ui/mod.rs
@@ -19,7 +19,7 @@ pub mod style;
pub struct HtmlTemplate<'a>(pub markup::DynRender<'a>);
impl<'r> Responder<'r, 'static> for HtmlTemplate<'_> {
- fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
+ fn respond_to(self, _req: &'r Request<'_>) -> response::Result<'static> {
let mut out = String::new();
self.0.render(&mut out).unwrap();
Response::build()
diff --git a/server/src/routes/ui/player.rs b/server/src/routes/ui/player.rs
index 764f583..116b441 100644
--- a/server/src/routes/ui/player.rs
+++ b/server/src/routes/ui/player.rs
@@ -1,15 +1,14 @@
-use super::{account::session::Session, layout::LayoutPage, HtmlTemplate};
+use super::{account::session::Session, layout::LayoutPage};
use crate::{
library::{Item, Library},
routes::{
stream::stream_uri,
- ui::{error::MyResult, layout::DynLayoutPage, node::rocket_uri_macro_r_item_assets},
+ ui::{error::MyResult, layout::DynLayoutPage},
},
};
use jellycommon::SourceTrackKind;
-use log::warn;
-use rocket::{get, uri, FromForm, State};
-use std::{alloc::Layout, path::PathBuf, sync::Arc};
+use rocket::{get, FromForm, State};
+use std::{path::PathBuf, sync::Arc};
pub fn player_uri(path: &PathBuf) -> String {
format!("/player/{}", path.to_str().unwrap())
diff --git a/server/src/routes/ui/style/layout.css b/server/src/routes/ui/style/layout.css
index 8f7afb4..0c0e906 100644
--- a/server/src/routes/ui/style/layout.css
+++ b/server/src/routes/ui/style/layout.css
@@ -78,6 +78,11 @@ nav h1 {
margin-right: 1em;
}
+nav .account {
+ float: right;
+ display: inline;
+}
+
#main {
display: block;
margin-top: var(--bar-height);
@@ -90,4 +95,3 @@ nav h1 {
color: rgb(255, 117, 117);
font-family: monospace;
}
-
diff --git a/server/src/routes/ui/style/player.css b/server/src/routes/ui/style/player.css
index 8d3488f..0a9c16f 100644
--- a/server/src/routes/ui/style/player.css
+++ b/server/src/routes/ui/style/player.css
@@ -11,7 +11,8 @@ option {
}
input[type="submit"] {
- width: 30%;
+ padding: 0.5em;
+ margin: 1em;
justify-self: center;
border: 0px solid transparent;
background-color: var(--accent-dark);
@@ -52,7 +53,7 @@ input[type="radio"]:checked {
background-color: var(--accent-light);
}
-input[type=submit]:hover {
+input[type="submit"]:hover {
filter: hue-rotate(-20deg);
}
@@ -82,5 +83,7 @@ fieldset label:hover {
}
.playerconf input[type="submit"] {
grid-area: b;
+ width: 30%;
+ height: 3em;
font-size: 1.5em;
}