Skip to content

Commit ed283d6

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

File tree

5 files changed

+47
-36
lines changed

5 files changed

+47
-36
lines changed

examples/async/Cargo.lock

Lines changed: 0 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/async/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ 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" }
9-
tokio = { version = "1.51.1", features = ["macros", "rt"] }
1012

1113
[workspace]

examples/async/README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,25 @@ The sample app wraps async code (an outbound HTTP request) as shown below:
66

77

88
```rust
9-
spin_executor::run(async move {
9+
wasip3::cli::command::export!(Main);
10+
11+
struct Main;
12+
13+
impl wasip3::exports::cli::run::Guest for Main {
14+
async fn run() -> Result<(), ()> {
15+
match main().await {
16+
Ok(()) => Ok(()),
17+
Err(e) => {
18+
eprintln!("{e}");
19+
Err(())
20+
}
21+
}
22+
}
23+
}
24+
25+
async fn main() -> anyhow::Result<()> {
1026
// async code goes here
11-
})
27+
}
1228
```
1329

1430
Upon running the Spin App (using `spin up`) it will send an HTTP request to `https://myip.fermyon.app` and print your public IP address to `stdout`.

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)