From a9e5aec6b4c29b2073898a1527b0d8ceed582d68 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Tue, 29 Nov 2022 11:44:09 +0600 Subject: [PATCH 1/3] chore: Drop unused wit files Signed-off-by: Konstantin Shabanov --- wit/ephemeral/wasi-cache.wit | 15 --------------- wit/ephemeral/wasi-log.wit | 20 -------------------- 2 files changed, 35 deletions(-) delete mode 100644 wit/ephemeral/wasi-cache.wit delete mode 100644 wit/ephemeral/wasi-log.wit diff --git a/wit/ephemeral/wasi-cache.wit b/wit/ephemeral/wasi-cache.wit deleted file mode 100644 index 2358a5394..000000000 --- a/wit/ephemeral/wasi-cache.wit +++ /dev/null @@ -1,15 +0,0 @@ -// A simple cache interface. - -use * from types - -// Set the payload for the given key. -// Implementations may choose to ignore the time-to-live (in seconds) argument. -// TODO (@radu-matei): perhaps return the number of bytes written? -set: func(key: string, value: payload, ttl: option) -> expected - -// Get the payload stored in the cache for the given key. -// If not found, return a success result with an empty payload. -get: func(key: string) -> expected - -// Delete the cache entry for the given key. -delete: func(key: string) -> expected diff --git a/wit/ephemeral/wasi-log.wit b/wit/ephemeral/wasi-log.wit deleted file mode 100644 index 954491f4c..000000000 --- a/wit/ephemeral/wasi-log.wit +++ /dev/null @@ -1,20 +0,0 @@ -// Log levels. -enum level { - trace, - debug, - info, - warn, - error, - fatal -} - -// General log function. -log: func(msg: string, lvl: level) - -// Specialized log functions. -trace: func(msg: string) -debug: func(msg: string) -info: func(msg: string) -warn: func(msg: string) -error: func(msg: string) -fatal: func(msg: string) From f4a64fd19685a31fa9c0e14a4eff7119ea769898 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Tue, 29 Nov 2022 13:15:53 +0600 Subject: [PATCH 2/3] chore: Unify TinyGo examples Signed-off-by: Konstantin Shabanov --- examples/config-tinygo/Makefile | 7 ------- examples/config-tinygo/README.md | 16 ++++++++++++++++ examples/config-tinygo/spin.toml | 4 +++- .../{readme.md => README.md} | 16 +++++++++------- examples/http-tinygo-outbound-http/spin.toml | 2 +- examples/http-tinygo/{readme.md => README.md} | 12 +++++++----- examples/tinygo-outbound-redis/README.md | 18 ++++++++++++++++++ examples/tinygo-redis/README.md | 11 +++++++++++ 8 files changed, 65 insertions(+), 21 deletions(-) delete mode 100644 examples/config-tinygo/Makefile create mode 100644 examples/config-tinygo/README.md rename examples/http-tinygo-outbound-http/{readme.md => README.md} (75%) rename examples/http-tinygo/{readme.md => README.md} (78%) create mode 100644 examples/tinygo-outbound-redis/README.md create mode 100644 examples/tinygo-redis/README.md diff --git a/examples/config-tinygo/Makefile b/examples/config-tinygo/Makefile deleted file mode 100644 index 1d94d6881..000000000 --- a/examples/config-tinygo/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -.PHONY: build -build: - tinygo build -wasm-abi=generic -target=wasi -gc=leaking -o main.wasm main.go - -.PHONY: serve -serve: - RUST_LOG=spin=trace spin up --file spin.toml diff --git a/examples/config-tinygo/README.md b/examples/config-tinygo/README.md new file mode 100644 index 000000000..a82cca0bf --- /dev/null +++ b/examples/config-tinygo/README.md @@ -0,0 +1,16 @@ +# Spin component in TinyGo + +```shell +$ RUST_LOG=spin=trace spin build --up +``` + +The application can now receive requests on `http://localhost:3000`: + +```shell +$ curl -i localhost:3000 +HTTP/1.1 200 OK +content-length: 23 +date: Tue, 29 Nov 2022 06:59:24 GMT + +message: I'm a teapot +``` diff --git a/examples/config-tinygo/spin.toml b/examples/config-tinygo/spin.toml index dc75b3468..8cba7f703 100644 --- a/examples/config-tinygo/spin.toml +++ b/examples/config-tinygo/spin.toml @@ -14,4 +14,6 @@ source = "main.wasm" [component.trigger] route = "/..." [component.config] -message = "I'm a {{object}}" \ No newline at end of file +message = "I'm a {{object}}" +[component.build] +command = "tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go" diff --git a/examples/http-tinygo-outbound-http/readme.md b/examples/http-tinygo-outbound-http/README.md similarity index 75% rename from examples/http-tinygo-outbound-http/readme.md rename to examples/http-tinygo-outbound-http/README.md index 6d9d5a406..ba77bc66f 100644 --- a/examples/http-tinygo-outbound-http/readme.md +++ b/examples/http-tinygo-outbound-http/README.md @@ -16,16 +16,17 @@ Creating and sending HTTP requests from Spin components closely follows the Go r1, err := spin_http.Get("https://some-random-api.ml/facts/dog") r2, err := spin_http.Post("https://postman-echo.com/post", "text/plain", bytes.NewBufferString("Hello there!")) - req, err := http.NewRequest("PUT", "https://postman-echo.com/put", bytes NewBufferString("General Kenobi!")) - req.Header.Add("foo", "bar") - r3, err := spin_http.Send(req) +req, err := http.NewRequest("PUT", "https://postman-echo.com/put", bytes NewBufferString("General Kenobi!")) +req.Header.Add("foo", "bar") +r3, err := spin_http.Send(req) ``` Building this as a WebAssembly module can be done using the `tinygo` compiler: ```shell -$ make build -tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go +$ spin build +Executing the build command for component tinygo-hello: tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go +Successfully ran the build command for the Spin components. ``` The component configuration must contain a list of all hosts allowed to send @@ -42,13 +43,14 @@ source = "main.wasm" allowed_http_hosts = [ "https://some-random-api.ml", "https://postman-echo.com" ] [component.trigger] route = "/hello" +[component.build] +command = "tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go" ``` At this point, we can execute the application with the `spin` CLI: ```shell -$ make serve -RUST_LOG=spin=trace,wasi_outbound_http=trace spin up --file spin.toml +$ RUST_LOG=spin=trace,wasi_outbound_http=trace spin up ``` The application can now receive requests on `http://localhost:3000/hello`: diff --git a/examples/http-tinygo-outbound-http/spin.toml b/examples/http-tinygo-outbound-http/spin.toml index 3723e5c25..297cacf12 100644 --- a/examples/http-tinygo-outbound-http/spin.toml +++ b/examples/http-tinygo-outbound-http/spin.toml @@ -1,7 +1,7 @@ spin_version = "1" authors = ["Fermyon Engineering "] description = "A simple Spin application written in (Tiny)Go that performs outbound HTTP requests." -name = "spin-hello-tinygo" +name = "spin-tinygo-outbound-http" trigger = { type = "http", base = "/" } version = "1.0.0" diff --git a/examples/http-tinygo/readme.md b/examples/http-tinygo/README.md similarity index 78% rename from examples/http-tinygo/readme.md rename to examples/http-tinygo/README.md index 77429191f..5535bc3bc 100644 --- a/examples/http-tinygo/readme.md +++ b/examples/http-tinygo/README.md @@ -1,4 +1,4 @@ -# Spin HTTP components in (Tiny)Go +# Spin HTTP components in TinyGo This example showcases how to build Spin HTTP components using TinyGo. @@ -30,8 +30,9 @@ func main() {} Building this as a WebAssembly module can be done using the `tinygo` compiler: ```shell -$ make build -tinygo build -wasm-abi=generic -target=wasi -no-debug -o main.wasm main.go +$ spin build +Executing the build command for component tinygo-hello: tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go +Successfully ran the build command for the Spin components. ``` Finally, we can create a Spin application configuration to execute this @@ -50,13 +51,14 @@ id = "hello" source = "main.wasm" [component.trigger] route = "/hello" +[component.build] +command = "tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go" ``` At this point, we can execute the application with the `spin` CLI: ```shell -$ make serve -RUST_LOG=spin=trace spin up --file spin.toml +$ RUST_LOG=spin=trace spin up ``` The application can now receive requests on `http://localhost:3000/hello`: diff --git a/examples/tinygo-outbound-redis/README.md b/examples/tinygo-outbound-redis/README.md new file mode 100644 index 000000000..ecaf8f1f9 --- /dev/null +++ b/examples/tinygo-outbound-redis/README.md @@ -0,0 +1,18 @@ +# Spin component in TinyGo + +```shell +$ RUST_LOG=spin=trace spin build --up +``` + +The application can now receive requests on `http://localhost:3000/publish`: + +```shell +$ curl -i localhost:3000/publish +HTTP/1.1 200 OK +content-length: 67 +date: Tue, 29 Nov 2022 07:03:52 GMT + +mykey value was: myvalue +spin-go-incr value: 1 +deleted keys num: 2 +``` diff --git a/examples/tinygo-redis/README.md b/examples/tinygo-redis/README.md new file mode 100644 index 000000000..8d0ab2b53 --- /dev/null +++ b/examples/tinygo-redis/README.md @@ -0,0 +1,11 @@ +# Spin component in TinyGo + +```shell +$ RUST_LOG=spin=trace spin build --up +``` + +```shell +$ redis-cli +127.0.0.1:6379> PUBLISH messages test-message +(integer) 1 +``` From 0f8c025969d04e40cdddd5d0efede7f5452c2dcd Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Tue, 29 Nov 2022 13:25:32 +0600 Subject: [PATCH 3/3] chore: Update wagi examples - Update spin-wagi-http Cargo.lock - Add build command into wagi-http-rust Signed-off-by: Konstantin Shabanov --- examples/spin-wagi-http/http-rust/Cargo.lock | 118 +++++-------------- examples/wagi-http-rust/spin.toml | 4 +- 2 files changed, 34 insertions(+), 88 deletions(-) diff --git a/examples/spin-wagi-http/http-rust/Cargo.lock b/examples/spin-wagi-http/http-rust/Cargo.lock index e37a66473..dc73cb9dd 100644 --- a/examples/spin-wagi-http/http-rust/Cargo.lock +++ b/examples/spin-wagi-http/http-rust/Cargo.lock @@ -31,18 +31,21 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "form_urlencoded" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" +dependencies = [ + "percent-encoding", +] + [[package]] name = "goodbyerust" version = "0.1.0" @@ -86,21 +89,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] -name = "log" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" -dependencies = [ - "cfg-if", -] - [[package]] name = "memchr" version = "2.4.1" @@ -108,10 +96,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] -name = "pin-project-lite" -version = "0.2.8" +name = "percent-encoding" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c" +checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" [[package]] name = "proc-macro2" @@ -159,13 +147,14 @@ dependencies = [ [[package]] name = "spin-sdk" -version = "0.2.0" +version = "0.6.0" dependencies = [ "anyhow", "bytes", + "form_urlencoded", "http", "spin-macro", - "wasi-experimental-http", + "thiserror", "wit-bindgen-rust", ] @@ -182,18 +171,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.30" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" +checksum = "10deb33631e3c9018b9baf9dcbbc4f737320d2b576bac10f6aefa048fa407e3e" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.30" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" +checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" dependencies = [ "proc-macro2", "quote", @@ -215,39 +204,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" -[[package]] -name = "tracing" -version = "0.1.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6c650a8ef0cd2dd93736f033d21cbd1224c5a967aa0c258d00fcf7dafef9b9f" -dependencies = [ - "cfg-if", - "log", - "pin-project-lite", - "tracing-attributes", - "tracing-core", -] - -[[package]] -name = "tracing-attributes" -version = "0.1.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8276d9a4a3a558d7b7ad5303ad50b53d58264641b82914b7ada36bd762e7a716" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "tracing-core" -version = "0.1.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03cfcb51380632a72d3111cb8d3447a8d908e577d31beeac006f836383d29a23" -dependencies = [ - "lazy_static", -] - [[package]] name = "unicase" version = "2.6.0" @@ -284,22 +240,10 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "wasi-experimental-http" -version = "0.9.0" -source = "git+https://github.com/deislabs/wasi-experimental-http?rev=a82ae3d6c85c4251b3663035febeb8100068f51d#a82ae3d6c85c4251b3663035febeb8100068f51d" -dependencies = [ - "anyhow", - "bytes", - "http", - "thiserror", - "tracing", -] - [[package]] name = "wit-bindgen-gen-core" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=2f46ce4cc072107153da0cefe15bdc69aa5b84d0#2f46ce4cc072107153da0cefe15bdc69aa5b84d0" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "anyhow", "wit-parser", @@ -307,8 +251,8 @@ dependencies = [ [[package]] name = "wit-bindgen-gen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=2f46ce4cc072107153da0cefe15bdc69aa5b84d0#2f46ce4cc072107153da0cefe15bdc69aa5b84d0" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "heck", "wit-bindgen-gen-core", @@ -316,8 +260,8 @@ dependencies = [ [[package]] name = "wit-bindgen-gen-rust-wasm" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=2f46ce4cc072107153da0cefe15bdc69aa5b84d0#2f46ce4cc072107153da0cefe15bdc69aa5b84d0" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "heck", "wit-bindgen-gen-core", @@ -326,8 +270,8 @@ dependencies = [ [[package]] name = "wit-bindgen-rust" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=2f46ce4cc072107153da0cefe15bdc69aa5b84d0#2f46ce4cc072107153da0cefe15bdc69aa5b84d0" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "async-trait", "bitflags", @@ -336,8 +280,8 @@ dependencies = [ [[package]] name = "wit-bindgen-rust-impl" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=2f46ce4cc072107153da0cefe15bdc69aa5b84d0#2f46ce4cc072107153da0cefe15bdc69aa5b84d0" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "proc-macro2", "syn", @@ -347,8 +291,8 @@ dependencies = [ [[package]] name = "wit-parser" -version = "0.1.0" -source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=2f46ce4cc072107153da0cefe15bdc69aa5b84d0#2f46ce4cc072107153da0cefe15bdc69aa5b84d0" +version = "0.2.0" +source = "git+https://github.com/bytecodealliance/wit-bindgen?rev=cb871cfa1ee460b51eb1d144b175b9aab9c50aba#cb871cfa1ee460b51eb1d144b175b9aab9c50aba" dependencies = [ "anyhow", "id-arena", diff --git a/examples/wagi-http-rust/spin.toml b/examples/wagi-http-rust/spin.toml index f4b8919a8..3063ba65b 100644 --- a/examples/wagi-http-rust/spin.toml +++ b/examples/wagi-http-rust/spin.toml @@ -10,4 +10,6 @@ id = "env" source = "target/wasm32-wasi/release/wagihelloworld.wasm" [component.trigger] route = "/env" -executor = { type = "wagi" } # _start (the default entrypoint) is automatically mapped to main() \ No newline at end of file +executor = { type = "wagi" } # _start (the default entrypoint) is automatically mapped to main() +[component.build] +command = "cargo build --target wasm32-wasi --release"