From 05a62bb456bb16e5c90a34fb007f2fe91b430a64 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Mon, 13 Nov 2023 17:27:19 -0600 Subject: [PATCH] Async is hard --- Cargo.lock | 19 ++++++++++++++++ Cargo.toml | 1 + rust-toolchain.toml | 2 +- src/database.rs | 1 + src/router.rs | 1 + src/state.rs | 1 + src/views.rs | 55 +++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 77 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0929654..6c0e5a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -146,6 +146,18 @@ dependencies = [ "tower-service", ] +[[package]] +name = "axum-macros" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdca6a10ecad987bda04e95606ef85a5417dcaac1a78455242d72e031e2b6b62" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.39", +] + [[package]] name = "axum_csrf" version = "0.8.0" @@ -574,6 +586,12 @@ dependencies = [ "http", ] +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.3" @@ -762,6 +780,7 @@ dependencies = [ "anyhow", "argon2", "axum", + "axum-macros", "axum_csrf", "base64", "cookie", diff --git a/Cargo.toml b/Cargo.toml index 8b448fd..d073c6d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,4 +31,5 @@ notify = "6.1.1" tower-livereload = "0.8.2" argon2 = { version = "0.5.2", features = ["std"] } thiserror = "1.0.50" +axum-macros = "0.3.8" diff --git a/rust-toolchain.toml b/rust-toolchain.toml index 8142c30..5d56faf 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,2 +1,2 @@ [toolchain] -channel = "1.73.0" +channel = "nightly" diff --git a/src/database.rs b/src/database.rs index 7ea11b9..3d52c04 100644 --- a/src/database.rs +++ b/src/database.rs @@ -3,6 +3,7 @@ use diesel::sqlite::Sqlite; use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; use tracing::info; +#[derive(Clone)] pub struct Database { pub pool: Pool, } diff --git a/src/router.rs b/src/router.rs index 1901ce4..6589115 100644 --- a/src/router.rs +++ b/src/router.rs @@ -49,6 +49,7 @@ pub async fn new() -> Result { .nest_service("/assets", assets_dir) .route("/", get(views::index)) .route("/register", get(views::register).post(register)) + .route("/all_users", get(views::all_users)) .with_state(state) .layer(CsrfLayer::new(csrf_config)) .layer(live_reload_layer); diff --git a/src/state.rs b/src/state.rs index 7cae4c1..5dce4fb 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2,6 +2,7 @@ use std::env; use crate::database; +#[derive(Clone)] pub struct State { pub database: database::Database, } diff --git a/src/views.rs b/src/views.rs index 4defefb..a6a190c 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,9 +1,18 @@ -use axum::response::{Html, IntoResponse}; +use axum::{ + extract::State, + response::{Html, IntoResponse}, +}; use axum_csrf::CsrfToken; +use axum_macros::debug_handler; +use diesel::{QueryDsl, RunQueryDsl, SelectableHelper}; use maud::html; use tracing::instrument; -use crate::partials::{footer, header}; +use crate::{ + error::AppError, + models::User, + partials::{footer, header}, +}; #[instrument] pub async fn index() -> Html { @@ -89,6 +98,48 @@ pub async fn login(csrf: CsrfToken) -> impl IntoResponse { .into_response() } +#[debug_handler] +pub async fn all_users( + State(state): State, +) -> Result { + use crate::schema::users::dsl::*; + + let conn = state.database.pool.get().await?; + let cc = |c| { + users + .select(User::as_select()) + .load(c) + .expect("error loading users") + }; + + let all_users = conn.interact(cc).await?; + + // @if let Some(name) = u.name { + // name + // } @else { + // "N/A" + // } + Ok(Html( + html! { + (header()) + main class="prose" { + h1 { "Users" } + ul { + @for u in &all_users { + li { + (u.username) + "(" + ")" + } + } + } + } + (footer()) + } + .into_string(), + )) +} + #[instrument] pub async fn greet_world() -> Html { Html(