Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade to axum 0.6.1 version #1285

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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