@@ -11,10 +11,28 @@ use tokio_retry::{strategy::FixedInterval, Retry};
11
11
12
12
type Error = Box < dyn std:: error:: Error + Send + Sync + ' static > ;
13
13
14
+ struct AdapterOptions {
15
+ host : String ,
16
+ port : String ,
17
+ readiness_check_port : String ,
18
+ readiness_check_path : String ,
19
+ base_path : Option < String > ,
20
+ }
21
+
14
22
#[ tokio:: main]
15
23
async fn main ( ) -> Result < ( ) , Error > {
16
24
env_logger:: init ( ) ;
17
25
26
+ // setup config options from environment variables
27
+ let options = & AdapterOptions {
28
+ host : env:: var ( "HOST" ) . unwrap_or_else ( |_| "127.0.0.1" . to_string ( ) ) ,
29
+ port : env:: var ( "PORT" ) . unwrap_or_else ( |_| "8080" . to_string ( ) ) ,
30
+ readiness_check_port : env:: var ( "READINESS_CHECK_PORT" )
31
+ . unwrap_or_else ( |_| env:: var ( "PORT" ) . unwrap_or_else ( |_| "8080" . to_string ( ) ) ) ,
32
+ readiness_check_path : env:: var ( "READINESS_CHECK_PATH" ) . unwrap_or_else ( |_| "/" . to_string ( ) ) ,
33
+ base_path : env:: var ( "REMOVE_BASE_PATH" ) . ok ( ) ,
34
+ } ;
35
+
18
36
// register as an external extension
19
37
let handle = Handle :: current ( ) ;
20
38
tokio:: task:: spawn_blocking ( move || {
@@ -31,48 +49,41 @@ async fn main() -> Result<(), Error> {
31
49
// check if the application is ready every 10 milliseconds
32
50
Retry :: spawn ( FixedInterval :: from_millis ( 10 ) , || {
33
51
let readiness_check_url = format ! (
34
- "http://127.0.0.1:{}{}" ,
35
- env:: var( "READINESS_CHECK_PORT" ) . unwrap_or_else( |_| "8080" . to_string( ) ) ,
36
- env:: var( "READINESS_CHECK_PATH" ) . unwrap_or_else( |_| "/" . to_string( ) )
52
+ "http://{}:{}{}" ,
53
+ options. host, options. readiness_check_port, options. readiness_check_path
37
54
) ;
38
55
reqwest:: get ( readiness_check_url)
39
56
} )
40
57
. await ?;
41
58
42
59
// start lambda runtime
43
- let options = & HandlerOptions {
44
- http_client : Client :: builder ( ) . redirect ( redirect:: Policy :: none ( ) ) . build ( ) . unwrap ( ) ,
45
- host : env:: var ( "HOST" ) . unwrap_or_else ( |_| "127.0.0.1" . to_string ( ) ) ,
46
- port : env:: var ( "PORT" ) . unwrap_or_else ( |_| "8080" . to_string ( ) ) ,
47
- base_path : env:: var ( "REMOVE_BASE_PATH" ) . unwrap_or_default ( ) ,
48
- } ;
49
- lambda_http:: run ( http_handler ( move |event : Request | async move { http_proxy_handler ( event, options) . await } ) ) . await ?;
60
+ let http_client = & Client :: builder ( ) . redirect ( redirect:: Policy :: none ( ) ) . build ( ) . unwrap ( ) ;
61
+ lambda_http:: run ( http_handler ( |event : Request | async move {
62
+ http_proxy_handler ( event, http_client, options) . await
63
+ } ) )
64
+ . await ?;
50
65
Ok ( ( ) )
51
66
}
52
67
53
- struct HandlerOptions {
54
- http_client : Client ,
55
- host : String ,
56
- port : String ,
57
- base_path : String ,
58
- }
59
-
60
- async fn http_proxy_handler ( event : Request , options : & HandlerOptions ) -> Result < Response < Body > , Error > {
68
+ async fn http_proxy_handler (
69
+ event : Request ,
70
+ http_client : & Client ,
71
+ options : & AdapterOptions ,
72
+ ) -> Result < Response < Body > , Error > {
61
73
let host = options. host . as_str ( ) ;
62
74
let port = options. port . as_str ( ) ;
63
75
let ( parts, body) = event. into_parts ( ) ;
64
76
let mut path_and_query = parts. uri . path_and_query ( ) . unwrap ( ) . as_str ( ) ;
65
77
// strip away Base Path if environment variable REMOVE_BASE_PATH is set.
66
- if options. base_path != String :: default ( ) {
67
- if let Some ( value) = path_and_query. strip_prefix ( options. base_path . as_str ( ) ) {
78
+ if options. base_path . is_some ( ) {
79
+ if let Some ( value) = path_and_query. strip_prefix ( options. base_path . as_ref ( ) . unwrap ( ) ) {
68
80
path_and_query = value;
69
81
} ;
70
82
}
71
83
let app_url = format ! ( "http://{}:{}{}" , host, port, path_and_query) ;
72
84
debug ! ( "app_url is {:#?}" , app_url) ;
73
85
74
- let app_response = options
75
- . http_client
86
+ let app_response = http_client
76
87
. request ( parts. method , app_url)
77
88
. headers ( parts. headers )
78
89
. body ( body. to_vec ( ) )
0 commit comments