Async is hard

This commit is contained in:
Daniel Flanagan 2023-11-13 17:27:19 -06:00
parent aaf2f3d1ac
commit 05a62bb456
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
7 changed files with 77 additions and 3 deletions

19
Cargo.lock generated
View file

@ -146,6 +146,18 @@ dependencies = [
"tower-service", "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]] [[package]]
name = "axum_csrf" name = "axum_csrf"
version = "0.8.0" version = "0.8.0"
@ -574,6 +586,12 @@ dependencies = [
"http", "http",
] ]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.3" version = "0.3.3"
@ -762,6 +780,7 @@ dependencies = [
"anyhow", "anyhow",
"argon2", "argon2",
"axum", "axum",
"axum-macros",
"axum_csrf", "axum_csrf",
"base64", "base64",
"cookie", "cookie",

View file

@ -31,4 +31,5 @@ notify = "6.1.1"
tower-livereload = "0.8.2" tower-livereload = "0.8.2"
argon2 = { version = "0.5.2", features = ["std"] } argon2 = { version = "0.5.2", features = ["std"] }
thiserror = "1.0.50" thiserror = "1.0.50"
axum-macros = "0.3.8"

View file

@ -1,2 +1,2 @@
[toolchain] [toolchain]
channel = "1.73.0" channel = "nightly"

View file

@ -3,6 +3,7 @@ use diesel::sqlite::Sqlite;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use tracing::info; use tracing::info;
#[derive(Clone)]
pub struct Database { pub struct Database {
pub pool: Pool, pub pool: Pool,
} }

View file

@ -49,6 +49,7 @@ pub async fn new() -> Result<Router, anyhow::Error> {
.nest_service("/assets", assets_dir) .nest_service("/assets", assets_dir)
.route("/", get(views::index)) .route("/", get(views::index))
.route("/register", get(views::register).post(register)) .route("/register", get(views::register).post(register))
.route("/all_users", get(views::all_users))
.with_state(state) .with_state(state)
.layer(CsrfLayer::new(csrf_config)) .layer(CsrfLayer::new(csrf_config))
.layer(live_reload_layer); .layer(live_reload_layer);

View file

@ -2,6 +2,7 @@ use std::env;
use crate::database; use crate::database;
#[derive(Clone)]
pub struct State { pub struct State {
pub database: database::Database, pub database: database::Database,
} }

View file

@ -1,9 +1,18 @@
use axum::response::{Html, IntoResponse}; use axum::{
extract::State,
response::{Html, IntoResponse},
};
use axum_csrf::CsrfToken; use axum_csrf::CsrfToken;
use axum_macros::debug_handler;
use diesel::{QueryDsl, RunQueryDsl, SelectableHelper};
use maud::html; use maud::html;
use tracing::instrument; use tracing::instrument;
use crate::partials::{footer, header}; use crate::{
error::AppError,
models::User,
partials::{footer, header},
};
#[instrument] #[instrument]
pub async fn index() -> Html<String> { pub async fn index() -> Html<String> {
@ -89,6 +98,48 @@ pub async fn login(csrf: CsrfToken) -> impl IntoResponse {
.into_response() .into_response()
} }
#[debug_handler]
pub async fn all_users(
State(state): State<crate::state::State>,
) -> Result<impl IntoResponse, AppError> {
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] #[instrument]
pub async fn greet_world() -> Html<String> { pub async fn greet_world() -> Html<String> {
Html( Html(