Skip to content

Commit

Permalink
chore: upgrade to axum 0.6.1 version
Browse files Browse the repository at this point in the history
  • Loading branch information
ttys3 committed Dec 6, 2022
1 parent 23b6e66 commit 0aaeea3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
22 changes: 11 additions & 11 deletions examples/axum_example/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
62 changes: 32 additions & 30 deletions examples/axum_example/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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;

Expand All @@ -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"),
Expand All @@ -59,19 +60,21 @@ 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?;

Ok(())
}

#[derive(Clone)]
struct AppState {
templates: Tera,
conn: DatabaseConnection,
}

#[derive(Deserialize)]
struct Params {
page: Option<u64>,
Expand All @@ -85,15 +88,14 @@ struct FlashData {
}

async fn list_posts(
Extension(ref templates): Extension<Tera>,
Extension(ref conn): Extension<DatabaseConnection>,
state: State<AppState>,
Query(params): Query<Params>,
cookies: Cookies,
) -> Result<Html<String>, (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");

Expand All @@ -107,32 +109,32 @@ 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<Tera>,
) -> Result<Html<String>, (StatusCode, &'static str)> {
async fn new_post(state: State<AppState>) -> Result<Html<String>, (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"))?;

Ok(Html(body))
}

async fn create_post(
Extension(ref conn): Extension<DatabaseConnection>,
form: Form<post::Model>,
state: State<AppState>,
mut cookies: Cookies,
form: Form<post::Model>,
) -> Result<PostResponse, (StatusCode, &'static str)> {
let form = form.0;

MutationCore::create_post(conn, form)
MutationCore::create_post(&state.conn, form)
.await
.expect("could not insert post");

Expand All @@ -145,34 +147,34 @@ async fn create_post(
}

async fn edit_post(
Extension(ref templates): Extension<Tera>,
Extension(ref conn): Extension<DatabaseConnection>,
state: State<AppState>,
Path(id): Path<i32>,
) -> Result<Html<String>, (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));

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"))?;

Ok(Html(body))
}

async fn update_post(
Extension(ref conn): Extension<DatabaseConnection>,
state: State<AppState>,
Path(id): Path<i32>,
form: Form<post::Model>,
mut cookies: Cookies,
form: Form<post::Model>,
) -> Result<PostResponse, (StatusCode, String)> {
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");

Expand All @@ -185,11 +187,11 @@ async fn update_post(
}

async fn delete_post(
Extension(ref conn): Extension<DatabaseConnection>,
state: State<AppState>,
Path(id): Path<i32>,
mut cookies: Cookies,
) -> Result<PostResponse, (StatusCode, &'static str)> {
MutationCore::delete_post(conn, id)
MutationCore::delete_post(&state.conn, id)
.await
.expect("could not delete post");

Expand Down

0 comments on commit 0aaeea3

Please sign in to comment.