-
Notifications
You must be signed in to change notification settings - Fork 307
WIP: add http middleware template #3617
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| target/ | ||
| .spin/ |
| 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] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| spin_manifest_version = 2 | ||
|
|
||
| [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"] | ||
| 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) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| [[trigger.http]] | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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).
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| manifest_version = "1" | ||
| id = "http-middleware" | ||
| description = "http middleware rust template" | ||
| tags = [] | ||
|
|
||
| [new_application] | ||
| supported = false | ||
|
|
||
| [add_component] | ||
| skip_files = ["spin.toml"] | ||
| [add_component.snippets] | ||
| component = "component.txt" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = "" } | ||
There was a problem hiding this comment.
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 testThere was a problem hiding this comment.
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 withspin testit's not clear how a standalone piece of middleware could work.