Skip to content

Commit 6339dc3

Browse files
committed
fix http errors in middleware
1 parent fa235c4 commit 6339dc3

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

fplus-http-server/src/main.rs

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use env_logger;
22
use log::info;
3-
use fplus_database;
3+
use fplus_database::database;
44
pub(crate) mod router;
55
use reqwest::Client;
66
use serde::Deserialize;
@@ -63,6 +63,14 @@ where
6363
fn call(&self, req: ServiceRequest) -> Self::Future {
6464
let query_string = req.query_string();
6565
let query: Result<web::Query<RepoQuery>, _> = web::Query::from_query(query_string);
66+
let RepoQuery { owner, repo } = match query {
67+
Ok(q) => q.into_inner(),
68+
Err(_) => {
69+
return Box::pin(async {
70+
return Err(actix_web::error::ErrorBadRequest("Wrong query string format"));
71+
});
72+
}
73+
};
6674

6775
let auth_header_value = req.headers().get("Authorization")
6876
.and_then(|hv| hv.to_str().ok())
@@ -71,47 +79,45 @@ where
7179
let fut = self.service.call(req);
7280

7381
Box::pin(async move {
74-
// if let Some(token) = auth_header_value {
75-
// // Make the asynchronous HTTP request here
76-
// let client = Client::new();
77-
// let user_info_result = client.get("https://api.github.com/user")
78-
// .header("Authorization", format!("Bearer {}", token))
79-
// .header("User-Agent", "Actix-web")
80-
// .send()
81-
// .await;
82+
if let Some(token) = auth_header_value {
83+
// Make the asynchronous HTTP request here
84+
let client = Client::new();
85+
let user_info_result = client.get("https://api.github.com/user")
86+
.header("Authorization", format!("Bearer {}", token))
87+
.header("User-Agent", "Actix-web")
88+
.send()
89+
.await;
8290

83-
// match user_info_result {
84-
// Ok(response) => {
85-
// if response.status().is_success() {
86-
// let user_info = response
87-
// .json::<serde_json::Value>()
88-
// .await
89-
// .expect("Failed to parse JSON");
90-
91-
// if let Some(login) = user_info.get("login").and_then(|v| v.as_str()) {
92-
// println!("Login: {}", login);
93-
// } else {
94-
// println!("Login information not found.");
95-
// }
96-
// } else {
97-
// println!("Failed to get GitHub user info");
98-
// }
99-
// },
100-
// Err(e) => println!("Request error: {:?}", e),
101-
// }
102-
// }
103-
104-
// match database::get_allocator(&owner, &repo).await {
105-
// Ok(allocator) => {
106-
// match allocator {
107-
// Some(allocator) => HttpResponse::Ok().json(allocator),
108-
// None => HttpResponse::NotFound().finish(),
109-
// }
110-
// },
111-
// Err(e) => {
112-
// HttpResponse::InternalServerError().body(e.to_string())
113-
// }
114-
// }
91+
match user_info_result {
92+
Ok(response) => {
93+
//Raise an actix test error
94+
if response.status().is_success() {
95+
let user_info = response
96+
.json::<serde_json::Value>()
97+
.await
98+
.expect("Failed to parse JSON");
99+
100+
if let Some(login) = user_info.get("login").and_then(|v| v.as_str()) {
101+
println!("Login: {}", login);
102+
} else {
103+
println!("Login information not found.");
104+
}
105+
} else {
106+
println!("Failed to get GitHub user info");
107+
}
108+
},
109+
Err(e) => println!("Request error: {:?}", e),
110+
}
111+
}
112+
113+
match database::get_allocator(&owner, &repo).await {
114+
Ok(allocator) => {
115+
println!("Allocator: {:?}", allocator);
116+
},
117+
Err(e) => {
118+
println!("Failed to get allocator: {:?}", e);
119+
}
120+
}
115121

116122
let res = fut.await?;
117123
println!("Hi from response");

0 commit comments

Comments
 (0)