-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_test.rs
More file actions
43 lines (36 loc) · 905 Bytes
/
Copy pathlambda_test.rs
File metadata and controls
43 lines (36 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use lambda_runtime::{service_fn, Error, LambdaEvent};
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
struct Event {
sourceIp: Option<String>,
xForwardedFor: Option<String>,
}
#[derive(Serialize)]
struct Response {
body: String,
}
#[tokio::main]
async fn main() -> Result<(), Error> {
let func = service_fn(handler);
lambda_runtime::run(func).await?;
Ok(())
}
async fn handler(event: LambdaEvent<Event>) -> Result<Response, Error> {
let (event, _context) = event.into_parts();
let body = if let Some(xff) = event.xForwardedFor {
xff.split(',').next().unwrap_or_default().to_string()
} else if let Some(sip) = event.sourceIp {
sip
} else {
"IP Address Not Found".to_string()
};
let html_response = format!(r#"
<html>
<body>
<h1>Your IP Address</h1>
<p>{}</p>
</body>
</html>
"#, body);
Ok(Response { body: html_response })
}