Compare commits

..

18 Commits

Author SHA1 Message Date
d5eba9620c Link to favicon
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 6s
Push Workflows / docs (push) Successful in 20s
Push Workflows / test (push) Successful in 23s
Push Workflows / clippy (push) Successful in 18s
Push Workflows / build (push) Successful in 47s
Push Workflows / nix-build (push) Successful in 6m24s
2026-07-11 12:41:03 -04:00
39afdd83a7 Add username and password form input presets
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 6s
Push Workflows / docs (push) Successful in 31s
Push Workflows / clippy (push) Successful in 28s
Push Workflows / test (push) Successful in 35s
Push Workflows / build (push) Successful in 1m12s
Push Workflows / nix-build (push) Successful in 6m24s
2026-07-11 12:37:30 -04:00
34b887730a Enable account icons in Lucide 2026-07-11 12:37:30 -04:00
f2dc4c908e Export individual components from crate::components 2026-07-11 12:28:35 -04:00
219f1f5e7d Add open-signup to list of allowed endpoints 2026-07-11 12:26:28 -04:00
102387fdad Initialize tracing to INFO in release mode 2026-07-11 12:21:37 -04:00
87bd1b50a9 Add form component
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 6s
Push Workflows / docs (push) Successful in 49s
Push Workflows / clippy (push) Successful in 1m19s
Push Workflows / test (push) Successful in 1m30s
Push Workflows / build (push) Successful in 2m24s
Push Workflows / nix-build (push) Successful in 5m35s
2026-07-08 21:33:04 -04:00
c8cd4b95dc Add dioxus-html 2026-07-08 21:33:04 -04:00
8348277a87 Add endpoint to check open signup 2026-07-08 20:47:58 -04:00
e7c43e7a47 Allow icon in form input component
Misc style adjustments
2026-07-08 20:44:38 -04:00
4bacbfd1ab Add FormInput component
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 4s
Push Workflows / docs (push) Successful in 19s
Push Workflows / clippy (push) Successful in 17s
Push Workflows / test (push) Successful in 25s
Push Workflows / build (push) Successful in 51s
Push Workflows / nix-build (push) Successful in 5m13s
2026-06-30 21:29:20 -04:00
fa9ea95657 Don't check for /favicon.ico path in require_auth_mw
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 5s
Push Workflows / clippy (push) Successful in 28s
Push Workflows / test (push) Successful in 40s
Push Workflows / docs (push) Successful in 41s
Push Workflows / build (push) Successful in 1m50s
Push Workflows / nix-build (push) Successful in 5m17s
The server returns the icon before this middleware is reached
2026-06-30 21:17:37 -04:00
3ec07c0469 Serve favicon on /favicon.ico 2026-06-30 21:16:52 -04:00
f41f4f8d80 Set DIOXUS_PUBLIC_PATH to configured value 2026-06-30 21:11:26 -04:00
c9b7bbfece Enable fs feature on tower-http 2026-06-30 21:10:29 -04:00
9de0b13ee8 Add tower-http 2026-06-30 21:09:02 -04:00
8b00db60eb Add public_path config option 2026-06-30 21:08:05 -04:00
69eb3a5e80 Move loading config outside of router setup
All checks were successful
Push Workflows / rustfmt (push) Successful in 5s
Push Workflows / tailwind-build (push) Successful in 6s
Push Workflows / clippy (push) Successful in 18s
Push Workflows / docs (push) Successful in 23s
Push Workflows / test (push) Successful in 26s
Push Workflows / build (push) Successful in 54s
Push Workflows / nix-build (push) Successful in 5m54s
2026-06-30 21:04:16 -04:00
11 changed files with 283 additions and 13 deletions

33
Cargo.lock generated
View File

