From 0aaeea37e44a86cf86d84a1ab4ff296572e197f7 Mon Sep 17 00:00:00 2001 From: ttyS3 Date: Wed, 7 Dec 2022 01:42:05 +0800 Subject: [PATCH] chore: upgrade to axum 0.6.1 version --- examples/axum_example/api/Cargo.toml | 22 +++++----- examples/axum_example/api/src/lib.rs | 62 ++++++++++++++-------------- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/examples/axum_example/api/Cargo.toml b/examples/axum_example/api/Cargo.toml index caa1cb8c5..69e8abc6f 100644 --- a/examples/axum_example/api/Cargo.toml +++ b/examples/axum_example/api/Cargo.toml @@ -7,16 +7,16 @@ publish = false [dependencies] axum-example-core = { path = "../core" } -tokio = { version = "1.18.1", features = ["full"] } -axum = "0.5.4" -tower = "0.4.12" -tower-http = { version = "0.3.3", features = ["fs"] } -tower-cookies = "0.6.0" -anyhow = "1.0.57" -dotenvy = "0.15.0" -serde = "1.0.137" -serde_json = "1.0.81" -tera = "1.15.0" -tracing-subscriber = { version = "0.3.11", features = ["env-filter"] } +tokio = { version = "1.23.0", features = ["full"] } +axum = "0.6.1" +tower = "0.4.13" +tower-http = { version = "0.3.5", features = ["fs"] } +tower-cookies = "0.8.0" +anyhow = "1.0.66" +dotenvy = "0.15.6" +serde = "1.0.149" +serde_json = "1.0.89" +tera = "1.17.1" +tracing-subscriber = { version = "0.3.16", features = ["env-filter"] } entity = { path = "../entity" } migration = { path = "../migration" } diff --git a/examples/axum_example/api/src/lib.rs b/examples/axum_example/api/src/lib.rs index edeee6619..ea4cd2c97 100644 --- a/examples/axum_example/api/src/lib.rs +++ b/examples/axum_example/api/src/lib.rs @@ -1,7 +1,7 @@ mod flash; use axum::{ - extract::{Extension, Form, Path, Query}, + extract::{Form, Path, Query, State}, http::StatusCode, response::Html, routing::{get, get_service, post}, @@ -18,7 +18,6 @@ use serde::{Deserialize, Serialize}; use std::str::FromStr; use std::{env, net::SocketAddr}; use tera::Tera; -use tower::ServiceBuilder; use tower_cookies::{CookieManagerLayer, Cookies}; use tower_http::services::ServeDir; @@ -37,16 +36,18 @@ async fn start() -> anyhow::Result<()> { .await .expect("Database connection failed"); Migrator::up(&conn, None).await.unwrap(); + let templates = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")) .expect("Tera initialization failed"); - // let state = AppState { templates, conn }; + + let state = AppState { templates, conn }; let app = Router::new() .route("/", get(list_posts).post(create_post)) .route("/:id", get(edit_post).post(update_post)) .route("/new", get(new_post)) .route("/delete/:id", post(delete_post)) - .nest( + .nest_service( "/static", get_service(ServeDir::new(concat!( env!("CARGO_MANIFEST_DIR"), @@ -59,12 +60,8 @@ async fn start() -> anyhow::Result<()> { ) }), ) - .layer( - ServiceBuilder::new() - .layer(CookieManagerLayer::new()) - .layer(Extension(conn)) - .layer(Extension(templates)), - ); + .layer(CookieManagerLayer::new()) + .with_state(state); let addr = SocketAddr::from_str(&server_url).unwrap(); Server::bind(&addr).serve(app.into_make_service()).await?; @@ -72,6 +69,12 @@ async fn start() -> anyhow::Result<()> { Ok(()) } +#[derive(Clone)] +struct AppState { + templates: Tera, + conn: DatabaseConnection, +} + #[derive(Deserialize)] struct Params { page: Option, @@ -85,15 +88,14 @@ struct FlashData { } async fn list_posts( - Extension(ref templates): Extension, - Extension(ref conn): Extension, + state: State, Query(params): Query, cookies: Cookies, ) -> Result, (StatusCode, &'static str)> { let page = params.page.unwrap_or(1); let posts_per_page = params.posts_per_page.unwrap_or(5); - let (posts, num_pages) = QueryCore::find_posts_in_page(conn, page, posts_per_page) + let (posts, num_pages) = QueryCore::find_posts_in_page(&state.conn, page, posts_per_page) .await .expect("Cannot find posts in page"); @@ -107,18 +109,18 @@ async fn list_posts( ctx.insert("flash", &value); } - let body = templates + let body = state + .templates .render("index.html.tera", &ctx) .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?; Ok(Html(body)) } -async fn new_post( - Extension(ref templates): Extension, -) -> Result, (StatusCode, &'static str)> { +async fn new_post(state: State) -> Result, (StatusCode, &'static str)> { let ctx = tera::Context::new(); - let body = templates + let body = state + .templates .render("new.html.tera", &ctx) .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?; @@ -126,13 +128,13 @@ async fn new_post( } async fn create_post( - Extension(ref conn): Extension, - form: Form, + state: State, mut cookies: Cookies, + form: Form, ) -> Result { let form = form.0; - MutationCore::create_post(conn, form) + MutationCore::create_post(&state.conn, form) .await .expect("could not insert post"); @@ -145,11 +147,10 @@ async fn create_post( } async fn edit_post( - Extension(ref templates): Extension, - Extension(ref conn): Extension, + state: State, Path(id): Path, ) -> Result, (StatusCode, &'static str)> { - let post: post::Model = QueryCore::find_post_by_id(conn, id) + let post: post::Model = QueryCore::find_post_by_id(&state.conn, id) .await .expect("could not find post") .unwrap_or_else(|| panic!("could not find post with id {}", id)); @@ -157,7 +158,8 @@ async fn edit_post( let mut ctx = tera::Context::new(); ctx.insert("post", &post); - let body = templates + let body = state + .templates .render("edit.html.tera", &ctx) .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?; @@ -165,14 +167,14 @@ async fn edit_post( } async fn update_post( - Extension(ref conn): Extension, + state: State, Path(id): Path, - form: Form, mut cookies: Cookies, + form: Form, ) -> Result { let form = form.0; - MutationCore::update_post_by_id(conn, id, form) + MutationCore::update_post_by_id(&state.conn, id, form) .await .expect("could not edit post"); @@ -185,11 +187,11 @@ async fn update_post( } async fn delete_post( - Extension(ref conn): Extension, + state: State, Path(id): Path, mut cookies: Cookies, ) -> Result { - MutationCore::delete_post(conn, id) + MutationCore::delete_post(&state.conn, id) .await .expect("could not delete post");