Compare commits
23 Commits
eae628bd62
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
d5eba9620c
|
|||
|
39afdd83a7
|
|||
|
34b887730a
|
|||
|
f2dc4c908e
|
|||
|
219f1f5e7d
|
|||
|
102387fdad
|
|||
|
87bd1b50a9
|
|||
|
c8cd4b95dc
|
|||
|
8348277a87
|
|||
|
e7c43e7a47
|
|||
|
4bacbfd1ab
|
|||
|
fa9ea95657
|
|||
|
3ec07c0469
|
|||
|
f41f4f8d80
|
|||
|
c9b7bbfece
|
|||
|
9de0b13ee8
|
|||
|
8b00db60eb
|
|||
|
69eb3a5e80
|
|||
|
7133e53536
|
|||
|
ebfe18aca2
|
|||
|
843572b487
|
|||
|
2cbc6aeaff
|
|||
|
f84e51d13a
|
33
Cargo.lock
generated
33
Cargo.lock
generated
@@ -1096,7 +1096,7 @@ dependencies = [
|
||||
"tokio-tungstenite 0.28.0",
|
||||
"tokio-util",
|
||||
"tower",
|
||||
"tower-http",
|
||||
"tower-http 0.6.11",
|
||||
"tower-layer",
|
||||
"tracing",
|
||||
"tungstenite 0.27.0",
|
||||
@@ -1374,7 +1374,7 @@ dependencies = [
|
||||
"tokio-tungstenite 0.28.0",
|
||||
"tokio-util",
|
||||
"tower",
|
||||
"tower-http",
|
||||
"tower-http 0.6.11",
|
||||
"tracing",
|
||||
"tracing-futures",
|
||||
"url",
|
||||
@@ -2419,6 +2419,7 @@ dependencies = [
|
||||
"diesel-async",
|
||||
"diesel_migrations",
|
||||
"dioxus",
|
||||
"dioxus-html",
|
||||
"dotenvy",
|
||||
"fred",
|
||||
"getrandom 0.4.3",
|
||||
@@ -2427,6 +2428,7 @@ dependencies = [
|
||||
"rand 0.10.1",
|
||||
"serde",
|
||||
"thiserror 2.0.18",
|
||||
"tower-http 0.7.0",
|
||||
"tower-sessions-redis-store",
|
||||
"tracing",
|
||||
]
|
||||
@@ -3293,7 +3295,7 @@ dependencies = [
|
||||
"tokio-rustls",
|
||||
"tokio-util",
|
||||
"tower",
|
||||
"tower-http",
|
||||
"tower-http 0.6.11",
|
||||
"tower-service",
|
||||
"url",
|
||||
"wasm-bindgen",
|
||||
@@ -4182,6 +4184,31 @@ dependencies = [
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tower-http"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b11f75e912b0c2be01b63d8cf8057b8c3f97cf34abb3d431a3a4c8675498e233"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http-body-util",
|
||||
"http-range-header",
|
||||
"httpdate",
|
||||
"mime",
|
||||
"mime_guess",
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tower-layer"
|
||||
version = "0.3.3"
|
||||
|
||||
@@ -17,13 +17,15 @@ diesel = { version = "2.3.10", optional = true, features = ["chrono"] }
|
||||
diesel-async = { version = "0.9.1", optional = true, features = ["postgres", "deadpool", "migrations"] }
|
||||
diesel_migrations = { version = "2.3.2", optional = true }
|
||||
dioxus = { version = "0.7.9", features = ["router", "fullstack"] }
|
||||
dioxus-html = "0.7.9"
|
||||
dotenvy = { version = "0.15.7", optional = true }
|
||||
fred = { version = "10.1.0", optional = true }
|
||||
lucide-dioxus = { version = "3.11.0", features = ["notifications"] }
|
||||
lucide-dioxus = { version = "3.11.0", features = ["notifications", "account"] }
|
||||
pbkdf2 = { version = "0.13.0", optional = true, features = ["getrandom", "phc"] }
|
||||
rand = "0.10.1"
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
thiserror = "2.0.18"
|
||||
tower-http = { version = "0.7.0", optional = true, features = ["fs"] }
|
||||
tower-sessions-redis-store = { version = "0.16.0", optional = true }
|
||||
tracing = "0.1.44"
|
||||
|
||||
@@ -40,6 +42,7 @@ server = [
|
||||
"dep:dotenvy",
|
||||
"dep:fred",
|
||||
"dep:pbkdf2",
|
||||
"dep:tower-http",
|
||||
"dep:tower-sessions-redis-store",
|
||||
]
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg viewBox="0 0 254 254" version="1.1" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"><clipPath clipPathUnits="userSpaceOnUse"><path fill="#4032a8" d="m 9.028871e-5,480.0001 v 0 C 9.028871e-5,214.90342 214.90341,1.0022047e-4 480.00009,1.0022047e-4 v 0 c 127.30393,0 249.39377,50.57128477953 339.41122,140.58874977953 90.01752,90.01746 140.58875,212.10733 140.58875,339.41125 v 0 c 0,265.09665 -214.90332,479.99997 -479.99997,479.99997 v 0 C 214.90341,960.00007 9.028871e-5,745.09675 9.028871e-5,480.0001 Z" fill-rule="evenodd" style="display:inline;fill:#4032a8;fill-opacity:.495496"/></clipPath><g><g style="fill:none;stroke:none;stroke-linecap:square;stroke-miterlimit:10" transform="matrix(0.26458333,0,0,0.26458333,0,4.5672766)"><g clip-path="url(#p.0)" transform="translate(0,-17.262248)"><path fill="#4032a8" d="m 9.028871e-5,480.0001 v 0 C 9.028871e-5,214.90342 214.90341,1e-4 480.00009,1e-4 v 0 c 127.30393,0 249.39377,50.571285 339.41122,140.58875 90.01752,90.01746 140.58875,212.10733 140.58875,339.41125 v 0 c 0,265.09665 -214.90332,479.99997 -479.99997,479.99997 v 0 C 214.90341,960.00007 9.028871e-5,745.09675 9.028871e-5,480.0001 Z" fill-rule="evenodd"/><path stroke="#ffffff" stroke-width="4" stroke-linejoin="round" stroke-linecap="butt" d="m 60.00009,480.0001 v 0 c 0,-231.9596 188.0404,-420 420,-420 v 0 c 111.3909,0 218.21957,44.24988 296.98483,123.01516 78.76532,78.76527 123.01514,185.59392 123.01514,296.98486 v 0 c 0,231.95956 -188.0404,419.99997 -419.99997,419.99997 v 0 c -231.9596,0 -420,-188.0404 -420,-419.99997 z" fill-rule="evenodd" style="stroke-width:10.6666668;stroke-dasharray:none"/><path stroke="#ffffff" stroke-width="4" stroke-linejoin="round" stroke-linecap="butt" d="m 120.00009,480.0001 v 0 c 0,-198.82251 161.17749,-360 360,-360 v 0 c 95.47794,0 187.04532,37.92846 254.55844,105.44157 67.51312,67.51309 105.44153,159.0805 105.44153,254.55844 v 0 c 0,198.82248 -161.17749,359.99997 -359.99997,359.99997 v 0 c -198.82251,0 -360,-161.17749 -360,-359.99997 z" fill-rule="evenodd" style="stroke-width:10.6666668;stroke-dasharray:none"/><path fill="#ffffff" d="m 319.01666,480.00092 v 0 c 0,-88.90915 72.0751,-160.98425 160.98425,-160.98425 v 0 c 42.69568,0 83.64264,16.96078 113.83307,47.15121 30.19037,30.1904 47.15118,71.13736 47.15118,113.83304 v 0 c 0,88.90918 -72.07507,160.98425 -160.98425,160.98425 v 0 c -88.90915,0 -160.98425,-72.07507 -160.98425,-160.98425 z" fill-rule="evenodd" style="display:inline"/><path stroke="#ffffff" stroke-width="4" stroke-linejoin="round" stroke-linecap="butt" d="m 180.00009,480.0001 v 0 c 0,-165.68542 134.31458,-300 300,-300 v 0 c 79.56497,0 155.87112,31.60704 212.13205,87.86795 56.26092,56.26092 87.86792,132.56711 87.86792,212.13205 v 0 c 0,165.6854 -134.31458,299.99997 -299.99997,299.99997 v 0 c -165.68542,0 -300,-134.31458 -300,-299.99997 z" fill-rule="evenodd" style="stroke-width:10.6666668;stroke-dasharray:none"/><path fill="#ffffff" d="M 427.60658,-273.65095 504.05265,93.386828 478.34507,135.07715 570.21183,450.80446 544.78065,501.65549 662.47712,1028.614 477.52388,620.42282 508.81779,566.37301 393.66837,305.06321 429.7686,227.61648 308.24258,-46.542095 Z" fill-rule="evenodd" style="stroke-width:1.16873" clip-path="url(#clipPath2)"/></g></g></g></svg>
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg viewBox="0 0 254 254" version="1.1" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg"><clipPath clipPathUnits="userSpaceOnUse" id="clippath"><path fill="#4032a8" d="m 9.028871e-5,480.0001 v 0 C 9.028871e-5,214.90342 214.90341,1.0022047e-4 480.00009,1.0022047e-4 v 0 c 127.30393,0 249.39377,50.57128477953 339.41122,140.58874977953 90.01752,90.01746 140.58875,212.10733 140.58875,339.41125 v 0 c 0,265.09665 -214.90332,479.99997 -479.99997,479.99997 v 0 C 214.90341,960.00007 9.028871e-5,745.09675 9.028871e-5,480.0001 Z" fill-rule="evenodd" style="display:inline;fill:#4032a8;fill-opacity:.495496"/></clipPath><g><g style="fill:none;stroke:none;stroke-linecap:square;stroke-miterlimit:10" transform="matrix(0.26458333,0,0,0.26458333,0,4.5672766)"><g clip-path="url(#p.0)" transform="translate(0,-17.262248)"><path fill="#4032a8" d="m 9.028871e-5,480.0001 v 0 C 9.028871e-5,214.90342 214.90341,1e-4 480.00009,1e-4 v 0 c 127.30393,0 249.39377,50.571285 339.41122,140.58875 90.01752,90.01746 140.58875,212.10733 140.58875,339.41125 v 0 c 0,265.09665 -214.90332,479.99997 -479.99997,479.99997 v 0 C 214.90341,960.00007 9.028871e-5,745.09675 9.028871e-5,480.0001 Z" fill-rule="evenodd"/><path stroke="#ffffff" stroke-width="4" stroke-linejoin="round" stroke-linecap="butt" d="m 60.00009,480.0001 v 0 c 0,-231.9596 188.0404,-420 420,-420 v 0 c 111.3909,0 218.21957,44.24988 296.98483,123.01516 78.76532,78.76527 123.01514,185.59392 123.01514,296.98486 v 0 c 0,231.95956 -188.0404,419.99997 -419.99997,419.99997 v 0 c -231.9596,0 -420,-188.0404 -420,-419.99997 z" fill-rule="evenodd" style="stroke-width:10.6666668;stroke-dasharray:none"/><path stroke="#ffffff" stroke-width="4" stroke-linejoin="round" stroke-linecap="butt" d="m 120.00009,480.0001 v 0 c 0,-198.82251 161.17749,-360 360,-360 v 0 c 95.47794,0 187.04532,37.92846 254.55844,105.44157 67.51312,67.51309 105.44153,159.0805 105.44153,254.55844 v 0 c 0,198.82248 -161.17749,359.99997 -359.99997,359.99997 v 0 c -198.82251,0 -360,-161.17749 -360,-359.99997 z" fill-rule="evenodd" style="stroke-width:10.6666668;stroke-dasharray:none"/><path fill="#ffffff" d="m 319.01666,480.00092 v 0 c 0,-88.90915 72.0751,-160.98425 160.98425,-160.98425 v 0 c 42.69568,0 83.64264,16.96078 113.83307,47.15121 30.19037,30.1904 47.15118,71.13736 47.15118,113.83304 v 0 c 0,88.90918 -72.07507,160.98425 -160.98425,160.98425 v 0 c -88.90915,0 -160.98425,-72.07507 -160.98425,-160.98425 z" fill-rule="evenodd" style="display:inline"/><path stroke="#ffffff" stroke-width="4" stroke-linejoin="round" stroke-linecap="butt" d="m 180.00009,480.0001 v 0 c 0,-165.68542 134.31458,-300 300,-300 v 0 c 79.56497,0 155.87112,31.60704 212.13205,87.86795 56.26092,56.26092 87.86792,132.56711 87.86792,212.13205 v 0 c 0,165.6854 -134.31458,299.99997 -299.99997,299.99997 v 0 c -165.68542,0 -300,-134.31458 -300,-299.99997 z" fill-rule="evenodd" style="stroke-width:10.6666668;stroke-dasharray:none"/><path fill="#ffffff" d="M 427.60658,-273.65095 504.05265,93.386828 478.34507,135.07715 570.21183,450.80446 544.78065,501.65549 662.47712,1028.614 477.52388,620.42282 508.81779,566.37301 393.66837,305.06321 429.7686,227.61648 308.24258,-46.542095 Z" fill-rule="evenodd" style="stroke-width:1.16873" clip-path="url(#clippath)"/></g></g></g></svg>
|
||||
|
||||
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
@@ -17,12 +17,12 @@ use crate::util::error::{AuthError, Contextualize, Error, ErrorType};
|
||||
#[post("/api/v1/auth/signup", mut auth: Extension<AuthSession>, db_pool: Extension<DbPool>, config: Extension<Config>)]
|
||||
pub async fn signup(credentials: UserCredentials) -> Result<User> {
|
||||
if !config.auth.open_signup {
|
||||
return Err(Error::new_here(ErrorType::Auth(AuthError::Unauthorized)));
|
||||
return Err(Error::message_here("Signup is disabled"));
|
||||
}
|
||||
|
||||
// Don't allow signup when already logged in
|
||||
if auth.user.is_some() {
|
||||
return Err(Error::new_here(ErrorType::Auth(AuthError::Unauthorized)));
|
||||
return Err(Error::message_here("Log out before creating an account"));
|
||||
}
|
||||
|
||||
let hashed_creds = credentials
|
||||
@@ -86,3 +86,9 @@ pub async fn logout() -> Result<()> {
|
||||
pub async fn get_user() -> Result<Option<User>> {
|
||||
Ok(auth.user.clone().map(Into::into))
|
||||
}
|
||||
|
||||
/// Check if open signup is enabled
|
||||
#[get("/api/v1/auth/open-signup", config: Extension<Config>)]
|
||||
pub async fn open_signup() -> Result<bool> {
|
||||
Ok(config.auth.open_signup)
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ enum Route {
|
||||
#[component]
|
||||
pub fn App() -> Element {
|
||||
rsx! {
|
||||
document::Link { rel: "icon", href: LOGO_ICO }
|
||||
document::Link { rel: "stylesheet", href: TAILWIND_CSS }
|
||||
Router::<Route> {}
|
||||
}
|
||||
|
||||
35
src/components/footer.rs
Normal file
35
src/components/footer.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
use crate::app::LOGO_SVG;
|
||||
|
||||
#[component]
|
||||
pub fn Footer() -> Element {
|
||||
rsx! {
|
||||
footer {
|
||||
class: "footer md:footer-horizontal fixed bottom-0 text-base-content/70 bg-base-300 items-center p-4",
|
||||
|
||||
aside {
|
||||
class: "flex items-center",
|
||||
|
||||
img {
|
||||
class: "w-12 h-12",
|
||||
src: LOGO_SVG,
|
||||
}
|
||||
|
||||
div {
|
||||
p {
|
||||
class: "text-xl font-bold",
|
||||
"LibreTunes",
|
||||
}
|
||||
|
||||
a {
|
||||
class: "text-sm",
|
||||
href: env!("CARGO_PKG_REPOSITORY"),
|
||||
"v" {env!("CARGO_PKG_VERSION")},
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
101
src/components/form.rs
Normal file
101
src/components/form.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
use dioxus::prelude::*;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::util::error::Error;
|
||||
|
||||
/// A simple styled form card. Parses form fields into the provided generic type, and calls the
|
||||
/// callback when submitted via button or enter key.
|
||||
#[component]
|
||||
pub fn Form<T>(
|
||||
/// Heading above the form
|
||||
title: String,
|
||||
/// Form content, placed inside a `fieldset`
|
||||
children: Element,
|
||||
/// Text displayed on the form button
|
||||
action_message: String,
|
||||
/// Form-related error. Set to a parsing error on parse failure, or can be used to display any
|
||||
/// submission error. Clears on each submit. Displays as a card between the inputs and action
|
||||
/// button, opens a modal on click.
|
||||
#[props(default)]
|
||||
error: Signal<Option<Error>>,
|
||||
/// Whether the form should display in a "loading" state, disables the action button and
|
||||
/// displays a loading animation instead. For best UX, especially on pages the user might open
|
||||
/// directly as opposed to navigate to within the app, initialize to true. This component will
|
||||
/// set it to false when run. This sequence will discourage users from submitting the form
|
||||
/// before the page is ready to handle it. Can also be used to indicate background work is
|
||||
/// taking place in the `callback`, but this is not done automatically.
|
||||
#[props(default)]
|
||||
loading: Signal<bool>,
|
||||
/// Content to display below the action button
|
||||
#[props(default)]
|
||||
extra_content: Option<Element>,
|
||||
/// Form submission callback. Called with the deserialized form inputs.
|
||||
onsubmit: Callback<T>,
|
||||
) -> Element
|
||||
where
|
||||
T: DeserializeOwned + 'static,
|
||||
{
|
||||
// Button is initialized to loading, then set back when WASM is finished loading (use_effect
|
||||
// runs only on the client)
|
||||
use_effect(move || loading.set(false));
|
||||
|
||||
rsx! {
|
||||
div {
|
||||
class: "card card-xl w-full sm:w-100 sm:h-fit bg-base-200",
|
||||
|
||||
form {
|
||||
onsubmit: move |evt| {
|
||||
evt.prevent_default();
|
||||
|
||||
let data: T = match evt.data.parsed_values() {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
let e = Error::message_here(e.to_string())
|
||||
.with_context("Failed to parse form inputs");
|
||||
|
||||
error.set(Some(e));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
error.set(None);
|
||||
|
||||
onsubmit.call(data);
|
||||
},
|
||||
|
||||
class: "card-body gap-3",
|
||||
|
||||
h1 {
|
||||
class: "card-title",
|
||||
{title}
|
||||
}
|
||||
|
||||
fieldset {
|
||||
class: "fieldset pt-0 gap-1",
|
||||
{children}
|
||||
}
|
||||
|
||||
{error().map(|e| e.as_alert())}
|
||||
|
||||
div {
|
||||
class: "card-actions gap-4",
|
||||
|
||||
button {
|
||||
class: "btn btn-primary btn-block",
|
||||
disabled: loading(),
|
||||
|
||||
if loading() {
|
||||
span {
|
||||
class: "loading loading-dots"
|
||||
}
|
||||
} else {
|
||||
{action_message}
|
||||
}
|
||||
}
|
||||
|
||||
{extra_content}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
68
src/components/form_input.rs
Normal file
68
src/components/form_input.rs
Normal file
@@ -0,0 +1,68 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
/// A simple styled input box
|
||||
#[component]
|
||||
pub fn FormInput(
|
||||
/// The placeholder text and label. When text is entered in the input, the label is shown above
|
||||
/// the input box.
|
||||
label: String,
|
||||
#[props(default)] name: Option<String>,
|
||||
#[props(default)] required: bool,
|
||||
#[props(default)] r#type: String,
|
||||
children: Element,
|
||||
) -> Element {
|
||||
let name = name.unwrap_or(label.to_lowercase());
|
||||
|
||||
rsx! {
|
||||
div {
|
||||
class: "group/field",
|
||||
|
||||
p {
|
||||
class: "opacity-0 not-group-has-placeholder-shown/field:opacity-70 transition-opacity pl-2",
|
||||
{label.clone()}
|
||||
}
|
||||
|
||||
label {
|
||||
class: "input w-full",
|
||||
|
||||
{children}
|
||||
|
||||
input {
|
||||
name,
|
||||
required,
|
||||
r#type,
|
||||
placeholder: label,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn UsernameInput() -> Element {
|
||||
rsx! {
|
||||
FormInput {
|
||||
label: "Username",
|
||||
required: true,
|
||||
|
||||
lucide_dioxus::UserRound {
|
||||
class: "opacity-50",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn PasswordInput() -> Element {
|
||||
rsx! {
|
||||
FormInput {
|
||||
label: "Password",
|
||||
required: true,
|
||||
r#type: "password",
|
||||
|
||||
lucide_dioxus::KeyRound {
|
||||
class: "opacity-50",
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1 +1,7 @@
|
||||
pub mod footer;
|
||||
pub mod form;
|
||||
pub mod form_input;
|
||||
|
||||
pub use footer::*;
|
||||
pub use form::*;
|
||||
pub use form_input::*;
|
||||
|
||||
@@ -16,6 +16,9 @@ use crate::app::App;
|
||||
fn tracing_setup() {
|
||||
#[cfg(debug_assertions)]
|
||||
dioxus::logger::init(tracing::Level::DEBUG).expect("Failed to initialize tracing logger");
|
||||
|
||||
#[cfg(not(debug_assertions))]
|
||||
dioxus::logger::init(tracing::Level::INFO).expect("Failed to initialize tracing logger");
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "server"))]
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
/// Enable secure cookies by default only in release mode
|
||||
@@ -141,12 +143,18 @@ impl KeyValStoreConfig {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct ServerConfig {
|
||||
pub public_path: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
/// Top-level application configuration
|
||||
pub struct Config {
|
||||
pub auth: AuthConfig,
|
||||
pub database: DatabaseConfig,
|
||||
pub key_val_store: KeyValStoreConfig,
|
||||
pub server: ServerConfig,
|
||||
}
|
||||
|
||||
/// Parse configuration from the expected files and environment variables
|
||||
@@ -159,6 +167,7 @@ pub fn load_config() -> Result<Config, config::ConfigError> {
|
||||
.set_default("server.port", 8080)?
|
||||
.set_default("auth.open_signup", false)?
|
||||
.set_default("auth.cookies_secure", DEFAULT_COOKIES_SECURE)?
|
||||
.set_default("server.public_path", default_public_dir())?
|
||||
.add_source(File::with_name(&format!("/etc/{pkg_name}/config")).required(false))
|
||||
.add_source(File::with_name(&format!("/etc/{pkg_name}")).required(false))
|
||||
.add_source(File::with_name("config").required(false))
|
||||
@@ -166,3 +175,19 @@ pub fn load_config() -> Result<Config, config::ConfigError> {
|
||||
.build()?
|
||||
.try_deserialize()
|
||||
}
|
||||
|
||||
/// Provide a sane default for the public path, using the same sources as Dioxus does internally.
|
||||
/// Checks the `DIOXUS_PUBLIC_PATH` environment variable, then tries relative to the path of this
|
||||
/// executable
|
||||
fn default_public_dir() -> Option<String> {
|
||||
std::env::var("DIOXUS_PUBLIC_PATH").ok().or_else(|| {
|
||||
std::env::current_exe().ok().and_then(|path| {
|
||||
path.parent()
|
||||
.expect("current executable path must have a parent")
|
||||
.join("public")
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.ok()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,10 +2,14 @@ use dioxus::{
|
||||
fullstack::axum::{Router, middleware::from_fn},
|
||||
server::axum::Extension,
|
||||
};
|
||||
use tower_http::services::ServeFile;
|
||||
|
||||
use crate::App;
|
||||
use crate::app::LOGO_ICO;
|
||||
use crate::server::{
|
||||
auth::build_auth_layer, config, database, key_val_store,
|
||||
auth::build_auth_layer,
|
||||
config::{self, Config},
|
||||
database, key_val_store,
|
||||
require_auth_mw::require_auth_middleware,
|
||||
};
|
||||
use crate::util::error::{Contextualize, Error, Result};
|
||||
@@ -15,17 +19,28 @@ pub fn main() -> Result<std::convert::Infallible> {
|
||||
tracing::warn!("Error reading .env: {e}");
|
||||
}
|
||||
|
||||
// `Ok(...?)` is because `dioxus::serve` expects an `anyhow::Result`
|
||||
dioxus::serve(async move || Ok(router_setup().await?));
|
||||
}
|
||||
|
||||
/// Set up the axum Router
|
||||
async fn router_setup() -> Result<Router> {
|
||||
tracing::debug!("Loading configuration...");
|
||||
let config = config::load_config()
|
||||
.map_err(|e| Error::message_here(e.to_string()))
|
||||
.err_context("Failed to load config")?;
|
||||
|
||||
// Dioxus doesn't expose a way to configure the public path other than this environment
|
||||
// variable, and also doesn't provide a way to read what the public path is. As a workaround we
|
||||
// expose a config option and set this variable that Dioxus expects.
|
||||
// SAFETY: "This function is safe to call in a single-threaded program."
|
||||
unsafe {
|
||||
std::env::set_var("DIOXUS_PUBLIC_PATH", config.server.public_path.clone());
|
||||
}
|
||||
|
||||
// `Ok(...?)` is because `dioxus::serve` expects an `anyhow::Result`
|
||||
dioxus::serve(move || {
|
||||
let config = config.clone();
|
||||
async move { Ok(router_setup(config).await?) }
|
||||
});
|
||||
}
|
||||
|
||||
/// Set up the axum Router
|
||||
async fn router_setup(config: Config) -> Result<Router> {
|
||||
let db_pool = database::setup(config.database.connection_uri())
|
||||
.await
|
||||
.err_context("Failed database setup")?;
|
||||
@@ -34,13 +49,28 @@ async fn router_setup() -> Result<Router> {
|
||||
.await
|
||||
.err_context("Failed key-value store setup")?;
|
||||
|
||||
let favicon_path = {
|
||||
// Resolve the favicon path
|
||||
let asset_path = LOGO_ICO.resolve();
|
||||
|
||||
// If the asset path starts with "/", strip it. Otherwise it behaves as an "absolute path"
|
||||
// and replaces the base path in the join operation. This is necessary because Dioxus will
|
||||
// produce a path like "/assets/" in the call to `resolve`
|
||||
let asset_path_rel = asset_path.strip_prefix("/").unwrap_or(&asset_path);
|
||||
|
||||
config.server.public_path.join(asset_path_rel)
|
||||
};
|
||||
|
||||
tracing::debug!("Favicon path: {}", favicon_path.display());
|
||||
|
||||
let auth_layer = build_auth_layer(db_pool.clone(), key_val_pool, config.auth.cookies_secure);
|
||||
|
||||
let router = dioxus::server::router(App)
|
||||
.layer(from_fn(require_auth_middleware))
|
||||
.layer(Extension(config))
|
||||
.layer(Extension(db_pool))
|
||||
.layer(auth_layer);
|
||||
.layer(auth_layer)
|
||||
.nest_service("/favicon.ico", ServeFile::new(favicon_path));
|
||||
|
||||
tracing::info!("Setup complete, returning Router...");
|
||||
Ok(router)
|
||||
|
||||
@@ -19,7 +19,7 @@ const ALLOWED_PATHS: [&str; 6] = [
|
||||
"/api/v1/auth/login",
|
||||
"/api/v1/auth/signup",
|
||||
"/api/v1/auth/user",
|
||||
"/favicon.ico",
|
||||
"/api/v1/auth/open-signup",
|
||||
];
|
||||
|
||||
/// Axum middleware to redirect an unauthenticated request to /login unless it matches one of the allowed paths
|
||||
|
||||
@@ -45,6 +45,20 @@ impl fmt::Display for ErrorLocation {
|
||||
|
||||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||
|
||||
/// Generate a random string to use as an id for the modal
|
||||
/// This allows multiple toast/modal to be present
|
||||
fn rand_modal_id() -> String {
|
||||
use rand::RngExt;
|
||||
|
||||
let mut rng = rand::rng();
|
||||
|
||||
let random_str = (0..5)
|
||||
.map(|_| rng.sample(rand::distr::Alphanumeric) as char)
|
||||
.collect::<String>();
|
||||
|
||||
format!("err-modal-{random_str}")
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, thiserror::Error)]
|
||||
pub struct Error {
|
||||
#[source]
|
||||
@@ -97,6 +111,12 @@ impl Error {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the original cause of this error
|
||||
/// Like `std::error::Error::source` but doesn't return an `Option`
|
||||
pub fn source(&self) -> &ErrorType {
|
||||
&self.source
|
||||
}
|
||||
|
||||
/// Display this error as a modal dialog activated by a checkbox with the given id
|
||||
pub fn as_modal(&self, id: String) -> Element {
|
||||
rsx! {
|
||||
@@ -173,42 +193,46 @@ impl Error {
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert this error into an alert label, tied to the given modal id
|
||||
fn as_alert_for(&self, modal_id: String) -> Element {
|
||||
rsx! {
|
||||
label {
|
||||
class: "alert alert-error alert-soft cursor-pointer",
|
||||
role: "alert",
|
||||
r#for: modal_id,
|
||||
lucide_dioxus::CircleAlert {}
|
||||
|
||||
p {
|
||||
class: "max-w-120 text-ellipsis line-clamp-3",
|
||||
{self.top_message()}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert this error to a toast message, which opens a modal when clicked
|
||||
pub fn as_toast(&self) -> Element {
|
||||
// Generate a random string to use as an id for the modal
|
||||
// This allows multiple toast/modal to be present
|
||||
let modal_id = {
|
||||
use rand::RngExt;
|
||||
|
||||
let mut rng = rand::rng();
|
||||
|
||||
let random_str = (0..5)
|
||||
.map(|_| rng.sample(rand::distr::Alphanumeric) as char)
|
||||
.collect::<String>();
|
||||
|
||||
format!("err-modal-{random_str}")
|
||||
};
|
||||
let modal_id = rand_modal_id();
|
||||
|
||||
rsx! {
|
||||
{self.as_modal(modal_id.clone())}
|
||||
|
||||
div {
|
||||
class: "toast z-99",
|
||||
|
||||
label {
|
||||
class: "alert alert-error alert-soft cursor-pointer",
|
||||
role: "alert",
|
||||
r#for: modal_id,
|
||||
lucide_dioxus::CircleAlert {}
|
||||
|
||||
p {
|
||||
class: "max-w-120 text-ellipsis line-clamp-3",
|
||||
{self.top_message()}
|
||||
}
|
||||
}
|
||||
{self.as_alert_for(modal_id)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert this error into an alert label, which opens a modal when clicked
|
||||
pub fn as_alert(&self) -> Element {
|
||||
let modal_id = rand_modal_id();
|
||||
|
||||
rsx! {
|
||||
{self.as_modal(modal_id.clone())}
|
||||
{self.as_alert_for(modal_id)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
|
||||
Reference in New Issue
Block a user