Skip to content

Commit ba65a0f

Browse files
committed
Fix python example
Signed-off-by: Ryan Levick <[email protected]>
1 parent 53e4769 commit ba65a0f

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

examples/python/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__pycache__
2+
http.wasm
3+
composed.wasm

examples/python/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@ component for serving static files.
1111
- [Spin v2.0+](https://developer.fermyon.com/spin/install)
1212
- [Rust](https://rustup.rs/)
1313
- [cargo-component](https://github.com/bytecodealliance/cargo-component)
14-
- [wasm-tools](https://github.com/bytecodealliance/wasm-tools/)
15-
- Note that you'll need [this fork](https://github.com/dicej/wasm-tools/tree/wasm-compose-resource-imports) until [this PR](https://github.com/bytecodealliance/wasm-tools/pull/1261) has been merged and released.
14+
- [wac](https://github.com/bytecodealliance/wac)
1615
- [Python](https://www.python.org/downloads/) 3.11 or later
1716
- [pip](https://pip.pypa.io/en/stable/installation/)
18-
- [componentize-py](https://pypi.org/project/componentize-py/) 0.6.0
17+
- [componentize-py](https://pypi.org/project/componentize-py/)
1918
- [curl](https://curl.se/download.html) or a web browser for testing
2019

2120
Once you have Rust, Python, and pip installed, the following should give you everything else:
2221

2322
```shell
2423
rustup target add wasm32-wasi
2524
cargo install cargo-component
26-
cargo install --locked --git https://github.com/dicej/wasm-tools \
27-
--branch wasm-compose-resource-imports wasm-tools
28-
pip install componentize-py==0.6.0
25+
cargo install wac-cli
26+
pip install componentize-py
2927
```
3028

3129
## Building and Running

examples/python/app.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
from proxy import exports
55
from proxy.types import Ok
66
from proxy.imports import types, incoming_handler
7-
from proxy.imports.types import MethodGet, IncomingRequest, ResponseOutparam, OutgoingResponse, Fields, OutgoingBody
7+
from proxy.imports.types import Method_Get, IncomingRequest, ResponseOutparam, OutgoingResponse, Fields, OutgoingBody
88
from poll_loop import Stream, Sink, PollLoop
99

10+
1011
class IncomingHandler(exports.IncomingHandler):
1112
def handle(self, request: IncomingRequest, response_out: ResponseOutparam):
1213
# Dispatch the request using `asyncio`, backed by a custom event loop
@@ -15,26 +16,27 @@ def handle(self, request: IncomingRequest, response_out: ResponseOutparam):
1516
asyncio.set_event_loop(loop)
1617
loop.run_until_complete(handle_async(request, response_out))
1718

19+
1820
async def handle_async(request: IncomingRequest, response_out: ResponseOutparam):
1921
method = request.method()
2022
path = request.path_with_query()
2123

22-
if isinstance(method, MethodGet) and path == "/hello":
23-
response = OutgoingResponse(200, Fields([("content-type", b"text/plain")]))
24+
if isinstance(method, Method_Get) and path == "/hello":
25+
response = OutgoingResponse(200, Fields(
26+
[("content-type", b"text/plain")]))
2427
response_body = response.write()
25-
28+
2629
ResponseOutparam.set(response_out, Ok(response))
2730

2831
sink = Sink(response_body)
2932
await sink.send(b"Hello, world!")
3033
sink.close()
31-
32-
elif isinstance(method, MethodGet):
34+
35+
elif isinstance(method, Method_Get):
3336
# Delegate to spin-fileserver component.
3437
incoming_handler.handle(request, response_out)
3538

3639
else:
3740
response = OutgoingResponse(400, Fields([]))
3841
ResponseOutparam.set(response_out, Ok(response))
3942
OutgoingBody.finish(response.write(), None)
40-

examples/python/compose.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/python/spin.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ component = "spin-fileserver-example"
1414
source = "composed.wasm"
1515
files = [{ source = "../static", destination = "/" }]
1616
[component.spin-fileserver-example.build]
17-
command = "componentize-py -d ../wit -w proxy componentize app -o http.wasm && (cd .. && cargo component build --release) && RUST_LOG=error wasm-tools compose http.wasm -c compose.yml -o composed.wasm"
17+
command = "componentize-py -d ../wit -w proxy componentize app -o http.wasm && (cd .. && cargo component build --release) && RUST_LOG=error wac plug --plug ../../target/wasm32-wasi/release/spin_static_fs.wasm http.wasm -o composed.wasm"
1818
watch = ["*.py"]

0 commit comments

Comments
 (0)