chore: upgrade to axum 0.6.1 version (#1285)
This commit is contained in:
parent
23b6e66c2e
commit
0c40345e1b
@ -7,16 +7,16 @@ publish = false
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
axum-example-core = { path = "../core" }
|
axum-example-core = { path = "../core" }
|
||||||
tokio = { version = "1.18.1", features = ["full"] }
|
tokio = { version = "1.23.0", features = ["full"] }
|
||||||
axum = "0.5.4"
|
axum = "0.6.1"
|
||||||
tower = "0.4.12"
|
tower = "0.4.13"
|
||||||
tower-http = { version = "0.3.3", features = ["fs"] }
|
tower-http = { version = "0.3.5", features = ["fs"] }
|
||||||
tower-cookies = "0.6.0"
|
tower-cookies = "0.8.0"
|
||||||
anyhow = "1.0.57"
|
anyhow = "1.0.66"
|
||||||
dotenvy = "0.15.0"
|
dotenvy = "0.15.6"
|
||||||
serde = "1.0.137"
|
serde = "1.0.149"
|
||||||
serde_json = "1.0.81"
|
serde_json = "1.0.89"
|
||||||
tera = "1.15.0"
|
tera = "1.17.1"
|
||||||
tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
|
||||||
entity = { path = "../entity" }
|
entity = { path = "../entity" }
|
||||||
migration = { path = "../migration" }
|
migration = { path = "../migration" }
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
mod flash;
|
mod flash;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Extension, Form, Path, Query},
|
extract::{Form, Path, Query, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::Html,
|
response::Html,
|
||||||
routing::{get, get_service, post},
|
routing::{get, get_service, post},
|
||||||
@ -18,7 +18,6 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::{env, net::SocketAddr};
|
use std::{env, net::SocketAddr};
|
||||||
use tera::Tera;
|
use tera::Tera;
|
||||||
use tower::ServiceBuilder;
|
|
||||||
use tower_cookies::{CookieManagerLayer, Cookies};
|
use tower_cookies::{CookieManagerLayer, Cookies};
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
|
|
||||||
@ -37,16 +36,18 @@ async fn start() -> anyhow::Result<()> {
|
|||||||
.await
|
.await
|
||||||
.expect("Database connection failed");
|
.expect("Database connection failed");
|
||||||
Migrator::up(&conn, None).await.unwrap();
|
Migrator::up(&conn, None).await.unwrap();
|
||||||
|
|
||||||
let templates = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"))
|
let templates = Tera::new(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"))
|
||||||
.expect("Tera initialization failed");
|
.expect("Tera initialization failed");
|
||||||
// let state = AppState { templates, conn };
|
|
||||||
|
let state = AppState { templates, conn };
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(list_posts).post(create_post))
|
.route("/", get(list_posts).post(create_post))
|
||||||
.route("/:id", get(edit_post).post(update_post))
|
.route("/:id", get(edit_post).post(update_post))
|
||||||
.route("/new", get(new_post))
|
.route("/new", get(new_post))
|
||||||
.route("/delete/:id", post(delete_post))
|
.route("/delete/:id", post(delete_post))
|
||||||
.nest(
|
.nest_service(
|
||||||
"/static",
|
"/static",
|
||||||
get_service(ServeDir::new(concat!(
|
get_service(ServeDir::new(concat!(
|
||||||
env!("CARGO_MANIFEST_DIR"),
|
env!("CARGO_MANIFEST_DIR"),
|
||||||
@ -59,12 +60,8 @@ async fn start() -> anyhow::Result<()> {
|
|||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.layer(
|
|
||||||
ServiceBuilder::new()
|
|
||||||
.layer(CookieManagerLayer::new())
|
.layer(CookieManagerLayer::new())
|
||||||
.layer(Extension(conn))
|
.with_state(state);
|
||||||
.layer(Extension(templates)),
|
|
||||||
);
|
|
||||||
|
|
||||||
let addr = SocketAddr::from_str(&server_url).unwrap();
|
let addr = SocketAddr::from_str(&server_url).unwrap();
|
||||||
Server::bind(&addr).serve(app.into_make_service()).await?;
|
Server::bind(&addr).serve(app.into_make_service()).await?;
|
||||||
@ -72,6 +69,12 @@ async fn start() -> anyhow::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct AppState {
|
||||||
|
templates: Tera,
|
||||||
|
conn: DatabaseConnection,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Params {
|
struct Params {
|
||||||
page: Option<u64>,
|
page: Option<u64>,
|
||||||
@ -85,15 +88,14 @@ struct FlashData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn list_posts(
|
async fn list_posts(
|
||||||
Extension(ref templates): Extension<Tera>,
|
state: State<AppState>,
|
||||||
Extension(ref conn): Extension<DatabaseConnection>,
|
|
||||||
Query(params): Query<Params>,
|
Query(params): Query<Params>,
|
||||||
cookies: Cookies,
|
cookies: Cookies,
|
||||||
) -> Result<Html<String>, (StatusCode, &'static str)> {
|
) -> Result<Html<String>, (StatusCode, &'static str)> {
|
||||||
let page = params.page.unwrap_or(1);
|
let page = params.page.unwrap_or(1);
|
||||||
let posts_per_page = params.posts_per_page.unwrap_or(5);
|
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
|
.await
|
||||||
.expect("Cannot find posts in page");
|
.expect("Cannot find posts in page");
|
||||||
|
|
||||||
@ -107,18 +109,18 @@ async fn list_posts(
|
|||||||
ctx.insert("flash", &value);
|
ctx.insert("flash", &value);
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = templates
|
let body = state
|
||||||
|
.templates
|
||||||
.render("index.html.tera", &ctx)
|
.render("index.html.tera", &ctx)
|
||||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?;
|
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?;
|
||||||
|
|
||||||
Ok(Html(body))
|
Ok(Html(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn new_post(
|
async fn new_post(state: State<AppState>) -> Result<Html<String>, (StatusCode, &'static str)> {
|
||||||
Extension(ref templates): Extension<Tera>,
|
|
||||||
) -> Result<Html<String>, (StatusCode, &'static str)> {
|
|
||||||
let ctx = tera::Context::new();
|
let ctx = tera::Context::new();
|
||||||
let body = templates
|
let body = state
|
||||||
|
.templates
|
||||||
.render("new.html.tera", &ctx)
|
.render("new.html.tera", &ctx)
|
||||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?;
|
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?;
|
||||||
|
|
||||||
@ -126,13 +128,13 @@ async fn new_post(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn create_post(
|
async fn create_post(
|
||||||
Extension(ref conn): Extension<DatabaseConnection>,
|
state: State<AppState>,
|
||||||
form: Form<post::Model>,
|
|
||||||
mut cookies: Cookies,
|
mut cookies: Cookies,
|
||||||
|
form: Form<post::Model>,
|
||||||
) -> Result<PostResponse, (StatusCode, &'static str)> {
|
) -> Result<PostResponse, (StatusCode, &'static str)> {
|
||||||
let form = form.0;
|
let form = form.0;
|
||||||
|
|
||||||
MutationCore::create_post(conn, form)
|
MutationCore::create_post(&state.conn, form)
|
||||||
.await
|
.await
|
||||||
.expect("could not insert post");
|
.expect("could not insert post");
|
||||||
|
|
||||||
@ -145,11 +147,10 @@ async fn create_post(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn edit_post(
|
async fn edit_post(
|
||||||
Extension(ref templates): Extension<Tera>,
|
state: State<AppState>,
|
||||||
Extension(ref conn): Extension<DatabaseConnection>,
|
|
||||||
Path(id): Path<i32>,
|
Path(id): Path<i32>,
|
||||||
) -> Result<Html<String>, (StatusCode, &'static str)> {
|
) -> 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
|
.await
|
||||||
.expect("could not find post")
|
.expect("could not find post")
|
||||||
.unwrap_or_else(|| panic!("could not find post with id {}", id));
|
.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();
|
let mut ctx = tera::Context::new();
|
||||||
ctx.insert("post", &post);
|
ctx.insert("post", &post);
|
||||||
|
|
||||||
let body = templates
|
let body = state
|
||||||
|
.templates
|
||||||
.render("edit.html.tera", &ctx)
|
.render("edit.html.tera", &ctx)
|
||||||
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?;
|
.map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?;
|
||||||
|
|
||||||
@ -165,14 +167,14 @@ async fn edit_post(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn update_post(
|
async fn update_post(
|
||||||
Extension(ref conn): Extension<DatabaseConnection>,
|
state: State<AppState>,
|
||||||
Path(id): Path<i32>,
|
Path(id): Path<i32>,
|
||||||
form: Form<post::Model>,
|
|
||||||
mut cookies: Cookies,
|
mut cookies: Cookies,
|
||||||
|
form: Form<post::Model>,
|
||||||
) -> Result<PostResponse, (StatusCode, String)> {
|
) -> Result<PostResponse, (StatusCode, String)> {
|
||||||
let form = form.0;
|
let form = form.0;
|
||||||
|
|
||||||
MutationCore::update_post_by_id(conn, id, form)
|
MutationCore::update_post_by_id(&state.conn, id, form)
|
||||||
.await
|
.await
|
||||||
.expect("could not edit post");
|
.expect("could not edit post");
|
||||||
|
|
||||||
@ -185,11 +187,11 @@ async fn update_post(
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn delete_post(
|
async fn delete_post(
|
||||||
Extension(ref conn): Extension<DatabaseConnection>,
|
state: State<AppState>,
|
||||||
Path(id): Path<i32>,
|
Path(id): Path<i32>,
|
||||||
mut cookies: Cookies,
|
mut cookies: Cookies,
|
||||||
) -> Result<PostResponse, (StatusCode, &'static str)> {
|
) -> Result<PostResponse, (StatusCode, &'static str)> {
|
||||||
MutationCore::delete_post(conn, id)
|
MutationCore::delete_post(&state.conn, id)
|
||||||
.await
|
.await
|
||||||
.expect("could not delete post");
|
.expect("could not delete post");
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user