@@ -1096,7 +1096,7 @@ dependencies = [
"tokio-tungstenite 0.28.0", "tokio-tungstenite 0.28.0",
"tokio-util", "tokio-util",
"tower", "tower",
"tower-http", "tower-http 0.6.11",
"tower-layer", "tower-layer",
"tracing", "tracing",
"tungstenite 0.27.0", "tungstenite 0.27.0",
@@ -1374,7 +1374,7 @@ dependencies = [
"tokio-tungstenite 0.28.0", "tokio-tungstenite 0.28.0",
"tokio-util", "tokio-util",
"tower", "tower",
"tower-http", "tower-http 0.6.11",
"tracing", "tracing",
"tracing-futures", "tracing-futures",
"url", "url",
@@ -2419,6 +2419,7 @@ dependencies = [
"diesel-async", "diesel-async",
"diesel_migrations", "diesel_migrations",
"dioxus", "dioxus",
"dioxus-html",
"dotenvy", "dotenvy",
"fred", "fred",
"getrandom 0.4.3", "getrandom 0.4.3",
@@ -2427,6 +2428,7 @@ dependencies = [
"rand 0.10.1", "rand 0.10.1",
"serde", "serde",
"thiserror 2.0.18", "thiserror 2.0.18",
"tower-http 0.7.0",
"tower-sessions-redis-store", "tower-sessions-redis-store",
"tracing", "tracing",
] ]
@@ -3293,7 +3295,7 @@ dependencies = [
"tokio-rustls", "tokio-rustls",
"tokio-util", "tokio-util",
"tower", "tower",
"tower-http", "tower-http 0.6.11",
"tower-service", "tower-service",
"url", "url",
"wasm-bindgen", "wasm-bindgen",
@@ -4182,6 +4184,31 @@ dependencies = [
"url", "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]] [[package]]
name = "tower-layer" name = "tower-layer"
version = "0.3.3" version = "0.3.3"

View File

@@ -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-async = { version = "0.9.1", optional = true, features = ["postgres", "deadpool", "migrations"] }
diesel_migrations = { version = "2.3.2", optional = true } diesel_migrations = { version = "2.3.2", optional = true }
dioxus = { version = "0.7.9", features = ["router", "fullstack"] } dioxus = { version = "0.7.9", features = ["router", "fullstack"] }
dioxus-html = "0.7.9"
dotenvy = { version = "0.15.7", optional = true } dotenvy = { version = "0.15.7", optional = true }
fred = { version = "10.1.0", 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"] } pbkdf2 = { version = "0.13.0", optional = true, features = ["getrandom", "phc"] }
rand = "0.10.1" rand = "0.10.1"
serde = { version = "1.0.228", features = ["derive"] } serde = { version = "1.0.228", features = ["derive"] }
thiserror = "2.0.18" thiserror = "2.0.18"
tower-http = { version = "0.7.0", optional = true, features = ["fs"] }
tower-sessions-redis-store = { version = "0.16.0", optional = true } tower-sessions-redis-store = { version = "0.16.0", optional = true }
tracing = "0.1.44" tracing = "0.1.44"
@@ -40,6 +42,7 @@ server = [
"dep:dotenvy", "dep:dotenvy",
"dep:fred", "dep:fred",
"dep:pbkdf2", "dep:pbkdf2",
"dep:tower-http",
"dep:tower-sessions-redis-store", "dep:tower-sessions-redis-store",
] ]

View File

@@ -86,3 +86,9 @@ pub async fn logout() -> Result<()> {
pub async fn get_user() -> Result<Option<User>> { pub async fn get_user() -> Result<Option<User>> {
Ok(auth.user.clone().map(Into::into)) 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)
}

View File

@@ -14,6 +14,7 @@ enum Route {
#[component] #[component]
pub fn App() -> Element { pub fn App() -> Element {
rsx! { rsx! {
document::Link { rel: "icon", href: LOGO_ICO }
document::Link { rel: "stylesheet", href: TAILWIND_CSS } document::Link { rel: "stylesheet", href: TAILWIND_CSS }
Router::<Route> {} Router::<Route> {}
} }

101
src/components/form.rs Normal file
View 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}
}
}
}
}
}

View 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",
}
}
}
}

View File

