-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathlib.rs
30 lines (25 loc) · 870 Bytes
/
lib.rs
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
use anyhow::Result;
use spin_sdk::{
http::{Request, Response},
http_component,
};
// The environment variable set in `spin.toml` that points to the
// URL that the component will send a request to.
//
// Note that the domain of the URL must also be present in the `allowed_http_hosts`
// list for the component to be allowed to connect to the host.
const SERVICE_URL_ENV: &str = "SERVICE_URL";
/// Send an HTTP request and return the response.
#[http_component]
fn send_outbound(_req: Request) -> Result<Response> {
let service_url = std::env::var(SERVICE_URL_ENV)?;
let mut res = spin_sdk::outbound_http::send_request(
http::Request::builder()
.method("GET")
.uri(service_url)
.body(None)?,
)?;
res.headers_mut()
.insert("spin-component", "rust-outbound-http".try_into()?);
Ok(res)
}