1
- use std:: env;
2
-
3
- use actix_web:: middleware:: Logger ;
4
- use actix_web:: { App , HttpServer } ;
5
1
use env_logger;
6
2
use log:: info;
7
3
use fplus_database;
8
4
pub ( crate ) mod router;
5
+ use reqwest:: Client ;
6
+ use serde:: Deserialize ;
7
+
8
+ use std:: { env, future:: { ready, Ready } } ;
9
+
10
+ use actix_web:: {
11
+ App ,
12
+ HttpServer ,
13
+ web,
14
+ middleware:: Logger ,
15
+ dev:: { forward_ready, Service , ServiceRequest , ServiceResponse , Transform } ,
16
+ Error ,
17
+ HttpResponse ,
18
+ http:: StatusCode ,
19
+ } ;
20
+ use futures_util:: future:: LocalBoxFuture ;
21
+
22
+ #[ derive( Deserialize , Debug ) ]
23
+ struct RepoQuery {
24
+ owner : String ,
25
+ repo : String ,
26
+ }
27
+
28
+ pub struct GHAuth ;
29
+
30
+ impl < S , B > Transform < S , ServiceRequest > for GHAuth
31
+ where
32
+ S : Service < ServiceRequest , Response = ServiceResponse < B > , Error = Error > ,
33
+ S :: Future : ' static ,
34
+ B : ' static ,
35
+ {
36
+ type Response = ServiceResponse < B > ;
37
+ type Error = Error ;
38
+ type InitError = ( ) ;
39
+ type Transform = GHAuthMiddleware < S > ;
40
+ type Future = Ready < Result < Self :: Transform , Self :: InitError > > ;
41
+
42
+ fn new_transform ( & self , service : S ) -> Self :: Future {
43
+ ready ( Ok ( GHAuthMiddleware { service } ) )
44
+ }
45
+ }
46
+
47
+ pub struct GHAuthMiddleware < S > {
48
+ service : S ,
49
+ }
50
+
51
+ impl < S , B > Service < ServiceRequest > for GHAuthMiddleware < S >
52
+ where
53
+ S : Service < ServiceRequest , Response = ServiceResponse < B > , Error = Error > ,
54
+ S :: Future : ' static ,
55
+ B : ' static ,
56
+ {
57
+ type Response = ServiceResponse < B > ;
58
+ type Error = Error ;
59
+ type Future = LocalBoxFuture < ' static , Result < Self :: Response , Self :: Error > > ;
60
+
61
+ forward_ready ! ( service) ;
62
+
63
+ fn call ( & self , req : ServiceRequest ) -> Self :: Future {
64
+ let query_string = req. query_string ( ) ;
65
+ let query: Result < web:: Query < RepoQuery > , _ > = web:: Query :: from_query ( query_string) ;
66
+
67
+ let auth_header_value = req. headers ( ) . get ( "Authorization" )
68
+ . and_then ( |hv| hv. to_str ( ) . ok ( ) )
69
+ . filter ( |hv| hv. starts_with ( "Bearer " ) )
70
+ . map ( |hv| hv[ "Bearer " . len ( ) ..] . to_string ( ) ) ;
71
+ let fut = self . service . call ( req) ;
72
+
73
+ 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
+
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
+ // }
115
+
116
+ let res = fut. await ?;
117
+ println ! ( "Hi from response" ) ;
118
+ Ok ( res)
119
+ } )
120
+ }
121
+ }
122
+
9
123
10
124
#[ tokio:: main]
11
125
async fn main ( ) -> std:: io:: Result < ( ) > {
@@ -27,9 +141,14 @@ async fn main() -> std::io::Result<()> {
27
141
. wrap ( cors)
28
142
. service ( router:: health)
29
143
. service ( router:: application:: create)
30
- . service ( router:: application:: trigger)
31
- . service ( router:: application:: propose)
32
- . service ( router:: application:: approve)
144
+ . service (
145
+ web:: scope ( "/api" )
146
+ . wrap ( GHAuth ) // Apply GitHubAuth to all routes under "/api"
147
+ . service ( router:: application:: testz)
148
+ . service ( router:: application:: trigger)
149
+ . service ( router:: application:: propose)
150
+ . service ( router:: application:: approve)
151
+ )
33
152
. service ( router:: application:: merged)
34
153
. service ( router:: application:: active)
35
154
. service ( router:: application:: all_applications)
0 commit comments