### Database Setup and Spin Build Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/postgres-v3/README.md Commands to create a PostgreSQL database named 'spin_dev', load test data from 'db/testdata.sql', and build/run the Spin application. ```shell createdb -h localhost -U postgres spin_dev psql -h localhost -U postgres -d spin_dev -f db/testdata.sql spin build --up ``` -------------------------------- ### Setup PostgreSQL Database and Spin Application Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/postgres/README.md Creates a PostgreSQL database named 'spin_dev' and populates it with test data using SQL scripts. It then builds and runs the Spin application. ```shell createdb -h localhost -U postgres spin_dev psql -h localhost -U postgres -d spin_dev -f db/testdata.sql spin build --up ``` -------------------------------- ### Run PostgreSQL Docker Container Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/postgres/README.md Starts a PostgreSQL Docker container, mapping port 5432 and setting authentication to trust for easy local access. This is a prerequisite for the example. ```shell docker run --rm -h 127.0.0.1 -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres ``` -------------------------------- ### Run PostgreSQL Docker Container Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/postgres-v3/README.md Starts a PostgreSQL Docker container, mapping the default port 5432 to the host and configuring trust-based authentication for easier local development. ```shell docker run --rm -h 127.0.0.1 -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres ``` -------------------------------- ### Curl Write Route Example Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/postgres/README.md Interacts with the Spin application's write route using curl. This demonstrates sending data or triggering an action that results in a database write operation. ```shell $ curl -i localhost:3000/write HTTP/1.1 200 OK content-length: 9 date: Sun, 25 Sep 2022 15:46:22 GMT Count: 3 ``` -------------------------------- ### Curl Read Route Example Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/postgres/README.md Fetches articles from the Spin application's read route using curl. This demonstrates retrieving data from the PostgreSQL database via the Spin component and displays the returned articles. ```shell $ curl -i localhost:3000/read HTTP/1.1 200 OK content-length: 501 date: Sun, 25 Sep 2022 15:45:02 GMT Found 2 article(s) as follows: article: Article { id: 1, title: "My Life as a Goat", content: "I went to Nepal to live as a goat, and it was much better than being a butler.", authorname: "E. Blackadder", } article: Article { id: 2, title: "Magnificent Octopus", content: "Once upon a time there was a lovely little sausage.", authorname: "S. Baldrick", } (Column info: id:DbDataType::Int32, title:DbDataType::Str, content:DbDataType::Str, authorname:DbDataType::Str) ``` -------------------------------- ### Build and Run Spin Application with Bash Source: https://github.com/spinframework/spin-rust-sdk/blob/main/README.md This bash command demonstrates how to build and run a Spin application. The `spin build --up` command compiles all components defined in the manifest and starts the application, making it available for testing. ```bash $ spin build --up Building component hello-world with `cargo build --target wasm32-wasip1 --release` Finished release [optimized] target(s) in 0.12s Finished building all Spin components Logging component stdio to ".spin/logs/" Serving http://127.0.0.1:3000 Available Routes: hello-world: http://127.0.0.1:3000 (wildcard) ``` -------------------------------- ### Build and Run Spin Application Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/key-value/README.md Demonstrates how to build and run a Spin application using the `spin build --up` command. This command compiles the Rust project and starts the application, making it ready to receive requests. It requires the Spin CLI and a Rust development environment. ```shell $ RUST_LOG=spin=trace spin build --up ``` -------------------------------- ### Curl Read Route Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/postgres-v3/README.md Fetches articles from the Spin service via the '/read' endpoint. Demonstrates the HTTP response, including status, headers, and the JSON payload containing article data. ```http $ curl -i localhost:3000/read HTTP/1.1 200 OK transfer-encoding: chunked date: Wed, 06 Nov 2024 20:17:03 GMT Found 2 article(s) as follows: article: Article { id: 1, title: "My Life as a Goat", content: "I went to Nepal to live as a goat, and it was much better than being a butler.", authorname: "E. Blackadder", published: Date( 2024-11-05, ), coauthor: None, } article: Article { id: 2, title: "Magnificent Octopus", content: "Once upon a time there was a lovely little sausage.", authorname: "S. Baldrick", published: Date( 2024-11-06, ), coauthor: None, } (Column info: id:DbDataType::Int32, title:DbDataType::Str, content:DbDataType::Str, authorname:DbDataType::Str, published:DbDataType::Date, coauthor:DbDataType::Str) ``` -------------------------------- ### Attempt GET After Deletion Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/key-value/README.md Shows the result of attempting to retrieve data with a GET request after the resource has been deleted. The application correctly returns a 404 Not Found status, indicating the resource is no longer available. ```shell $ curl -i -X GET localhost:3000/test HTTP/1.1 404 Not Found content-length: 0 date: Tue, 25 Apr 2023 14:31:53 GMT ``` -------------------------------- ### Retrieve Data with GET Request Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/key-value/README.md Demonstrates retrieving data from the Spin application's `/test` endpoint using a GET request with `curl`. This shows the data previously sent via POST. A 200 OK status confirms successful retrieval. ```shell $ curl -i -X GET localhost:3000/test HTTP/1.1 200 OK content-length: 3 date: Tue, 25 Apr 2023 14:25:54 GMT ok! ``` -------------------------------- ### Curl Write Route Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/postgres-v3/README.md Executes a write operation on the Spin service via the '/write' endpoint. Shows the HTTP response indicating success and the number of rows affected. ```http $ curl -i localhost:3000/write HTTP/1.1 200 OK content-length: 9 date: Sun, 25 Sep 2022 15:46:22 GMT Count: 3 ``` -------------------------------- ### Curl Write Datetime Info Route Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/postgres-v3/README.md Tests writing datetime information to the Spin service via the '/write_datetime_info' endpoint. The response confirms the operation and indicates the number of records updated. ```http $ curl -i localhost:3000/write_datetime_info HTTP/1.1 200 OK content-length: 9 date: Sun, 25 Sep 2022 15:46:22 GMT Count: 4 ``` -------------------------------- ### Test Spin Application with Curl Source: https://github.com/spinframework/spin-rust-sdk/blob/main/README.md This bash command shows how to test a running Spin application using `curl`. It sends an HTTP GET request to the application's local address and displays the response, including headers and body. ```bash $ curl -i localhost:3000 HTTP/1.1 200 OK content-length: 77 content-type: application/json {"timestamp":1702599575198,"fact":"Sharks lay the biggest eggs in the world"} ``` -------------------------------- ### Make Outbound HTTP Requests in Rust Source: https://github.com/spinframework/spin-rust-sdk/blob/main/README.md This example shows how to make outbound HTTP requests from a Spin component using the Rust SDK. It covers creating a `Request`, sending it asynchronously with `spin_sdk::http::send`, and handling the `Response`. Requires `allowed_outbound_hosts` configuration in `spin.toml`. ```rust use spin_sdk::{ http::{IntoResponse, Request, Method, Response}, http_component, }; #[http_component] async fn handle_hello_world(_req: Request) -> Result { // Create the outbound request object let req = Request::builder() .method(Method::Get) .uri("https://random-data-api.fermyon.app/animals/json") .build(); // Send the request and await the response let res: Response = spin_sdk::http::send(req).await?; println!("{:?}", res); // log the response Ok(res) } ``` -------------------------------- ### Test JSON Deserialization with Curl Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/json-http/README.md Example command to test the automatic JSON deserialization functionality. It sends a POST request with a JSON payload to localhost:3000. ```shell curl -X POST -d '{"name": "Vyvyan"}' localhost:3000 ``` -------------------------------- ### Write Spin HTTP Components in Rust Source: https://github.com/spinframework/spin-rust-sdk/blob/main/README.md This snippet demonstrates how to create a basic Spin HTTP component in Rust. It uses the `spin_sdk::http_component` macro to mark the entry point and shows how to handle incoming requests and return responses. ```rust use spin_sdk::http::{IntoResponse, Request, Response}; use spin_sdk::http_component; /// A simple Spin HTTP component. #[http_component] fn handle_hello_world(req: Request) -> anyhow::Result { println!("Handling request to {:?}", req.header("spin-full-url")); Ok(Response::builder() .status(200) .header("content-type", "text/plain") .body("Hello, Fermyon") .build()) } ``` -------------------------------- ### Tagging and Pushing a New Release Source: https://github.com/spinframework/spin-rust-sdk/blob/main/release-process.md These Git commands are used to create a GPG-signed, annotated tag for the new release version and push it to the remote repository. Pushing the tag triggers the automated release process. ```git git tag -s -m "Spin Rust SDK v3.1.0" v3.1.0 ``` ```git git push origin v3.1.0 ``` -------------------------------- ### Spin Application Manifest Configuration Source: https://github.com/spinframework/spin-rust-sdk/blob/main/README.md This TOML snippet defines a Spin application's manifest (`spin.toml`). It includes application metadata, an HTTP trigger configuration, component source path, and specifies allowed outbound hosts for network requests. ```toml spin_manifest_version = 2 [application] name = "hello_world" version = "0.1.0" authors = ["Your Name "] description = "An example application" [[trigger.http]] route = "/..." component = "hello-world" [component.hello-world] source = "target/wasm32-wasip1/release/hello_world.wasm" allowed_outbound_hosts = ["https://random-data-api.fermyon.app"] [component.hello-world.build] command = "cargo build --target wasm32-wasip1 --release" watch = ["src/**/*.rs", "Cargo.toml"] ``` -------------------------------- ### Send POST Request to Spin App Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/key-value/README.md Shows how to send a POST request to the Spin application's `/test` endpoint using `curl`. This is used to send data to the application. The response indicates a successful operation with a 200 OK status. ```shell $ curl -i -X POST -d "ok!" localhost:3000/test HTTP/1.1 200 OK content-length: 0 date: Tue, 25 Apr 2023 14:25:43 GMT ``` -------------------------------- ### Delete Resource with DELETE Request Source: https://github.com/spinframework/spin-rust-sdk/blob/main/examples/key-value/README.md Illustrates how to delete a resource from the Spin application's `/test` endpoint using a DELETE request with `curl`. This operation typically removes the associated data. A 200 OK status indicates the deletion was successful. ```shell $ curl -i -X DELETE localhost:3000/test HTTP/1.1 200 OK content-length: 0 date: Tue, 25 Apr 2023 14:26:30 GMT ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.