Skip to content

Commit f1c5934

Browse files
committed
P3 command example
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
1 parent c89b225 commit f1c5934

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

examples/async/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ name = "job"
33
version = "0.1.0"
44
edition = "2021"
55

6+
[lib]
7+
crate-type = ["cdylib"]
8+
69
[dependencies]
710
anyhow = "1.0.102"
811
spin-sdk = { git = "https://github.com/spinframework/spin-rust-sdk" }

examples/async/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use spin_sdk::{wasip3, http::{EmptyBody, body::IncomingBodyExt, Request, send}};
2+
3+
wasip3::cli::command::export!(Main);
4+
5+
struct Main;
6+
7+
impl wasip3::exports::cli::run::Guest for Main {
8+
async fn run() -> Result<(), ()> {
9+
match main().await {
10+
Ok(()) => Ok(()),
11+
Err(e) => {
12+
eprintln!("{e}");
13+
Err(())
14+
}
15+
}
16+
}
17+
}
18+
19+
async fn main() -> anyhow::Result<()> {
20+
let request = Request::get("https://myip.fermyon.app").body(EmptyBody::new())?;
21+
let response = send(request).await?;
22+
let response_bytes = response.into_body().bytes().await?;
23+
let response_text = String::from_utf8_lossy(&response_bytes.as_ref());
24+
println!("Your IP is: {}", response_text);
25+
Ok(())
26+
}

examples/async/src/main.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)