-
Notifications
You must be signed in to change notification settings - Fork 13
split example into smaller examples #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
karthik2804
merged 1 commit into
spinframework:main
from
karthik2804:examples/split_examples
Feb 27, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| __pycache__ | ||
| *.wasm | ||
| .spin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Spin python Key Value | ||
|
|
||
| A simple example showcasing the usage of Spin Key Value in the Spin Python SDK. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from spin_http import Response | ||
| from spin_key_value import kv_open_default | ||
|
|
||
|
|
||
| def handle_request(request): | ||
|
|
||
| store = kv_open_default() | ||
|
|
||
| match request.method: | ||
| case "GET": | ||
| value = store.get(request.uri) | ||
| return Response(200, [("content-type", "text/plain")], value) | ||
| case "POST": | ||
| store.set(request.uri, request.body) | ||
| return Response(200, [("content-type", "text/plain")]) | ||
| case "DELETE": | ||
| store.delete(request.uri) | ||
| return Response(200, [("content-type", "text/plain")]) | ||
| case "HEAD": | ||
| if store.exists(request.uri): | ||
| return Response(200, [("content-type", "text/plain")]) | ||
| return Response(404, [("content-type", "text/plain")]) | ||
| case default: | ||
| return Response(405, [("content-type", "text/plain")]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| spin_version = "1" | ||
| authors = ["Fermyon Engineering <engineering@fermyon.com>"] | ||
| description = "Spin Python SDK KV" | ||
| name = "spin-py-KV" | ||
| trigger = { type = "http", base = "/" } | ||
| version = "0.1.0" | ||
|
|
||
| [[component]] | ||
| id = "python-sdk-example" | ||
| source = "app.wasm" | ||
| key_value_stores = ["default"] | ||
| [component.trigger] | ||
| route = "/..." | ||
| [component.build] | ||
| command = "spin py2wasm app -o app.wasm" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| __pycache__ | ||
| *.wasm | ||
| .spin |
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # External Libraries With Spin Python | ||
|
|
||
| A simple example showcasing the usage of an external library with the Spin Python SDK. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from spin_http import Response | ||
| import toml | ||
|
|
||
| def handle_request(request): | ||
|
|
||
| some_toml = """ | ||
| title = "foo" | ||
| [bar] | ||
| baz = "wow" | ||
| such = "toml" | ||
| """ | ||
|
|
||
| return Response(200, | ||
| [("content-type", "text/plain")], | ||
| bytes(f"Toml content:{toml.loads(some_toml)}", "utf-8")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| spin_version = "1" | ||
| authors = ["Fermyon Engineering <engineering@fermyon.com>"] | ||
| description = "Spin Python SDK example using external library" | ||
| name = "spin-py-external-library" | ||
| trigger = { type = "http", base = "/" } | ||
| version = "0.1.0" | ||
|
|
||
| [[component]] | ||
| id = "python-sdk-example" | ||
| source = "app.wasm" | ||
| [component.trigger] | ||
| route = "/..." | ||
| [component.build] | ||
| command = "spin py2wasm app -o app.wasm" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| __pycache__ | ||
| *.wasm | ||
| .spin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Spin python Hello World | ||
|
|
||
| A simple example showcasing the return of a simple response to a request in the Spin Python SDK. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| from spin_http import Response | ||
|
|
||
|
|
||
| def handle_request(request): | ||
|
|
||
| return Response(200, | ||
| [("content-type", "text/plain")], | ||
| bytes(f"Hello from Python!", "utf-8")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| spin_version = "1" | ||
| authors = ["Fermyon Engineering <engineering@fermyon.com>"] | ||
| description = "Spin Python SDK hello world" | ||
| name = "spin-py-hello-world" | ||
| trigger = { type = "http", base = "/" } | ||
| version = "0.1.0" | ||
|
|
||
| [[component]] | ||
| id = "python-sdk-example" | ||
| source = "app.wasm" | ||
| [component.trigger] | ||
| route = "/..." | ||
| [component.build] | ||
| command = "spin py2wasm app -o app.wasm" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| __pycache__ | ||
| *.wasm | ||
| .spin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Spin python Outbound HTTP | ||
|
|
||
| A simple example showcasing an outbound HTTP request in Spin Python SDK. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from spin_http import Request, Response, http_send | ||
|
|
||
|
|
||
| def handle_request(request): | ||
|
|
||
| response = http_send( | ||
| Request("GET", "https://some-random-api.ml/facts/dog", [], None)) | ||
|
|
||
| return Response(200, | ||
| [("content-type", "text/plain")], | ||
| bytes(f"Here is a dog fact: {str(response.body, 'utf-8')}", "utf-8")) |
12 changes: 5 additions & 7 deletions
12
examples/hello/spin.toml → examples/outbound_http/spin.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,16 @@ | ||
| spin_version = "1" | ||
| authors = ["Fermyon Engineering <engineering@fermyon.com>"] | ||
| description = "Spin Python SDK example" | ||
| name = "python-sdk-example" | ||
| description = "Spin Python Outbound HTTP Example" | ||
| name = "Spin-python-outbound-http" | ||
| trigger = { type = "http", base = "/" } | ||
| version = "0.1.0" | ||
|
|
||
| [[component]] | ||
| id = "python-sdk-example" | ||
| environment = { hello = "teapot" } | ||
| config = { redis_address = "redis://127.0.0.1:6379" } | ||
| files = [{ source = "static-assets/", destination = "/" }] | ||
| source = "app.wasm" | ||
| allowed_http_hosts = ["insecure:allow-all"] | ||
| environment = { hello = "teapot" } | ||
| allowed_http_hosts = ["https://some-random-api.ml"] | ||
| [component.trigger] | ||
| route = "/..." | ||
| [component.build] | ||
| command = "../../target/release/spin-python app -o app.wasm" | ||
| command = "spin py2wasm app -o app.wasm" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| __pycache__ | ||
| *.wasm | ||
| .spin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Spin python Outbound Redis | ||
|
|
||
| A simple example showcasing outbound Redis command execution using the Spin Python SDK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from spin_http import Response | ||
| from spin_redis import redis_del, redis_get, redis_incr, redis_set, redis_sadd, redis_srem, redis_smembers, redis_execute | ||
| from spin_config import config_get | ||
|
|
||
|
|
||
| def handle_request(request): | ||
|
|
||
| redis_address = config_get("redis_address") | ||
| redis_set(redis_address, "foo", b"bar") | ||
| value = redis_get(redis_address, "foo") | ||
| redis_del(redis_address, ["testIncr"]) | ||
| redis_incr(redis_address, "testIncr") | ||
|
|
||
| redis_sadd(redis_address, "testSets", ["hello", "world"]) | ||
| content = redis_smembers(redis_address, "testSets") | ||
| redis_srem(redis_address, "testSets", ["hello"]) | ||
| redis_execute("redis_address", "set", [b"foo", b"hello"]) | ||
| redis_execute("redis_address", "append", [b"foo", b", world!"]) | ||
|
|
||
| assert value == b"bar", f"expected \"bar\", got \"{str(value, 'utf-8')}\"" | ||
|
|
||
| return Response(200, | ||
| [("content-type", "text/plain")], | ||
| bytes(f"Executed outbound Redis commands", "utf-8")) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| spin_version = "1" | ||
| authors = ["Fermyon Engineering <engineering@fermyon.com>"] | ||
| description = "Spin Python Outbound Redis example" | ||
| name = "spin-python-outbound-redis" | ||
| trigger = { type = "http", base = "/" } | ||
| version = "0.1.0" | ||
|
|
||
| [[component]] | ||
| id = "python-sdk-outbound-redis" | ||
| config = { redis_address = "redis://127.0.0.1:6379" } | ||
| source = "app.wasm" | ||
| [component.trigger] | ||
| route = "/..." | ||
| [component.build] | ||
| command = "spin py2wasm app -o app.wasm" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.