@@ -1 +1,7 @@
pub mod footer; pub mod footer;
pub mod form;
pub mod form_input;
pub use footer::*;
pub use form::*;
pub use form_input::*;

View File

@@ -16,6 +16,9 @@ use crate::app::App;
fn tracing_setup() { fn tracing_setup() {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
dioxus::logger::init(tracing::Level::DEBUG).expect("Failed to initialize tracing logger"); 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"))] #[cfg(not(feature = "server"))]

View File

@@ -1,3 +1,5 @@
use std::path::PathBuf;
use serde::Deserialize; use serde::Deserialize;
/// Enable secure cookies by default only in release mode /// 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)] #[derive(Debug, Clone, Deserialize)]
/// Top-level application configuration /// Top-level application configuration
pub struct Config { pub struct Config {
pub auth: AuthConfig, pub auth: AuthConfig,
pub database: DatabaseConfig, pub database: DatabaseConfig,
pub key_val_store: KeyValStoreConfig, pub key_val_store: KeyValStoreConfig,
pub server: ServerConfig,
} }
/// Parse configuration from the expected files and environment variables /// 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("server.port", 8080)?
.set_default("auth.open_signup", false)? .set_default("auth.open_signup", false)?
.set_default("auth.cookies_secure", DEFAULT_COOKIES_SECURE)? .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}/config")).required(false))
.add_source(File::with_name(&format!("/etc/{pkg_name}")).required(false)) .add_source(File::with_name(&format!("/etc/{pkg_name}")).required(false))
.add_source(File::with_name("config").required(false)) .add_source(File::with_name("config").required(false))
@@ -166,3 +175,19 @@ pub fn load_config() -> Result<Config, config::ConfigError> {
.build()? .build()?
.try_deserialize() .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()
})
})
}

View File

@@ -2,10 +2,14 @@ use dioxus::{
fullstack::axum::{Router, middleware::from_fn}, fullstack::axum::{Router, middleware::from_fn},
server::axum::Extension, server::axum::Extension,
}; };
use tower_http::services::ServeFile;
use crate::App; use crate::App;
use crate::app::LOGO_ICO;
use crate::server::{ 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, require_auth_mw::require_auth_middleware,
}; };
use crate::util::error::{Contextualize, Error, Result}; use crate::util::error::{Contextualize, Error, Result};
@@ -15,17 +19,28 @@ pub fn main() -> Result<std::convert::Infallible> {
tracing::warn!("Error reading .env: {e}"); 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..."); tracing::debug!("Loading configuration...");
let config = config::load_config() let config = config::load_config()
.map_err(|e| Error::message_here(e.to_string())) .map_err(|e| Error::message_here(e.to_string()))
.err_context("Failed to load config")?; .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()) let db_pool = database::setup(config.database.connection_uri())
.await .await
.err_context("Failed database setup")?; .err_context("Failed database setup")?;
@@ -34,13 +49,28 @@ async fn router_setup() -> Result<Router> {
.await .await
.err_context("Failed key-value store setup")?; .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 auth_layer = build_auth_layer(db_pool.clone(), key_val_pool, config.auth.cookies_secure);
let router = dioxus::server::router(App) let router = dioxus::server::router(App)
.layer(from_fn(require_auth_middleware)) .layer(from_fn(require_auth_middleware))
.layer(Extension(config)) .layer(Extension(config))
.layer(Extension(db_pool)) .layer(Extension(db_pool))
.layer(auth_layer); .layer(auth_layer)
.nest_service("/favicon.ico", ServeFile::new(favicon_path));
tracing::info!("Setup complete, returning Router..."); tracing::info!("Setup complete, returning Router...");
Ok(router) Ok(router)

View File

@@ -19,7 +19,7 @@ const ALLOWED_PATHS: [&str; 6] = [
"/api/v1/auth/login", "/api/v1/auth/login",
"/api/v1/auth/signup", "/api/v1/auth/signup",
"/api/v1/auth/user", "/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 /// Axum middleware to redirect an unauthenticated request to /login unless it matches one of the allowed paths