From 7f52237ceaa70af43b7d0f415a08ae05915bdeb5 Mon Sep 17 00:00:00 2001 From: Vaughn Dice Date: Mon, 27 Apr 2026 16:14:02 -0600 Subject: [PATCH] feat(templates): add http-py-p2 Signed-off-by: Vaughn Dice --- templates/http-py-p2/content/.gitignore | 3 +++ templates/http-py-p2/content/README.md | 19 +++++++++++++++++++ templates/http-py-p2/content/app.py | 9 +++++++++ templates/http-py-p2/content/requirements.txt | 2 ++ templates/http-py-p2/content/spin.toml | 17 +++++++++++++++++ .../metadata/snippets/component.txt | 10 ++++++++++ .../http-py-p2/metadata/spin-template.toml | 13 +++++++++++++ 7 files changed, 73 insertions(+) create mode 100644 templates/http-py-p2/content/.gitignore create mode 100644 templates/http-py-p2/content/README.md create mode 100644 templates/http-py-p2/content/app.py create mode 100644 templates/http-py-p2/content/requirements.txt create mode 100644 templates/http-py-p2/content/spin.toml create mode 100644 templates/http-py-p2/metadata/snippets/component.txt create mode 100644 templates/http-py-p2/metadata/spin-template.toml diff --git a/templates/http-py-p2/content/.gitignore b/templates/http-py-p2/content/.gitignore new file mode 100644 index 0000000..dc5c849 --- /dev/null +++ b/templates/http-py-p2/content/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +*.wasm +.spin \ No newline at end of file diff --git a/templates/http-py-p2/content/README.md b/templates/http-py-p2/content/README.md new file mode 100644 index 0000000..11682ed --- /dev/null +++ b/templates/http-py-p2/content/README.md @@ -0,0 +1,19 @@ +# A HTTP python component using componentize-py + +This template uses the latest v3 version of the Spin Python SDK ([3.4.1](https://pypi.org/project/spin-sdk/3.4.1/)) +for compatibility with Spin 3.x (and hosts that use this major version). The 'p2' corresponds to the wasip2 or +WASI Preview 2 set of WASI APIs, as used by Spin 3.x. + +## Installing the requirements + +To build the component, [`componentize-py`](https://pypi.org/project/componentize-py/) and [`spin-sdk`](https://pypi.org/project/spin-sdk/) are required. To install them, run: + +```bash +pip3 install -r requirements.txt +``` + +## Building and Running + +``` +spin up --build +``` diff --git a/templates/http-py-p2/content/app.py b/templates/http-py-p2/content/app.py new file mode 100644 index 0000000..08bd721 --- /dev/null +++ b/templates/http-py-p2/content/app.py @@ -0,0 +1,9 @@ +from spin_sdk.http import IncomingHandler, Request, Response + +class IncomingHandler(IncomingHandler): + def handle_request(self, request: Request) -> Response: + return Response( + 200, + {"content-type": "text/plain"}, + bytes("Hello from Python!", "utf-8") + ) diff --git a/templates/http-py-p2/content/requirements.txt b/templates/http-py-p2/content/requirements.txt new file mode 100644 index 0000000..ab7cdf0 --- /dev/null +++ b/templates/http-py-p2/content/requirements.txt @@ -0,0 +1,2 @@ +spin-sdk == 3.4.1 +componentize-py == 0.17.2 diff --git a/templates/http-py-p2/content/spin.toml b/templates/http-py-p2/content/spin.toml new file mode 100644 index 0000000..f887f78 --- /dev/null +++ b/templates/http-py-p2/content/spin.toml @@ -0,0 +1,17 @@ +spin_manifest_version = 2 + +[application] +authors = ["{{authors}}"] +description = "{{project-description}}" +name = "{{project-name}}" +version = "0.1.0" + +[[trigger.http]] +route = "{{http-path}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "app.wasm" +[component.{{project-name | kebab_case}}.build] +command = "componentize-py -w spin-http componentize app -o app.wasm" +watch = ["*.py", "requirements.txt"] diff --git a/templates/http-py-p2/metadata/snippets/component.txt b/templates/http-py-p2/metadata/snippets/component.txt new file mode 100644 index 0000000..f52f35b --- /dev/null +++ b/templates/http-py-p2/metadata/snippets/component.txt @@ -0,0 +1,10 @@ +[[trigger.http]] +route = "{{http-path}}" +component = "{{project-name | kebab_case}}" + +[component.{{project-name | kebab_case}}] +source = "{{ output-path }}/app.wasm" +[component.{{project-name | kebab_case}}.build] +command = "componentize-py -w spin-http componentize app -o app.wasm" +workdir = "{{ output-path }}" +watch = ["*.py", "requirements.txt"] diff --git a/templates/http-py-p2/metadata/spin-template.toml b/templates/http-py-p2/metadata/spin-template.toml new file mode 100644 index 0000000..83cf86a --- /dev/null +++ b/templates/http-py-p2/metadata/spin-template.toml @@ -0,0 +1,13 @@ +manifest_version = "1" +id = "http-py-p2" +description = "HTTP request handler using Python compatible with Spin 3.x hosts" +tags = ["http", "python", "py"] + +[add_component] +skip_files = ["spin.toml"] +[add_component.snippets] +component = "component.txt" + +[parameters] +project-description = { type = "string", prompt = "Description", default = "" } +http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }