Skip to content

Commit f4993b5

Browse files
committed
Last example adjustments
Signed-off-by: Ryan Levick <[email protected]>
1 parent 8edf108 commit f4993b5

File tree

7 files changed

+19
-26
lines changed

7 files changed

+19
-26
lines changed

examples/config-rust/Cargo.lock

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

examples/config-rust/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ crate-type = ["cdylib"]
99
[dependencies]
1010
# Useful crate to handle errors.
1111
anyhow = "1"
12-
# General-purpose crate with common HTTP types.
13-
http = "0.2"
1412
# The Spin SDK.
1513
spin-sdk = { path = "../../sdk/rust" }
1614
[workspace]

examples/config-rust/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
use anyhow::Result;
2-
use spin_sdk::{config, http::IntoResponse, http_component};
1+
use spin_sdk::{
2+
config,
3+
http::{Request, Response},
4+
http_component,
5+
};
36

47
/// This endpoint returns the config value specified by key.
58
#[http_component]
6-
fn get(req: http::Request<()>) -> Result<impl IntoResponse> {
7-
let path = req.uri().path();
8-
9-
if path.contains("dotenv") {
9+
fn get(req: Request) -> anyhow::Result<Response> {
10+
if req.uri.contains("dotenv") {
1011
let val = config::get("dotenv").expect("Failed to acquire dotenv from spin.toml");
11-
return Ok(http::Response::builder().status(200).body(val)?);
12+
return Ok(Response::new(200, val));
1213
}
1314
let val = format!("message: {}", config::get("message")?);
14-
Ok(http::Response::builder().status(200).body(val)?)
15+
Ok(Response::new(200, val))
1516
}

examples/rust-outbound-redis/Cargo.lock

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

examples/rust-outbound-redis/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ crate-type = ["cdylib"]
99
[dependencies]
1010
# Useful crate to handle errors.
1111
anyhow = "1"
12-
# General-purpose crate with common HTTP types.
13-
http = "0.2"
1412
# The Spin SDK.
1513
spin-sdk = { path = "../../sdk/rust" }
1614

examples/rust-outbound-redis/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{anyhow, Context, Result};
22
use spin_sdk::{
33
http::responses::internal_server_error,
4-
http::{IntoResponse, Response},
4+
http::{IntoResponse, Request, Response},
55
http_component, redis,
66
};
77

@@ -19,7 +19,7 @@ const REDIS_CHANNEL_ENV: &str = "REDIS_CHANNEL";
1919
/// to a Redis channel. The component is triggered by an HTTP
2020
/// request served on the route configured in the `spin.toml`.
2121
#[http_component]
22-
fn publish(_req: http::Request<()>) -> Result<impl IntoResponse> {
22+
fn publish(_req: Request) -> Result<impl IntoResponse> {
2323
let address = std::env::var(REDIS_ADDRESS_ENV)?;
2424
let channel = std::env::var(REDIS_CHANNEL_ENV)?;
2525

sdk/rust/readme.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ use spin_sdk::{
1818
/// A simple Spin HTTP component.
1919
#[http_component]
2020
fn hello_world(req: Request) -> Result<Response> {
21-
println!("{:?}", req.headers());
22-
Ok(http::Response::builder()
23-
.status(200)
24-
.header("foo", "bar")
25-
.body(Some("Hello, Fermyon!".into()))?)
21+
println!("{:?}", req.headers);
22+
Ok(Response::new_with_headers(200, &[] "Hello, Fermyon!"))
2623
}
2724
```
2825

@@ -31,9 +28,10 @@ The important things to note in the function above:
3128
- the `spin_sdk::http_component` macro — this marks the function as the
3229
entrypoint for the Spin component
3330
- the function signature — `fn hello_world(req: Request) -> Result<Response>`
34-
the Spin HTTP component uses the HTTP objects from the popular Rust crate
35-
[`http`](https://crates.io/crates/http), and the request and response bodies
36-
are optionally using [`bytes::Bytes`](https://crates.io/crates/bytes)
31+
`req` can be any number of types including the built in `Request` type or
32+
the `http::Request` from the popular `http` crate. Likewise, the response type
33+
can be many things including the built in `Response` type or the `http::Response` type
34+
from the `http` crate.
3735

3836
### Making outbound HTTP requests
3937

@@ -43,11 +41,11 @@ server, modifies the result, then returns it:
4341
```rust
4442
#[http_component]
4543
fn hello_world(_req: Request) -> Result<Response> {
46-
let mut res = spin_sdk::http::send(
44+
let mut res: http::Response<()> = spin_sdk::http::send(
4745
http::Request::builder()
4846
.method("GET")
4947
.uri("https://fermyon.com")
50-
.body(None)?,
48+
.body(())?,
5149
)?;
5250

5351
res.headers_mut()

0 commit comments

Comments
 (0)