AppState采用Arc包装

This commit is contained in:
胡天 2024-02-04 10:13:24 +08:00
parent b6a43e1d44
commit 0fcff12d4a
3 changed files with 12 additions and 12 deletions

View File

@ -1,3 +1,4 @@
use std::sync::Arc;
use askama::Template;
use axum::extract::{Path, Query, State};
use axum::Form;
@ -17,7 +18,7 @@ pub struct PageQuery {
pub async fn index(
State(state): State<AppState>,
State(state): State<Arc<AppState>>,
Query(q): Query<PageQuery>,
) -> Result<Html<String>> {
let tpl = MemberService::index(&state, q).await?;
@ -26,7 +27,7 @@ pub async fn index(
}
pub async fn detail(
State(state): State<AppState>,
State(state): State<Arc<AppState>>,
Path(id): Path<u32>,
) -> Result<Html<String>> {
let tpl = MemberService::detail(&state, id).await?;
@ -43,7 +44,7 @@ pub async fn add_ui() -> Result<Html<String>> {
pub async fn add(
State(state): State<AppState>,
State(state): State<Arc<AppState>>,
Form(frm): Form<form::AddAndEdit>,
) -> Result<(StatusCode, HeaderMap, ())> {
MemberService::add(&state, frm).await?;
@ -52,7 +53,7 @@ pub async fn add(
pub async fn edit_ui(
State(state): State<AppState>,
State(state): State<Arc<AppState>>,
Path(id): Path<u32>,
) -> Result<Html<String>> {
let tpl = MemberService::edit_ui(&state, id).await?;
@ -62,7 +63,7 @@ pub async fn edit_ui(
pub async fn edit(
State(state): State<AppState>,
State(state): State<Arc<AppState>>,
Path(id): Path<u32>,
Form(frm): Form<form::AddAndEdit>,
) -> Result<(StatusCode, HeaderMap, ())> {
@ -71,7 +72,7 @@ pub async fn edit(
}
pub async fn del(
State(state): State<AppState>,
State(state): State<Arc<AppState>>,
Path(id): Path<u32>,
) -> Result<(StatusCode, HeaderMap, ())> {
MemberService::del(&state, id).await?;
@ -79,7 +80,7 @@ pub async fn del(
}
pub async fn real_del(
State(state): State<AppState>,
State(state): State<Arc<AppState>>,
Path(id): Path<u32>,
) -> Result<(StatusCode, HeaderMap, ())> {
MemberService::real_del(&state, id).await?;
@ -95,7 +96,7 @@ pub async fn tran_ui() -> Result<Html<String>> {
pub async fn tran(
State(state): State<AppState>,
State(state): State<Arc<AppState>>,
Form(frm): Form<form::Tran>,
) -> Result<(StatusCode, HeaderMap, ())> {
let aff = MemberService::tran(&state, frm).await?;

View File

@ -62,7 +62,6 @@ async fn main() {
// 默认TraceLayer的打印级别为DEBUG设置response的打印级别为INFO这样可以避免过多的打印日志
// 利用make_span_with来显示span中的其他信息
let trace_layer = TraceLayer::new_for_http()
// TODO span的request写到日志会乱码 不知道怎么解决with_ansi为什么不起作用
.make_span_with(DefaultMakeSpan::new().level(Level::INFO))
.on_response(DefaultOnResponse::new().level(Level::INFO));
@ -75,9 +74,9 @@ async fn main() {
.route("/real_del/:id", get(handler::real_del))
.route("/tran", get(handler::tran_ui).post(handler::tran))
.layer(trace_layer)
.with_state(AppState {
.with_state(Arc::new(AppState {
pool: Arc::new(pool)
});
}));
let listener = tokio::net::TcpListener::bind(&cfg.web.addr).await.unwrap();

View File

@ -1,7 +1,7 @@
use std::sync::Arc;
// 最外层的state必须实现Clone
#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct AppState {
pub pool: Arc<sqlx::MySqlPool>,
}