Skip to content

Commit

Permalink
升级 axum 及相关依赖到最新版本
Browse files Browse the repository at this point in the history
  • Loading branch information
veoco committed Jun 11, 2024
1 parent f43276c commit 7e3d821
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
http = "0.2"
axum = { version = "0.6", features = ["headers", "multipart"] }
hyper = { version = "0.14", features = ["full"] }
http = "1.1"
http-body-util = "0.1"
axum = { version = "0.7", features = ["multipart"] }
hyper = { version = "1.3", features = ["full"] }
tokio = { version = "1.22", features = ["full"] }

axum-client-ip = "0.3"
axum-client-ip = "0.6"
axum-macros = "0.4"
axum-extra = { version = "0.9", features = ["typed-header"] }
futures = "0.3"
tokio-util = { version = "0.7", features = ["io"] }
validator = { version = "0.18", features = ["derive"] }

tower = "0.4"
tower-http = { version = "0.3", features = ["full"] }
tower-http = { version = "0.5", features = ["full"] }

tracing = "0.1"
tracing-subscriber = "0.3"
Expand Down
10 changes: 5 additions & 5 deletions src/comments/views.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use axum::extract::{Path, State, TypedHeader};
use axum::headers::UserAgent;
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::Json;
use axum_client_ip::ClientIp;
use axum_client_ip::InsecureClientIp;
use axum_extra::{headers::UserAgent, TypedHeader};
use md5::{Digest, Md5};
use serde_json::{json, Value};
use sqlx::any::AnyKind;
Expand All @@ -19,7 +19,7 @@ pub async fn create_page_comment(
State(state): State<Arc<AppState>>,
PMVisitor(user): PMVisitor,
TypedHeader(user_agent): TypedHeader<UserAgent>,
ClientIp(ip): ClientIp,
InsecureClientIp(ip): InsecureClientIp,
Path(slug): Path<String>,
ValidatedJson(comment_create): ValidatedJson<CommentCreate>,
) -> Result<(StatusCode, Json<Value>), FieldError> {
Expand Down Expand Up @@ -94,7 +94,7 @@ pub async fn create_post_comment(
State(state): State<Arc<AppState>>,
PMVisitor(user): PMVisitor,
TypedHeader(user_agent): TypedHeader<UserAgent>,
ClientIp(ip): ClientIp,
InsecureClientIp(ip): InsecureClientIp,
Path(slug): Path<String>,
ValidatedJson(comment_create): ValidatedJson<CommentCreate>,
) -> Result<(StatusCode, Json<Value>), FieldError> {
Expand Down
8 changes: 4 additions & 4 deletions src/common/extractors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use axum::{
async_trait,
body::Body,
extract::{rejection::JsonRejection, FromRef, FromRequest, FromRequestParts, Query},
http::{request::Parts, Request},
Json,
Expand All @@ -17,16 +18,15 @@ use crate::AppState;
pub struct ValidatedJson<T>(pub T);

#[async_trait]
impl<T, S, B> FromRequest<S, B> for ValidatedJson<T>
impl<S, T> FromRequest<S> for ValidatedJson<T>
where
T: DeserializeOwned + Validate,
S: Send + Sync,
Json<T>: FromRequest<S, B, Rejection = JsonRejection>,
B: Send + 'static,
Json<T>: FromRequest<S, Rejection = JsonRejection>,
{
type Rejection = ValidateRequestError;

async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
let Json(value) = Json::<T>::from_request(req, state).await?;
value.validate()?;
Ok(ValidatedJson(value))
Expand Down
15 changes: 11 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use getopts::Options;
use std::env;

use getopts::Options;
use tracing::{info, Level};
use tracing_subscriber::FmtSubscriber;
use std::net::SocketAddr;
use tokio::signal;
use tokio::net::TcpListener;

use rumo::{app, init};

Expand Down Expand Up @@ -72,10 +73,16 @@ async fn main() {
tracing_subscriber::fmt::init();
let addr = env::var("LISTEN_ADDRESS").unwrap_or(String::from("127.0.0.1:3000"));
info!("Listening on http://{}", addr);
let listener = match TcpListener::bind(addr).await{
Ok(l) => l,
Err(e) => {
info!("Failed to bind address: {}", e);
return;
}
};

let app = app(None).await;
let _ = axum::Server::bind(&addr.parse().unwrap())
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
let _ = axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
.await;
}
Expand Down
7 changes: 3 additions & 4 deletions src/users/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use axum::{
extract::TypedHeader,
use axum::{http::request::Parts, RequestPartsExt};
use axum_extra::{
headers::{authorization::Bearer, Authorization},
http::request::Parts,
RequestPartsExt,
TypedHeader,
};
use hmac::{Hmac, Mac};
use jwt::VerifyWithKey;
Expand Down
30 changes: 15 additions & 15 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use axum::{
body::Body,
http::{self, Request, StatusCode},
};
use hyper::body::to_bytes;
use http_body_util::BodyExt;
use minijinja::Environment;
use serde_json::{json, Value};
use sqlx::AnyPool;
Expand Down Expand Up @@ -70,7 +70,7 @@ pub async fn get(url: &str) -> (StatusCode, Option<Value>) {
.unwrap();
let response = app.oneshot(request).await.unwrap();
let status_code = response.status();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = serde_json::from_slice(&body).unwrap_or(None);
(status_code, body)
}
Expand All @@ -90,7 +90,7 @@ pub async fn post(url: &str, data: String) -> (StatusCode, Option<Value>) {
.unwrap();
let response = app.oneshot(request).await.unwrap();
let status_code = response.status();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = serde_json::from_slice(&body).unwrap_or(None);
(status_code, body)
}
Expand All @@ -107,7 +107,7 @@ pub async fn admin_get(url: &str) -> (StatusCode, Option<Value>) {
.body(Body::from(login_data))
.unwrap();
let response = app.oneshot(request).await.unwrap();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
let token = body.get("access_token").unwrap().as_str().unwrap();

Expand All @@ -122,7 +122,7 @@ pub async fn admin_get(url: &str) -> (StatusCode, Option<Value>) {
.unwrap();
let response = app.oneshot(request).await.unwrap();
let status_code = response.status();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = serde_json::from_slice(&body).unwrap_or(None);
(status_code, body)
}
Expand All @@ -139,7 +139,7 @@ pub async fn admin_delete(url: &str) -> (StatusCode, Option<Value>) {
.body(Body::from(login_data))
.unwrap();
let response = app.oneshot(request).await.unwrap();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
let token = body.get("access_token").unwrap().as_str().unwrap();

Expand All @@ -154,7 +154,7 @@ pub async fn admin_delete(url: &str) -> (StatusCode, Option<Value>) {
.unwrap();
let response = app.oneshot(request).await.unwrap();
let status_code = response.status();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = serde_json::from_slice(&body).unwrap_or(None);
(status_code, body)
}
Expand All @@ -171,7 +171,7 @@ pub async fn admin_post(url: &str, data: String) -> (StatusCode, Option<Value>)
.body(Body::from(login_data))
.unwrap();
let response = app.oneshot(request).await.unwrap();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
let token = body.get("access_token").unwrap().as_str().unwrap();

Expand All @@ -186,7 +186,7 @@ pub async fn admin_post(url: &str, data: String) -> (StatusCode, Option<Value>)
.unwrap();
let response = app.oneshot(request).await.unwrap();
let status_code = response.status();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = serde_json::from_slice(&body).unwrap_or(None);
(status_code, body)
}
Expand All @@ -203,7 +203,7 @@ pub async fn admin_patch(url: &str, data: String) -> (StatusCode, Option<Value>)
.body(Body::from(login_data))
.unwrap();
let response = app.oneshot(request).await.unwrap();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
let token = body.get("access_token").unwrap().as_str().unwrap();

Expand All @@ -218,7 +218,7 @@ pub async fn admin_patch(url: &str, data: String) -> (StatusCode, Option<Value>)
.unwrap();
let response = app.oneshot(request).await.unwrap();
let status_code = response.status();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = serde_json::from_slice(&body).unwrap_or(None);
(status_code, body)
}
Expand All @@ -235,7 +235,7 @@ pub async fn admin_post_file(url: &str, data: Vec<u8>) -> (StatusCode, Option<Va
.body(Body::from(login_data))
.unwrap();
let response = app.oneshot(request).await.unwrap();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
let token = body.get("access_token").unwrap().as_str().unwrap();

Expand All @@ -253,7 +253,7 @@ pub async fn admin_post_file(url: &str, data: Vec<u8>) -> (StatusCode, Option<Va
.unwrap();
let response = app.oneshot(request).await.unwrap();
let status_code = response.status();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = serde_json::from_slice(&body).unwrap_or(None);
(status_code, body)
}
Expand All @@ -270,7 +270,7 @@ pub async fn admin_patch_file(url: &str, data: Vec<u8>) -> (StatusCode, Option<V
.body(Body::from(login_data))
.unwrap();
let response = app.oneshot(request).await.unwrap();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body: Value = serde_json::from_slice(&body).unwrap();
let token = body.get("access_token").unwrap().as_str().unwrap();

Expand All @@ -288,7 +288,7 @@ pub async fn admin_patch_file(url: &str, data: Vec<u8>) -> (StatusCode, Option<V
.unwrap();
let response = app.oneshot(request).await.unwrap();
let status_code = response.status();
let body = to_bytes(response.into_body()).await.unwrap();
let body = response.into_body().collect().await.unwrap().to_bytes();
let body = serde_json::from_slice(&body).unwrap_or(None);
(status_code, body)
}
Expand Down

0 comments on commit 7e3d821

Please sign in to comment.