Async is hard
This commit is contained in:
parent
aaf2f3d1ac
commit
05a62bb456
7 changed files with 77 additions and 3 deletions
19
Cargo.lock
generated
19
Cargo.lock
generated
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
[toolchain]
|
||||
channel = "1.73.0"
|
||||
channel = "nightly"
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -49,6 +49,7 @@ pub async fn new() -> Result<Router, anyhow::Error> {
|
|||
.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);
|
||||
|
|
|
@ -2,6 +2,7 @@ use std::env;
|
|||
|
||||
use crate::database;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct State {
|
||||
pub database: database::Database,
|
||||
}
|
||||
|
|
55
src/views.rs
55
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<String> {
|
||||
|
@ -89,6 +98,48 @@ pub async fn login(csrf: CsrfToken) -> impl IntoResponse {
|
|||
.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]
|
||||
pub async fn greet_world() -> Html<String> {
|
||||
Html(
|
||||
|
|
Loading…
Reference in a new issue