Skip to content

Commit

Permalink
Explicit documentation on registering http middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
erdtsieck authored and jeremydmiller committed Nov 9, 2023
1 parent df4550d commit 8c6d153
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/guide/http/endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ and register that strategy within our `MapWolverineEndpoints()` set up like so:
// Customizing parameter handling
opts.AddParameterHandlingStrategy<NowParameterStrategy>();
```
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L143-L148' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_adding_custom_parameter_handling' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L145-L150' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_adding_custom_parameter_handling' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

And lastly, here's the application within an HTTP endpoint for extra context:
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/http/mediator.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ app.MapPostToWolverine<CustomRequest, CustomResponse>("/wolverine/request");
app.MapDeleteToWolverine<CustomRequest, CustomResponse>("/wolverine/request");
app.MapPutToWolverine<CustomRequest, CustomResponse>("/wolverine/request");
```
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L152-L164' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_optimized_mediator_usage' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L154-L166' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_optimized_mediator_usage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

With this mechanism, Wolverine is able to optimize the runtime function for Minimal API by eliminating IoC service locations
Expand Down
10 changes: 10 additions & 0 deletions docs/guide/http/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public class FakeAuthenticationMiddleware
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/MiddlewareEndpoints.cs#L103-L119' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_fake_authentication_middleware' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Which is registered like this (or as described in [`Registering Middleware by Message Type`](/guide/handlers/middleware.html##registering-middleware-by-message-type)):

<!-- snippet: sample_register_http_middleware_by_type -->
<a id='snippet-sample_register_http_middleware_by_type'></a>
```cs
opts.AddMiddlewareByMessageType(typeof(FakeAuthenticationMiddleware));
```
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Program.cs#L132-L134' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_register_http_middleware_by_type' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

The key point to notice there is that `IResult` is a "return value" of the middleware. In the case of an HTTP endpoint,
Wolverine will check if that `IResult` is a `WolverineContinue` object, and if so, will continue processing. If the `IResult`
object is anything else, Wolverine will execute that `IResult` and stop processing the HTTP request otherwise.
Expand Down
2 changes: 2 additions & 0 deletions src/Http/WolverineWebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@
opts.AddMiddleware(typeof(BeforeAndAfterMiddleware),
chain => chain.Method.HandlerType == typeof(MiddlewareEndpoints));

#region sample_register_http_middleware_by_type
opts.AddMiddlewareByMessageType(typeof(FakeAuthenticationMiddleware));
#endregion

// Publish messages coming from
opts.PublishMessage<HttpMessage1>(HttpMethod.Post, "/publish/message1");
Expand Down

0 comments on commit 8c6d153

Please sign in to comment.