Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions templates/http-middleware/content/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.spin/
16 changes: 16 additions & 0 deletions templates/http-middleware/content/Cargo.toml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "{{project-name | kebab_case}}"
authors = ["{{authors}}"]
description = "{{project-description}}"
version = "0.1.0"
rust-version = "1.93"
edition = "2024"

[lib]
crate-type = ["cdylib"]

[dependencies]
anyhow = "1"
spin-sdk = "6.0.0"

[workspace]
14 changes: 14 additions & 0 deletions templates/http-middleware/content/spin.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spin_manifest_version = 2

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not really a clear way to test middleware as a standalone spin app unless we revive (I think)spin test

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I briefly looked at a middleware template, I assumed it would have to be add-only (not available in spin new). Even with spin test it's not clear how a standalone piece of middleware could work.


[application]
name = "{{project-name | kebab_case}}"
version = "0.1.0"
authors = ["{{authors}}"]
description = "{{project-description}}"

[component.{{project-name | kebab_case}}]
source = "target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm"
allowed_outbound_hosts = []
[component.{{project-name | kebab_case}}.build]
command = "cargo build --target wasm32-wasip2 --release"
watch = ["src/**/*.rs", "Cargo.toml"]
17 changes: 17 additions & 0 deletions templates/http-middleware/content/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use spin_sdk::http::{self, HeaderName, HeaderValue, Request, Response};
use spin_sdk::http_service;

#[http_service]
async fn handle(mut req: Request) -> http::Result<Response> {
// Request runs on the way in, before the next handler.
eprintln!("[middleware] --> {} {}", req.method(), req.uri().path());

// Forward the (modified) request to the next handler in the chain and wait
// for its response.
let mut resp = http::next(req).await?;

// Response runs on the way out, after the next handler
eprintln!("[middleware] <-- {}", resp.status());

Ok(resp)
}
10 changes: 10 additions & 0 deletions templates/http-middleware/metadata/snippets/component.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[[trigger.http]]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current templating system requires the add snippet to contain a trigger section which doesn't make sense for a middleware component (or any library component).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, weird. I think we used to check the trigger to make sure it was the same as existing triggers. I don't think that would be needed any more - let's see how the trigger info is being depended on and see if we can bin it off.

component = "{{project-name | kebab_case}}"

[component.{{project-name | kebab_case}}]
source = "{{ output-path }}/target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm"
allowed_outbound_hosts = []
[component.{{project-name | kebab_case}}.build]
command = "cargo build --target wasm32-wasip2 --release"
workdir = "{{ output-path }}"
watch = ["src/**/*.rs", "Cargo.toml"]
14 changes: 14 additions & 0 deletions templates/http-middleware/metadata/spin-template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
manifest_version = "1"
id = "http-middleware"
description = "http middleware rust template"
tags = []

[add_component]
skip_files = ["spin.toml"]
[add_component.snippets]
component = "component.txt"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding middleware to an existing app would require post add instructions including adding the middleware to dependencies under the correct trigger section.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be unavoidable - presumably the reason you're building your thing as middleware rather than including it in component source code is that you want to apply it to multiple triggers. (Although I guess another case is "my app is an off-the-shelf component like the fileserver but I want to wrap it." So maybe sometimes we do want to be able to auto-add it to triggers. But yeah, we don't have syntax for that yet.)


[parameters]
# These are placehodlers for the template parameters
# You can add more parameters here, or remove them if you don't need them.
project-description = { type = "string", prompt = "Description", default = "" }
Loading