Skip to content

Add middleware example#17

Merged
FranzBusch merged 4 commits into
mainfrom
fb-middleware-example
May 12, 2026
Merged

Add middleware example#17
FranzBusch merged 4 commits into
mainfrom
fb-middleware-example

Conversation

@FranzBusch
Copy link
Copy Markdown
Member

@FranzBusch FranzBusch commented Nov 28, 2025

This PR adds an example showcasing how middleware can be used to intercept requests handled by a server and performed by a client. In detail, this PR adds a logging and request handling middleware. The logging middleware is capable of inspecting every single part of the request including the individual chunks read.

Copy link
Copy Markdown
Contributor

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

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

This is awesome - the logging middleware is a great example of a fully powerful middleware that also inspects individual body chunks. Though it does make it appear pretty complicated - I wonder if you could add (in a subsequent PR) a simpler middleware example that doesn't need to implement its own reader/writer, and only inspects the headers/trailers, so in theory should be much simpler.

Comment thread Examples/MiddlewareServer/MiddlewareServer.swift Outdated
Comment thread Examples/MiddlewareServer/MiddlewareServer.swift
Comment thread Examples/ExampleMiddleware/HTTPServerRequestHandlerMiddleware.swift
@FranzBusch FranzBusch force-pushed the fb-middleware-example branch from 5ffce3f to 0761920 Compare December 1, 2025 14:46
@FranzBusch FranzBusch changed the title Add middleware server example Add middleware example Dec 1, 2025
This PR adds an example showcasing how middleware can be used to intercept requests handled by a server. In detail, this PR adds a logging and request handling middleware. The logging middleware is capable of inspecting every single part of the request including the individual chunks read.
@FranzBusch FranzBusch force-pushed the fb-middleware-example branch 2 times, most recently from c0c7c16 to bb8c3a5 Compare May 12, 2026 12:22
@FranzBusch FranzBusch added the 🆕 semver/minor Adds new public API. label May 12, 2026
@FranzBusch FranzBusch force-pushed the fb-middleware-example branch 2 times, most recently from 2ce71b7 to 4a65990 Compare May 12, 2026 12:29
Comment thread Package.swift Outdated
Comment on lines +46 to +47
next: (consuming NextInput) async throws -> Return
) async throws -> Return
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it make sense to allow for separate Return types in next and in intercept? What if a middleware wants to transform next's return value into something else?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I am not sure that transforming the Return type would work since in the end this is the type that a user returns from their perform closure. Let's keep it to one type for now.

Comment thread Sources/Middleware/Middleware.swift
Comment thread Sources/HTTPAPIs/Server/HTTPServerClosureRequestHandler.swift Outdated
Comment thread Sources/HTTPAPIs/Server/HTTPServerRequestHandler.swift Outdated
Comment thread Examples/ExampleMiddleware/ForwardingMiddleware.swift
Comment thread Examples/ExampleMiddleware/HTTPServerLoggingMiddleware.swift Outdated
Comment thread Examples/MiddlewareClient/MiddlewareClient.swift Outdated
Comment on lines +67 to +68
consuming RequestReader,
consuming HTTPResponseSender<ResponseWriter>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should these be sending too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It would require us to take the sender as sending right now I didn't need that. Let's keep an eye if for some middleware we want this.

Comment on lines 45 to +46
input: consuming Input,
next: (consuming NextInput) async throws -> Void
) async throws
next: (consuming NextInput) async throws -> Return
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should these be sending?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Same here

@FranzBusch FranzBusch force-pushed the fb-middleware-example branch from 4a65990 to 1a6330e Compare May 12, 2026 13:05
Comment thread Sources/HTTPAPIs/Server/HTTPServerRequestHandler.swift Outdated
@FranzBusch FranzBusch force-pushed the fb-middleware-example branch from 6ede67d to 42029f3 Compare May 12, 2026 13:48
@FranzBusch FranzBusch force-pushed the fb-middleware-example branch from 42029f3 to ffed766 Compare May 12, 2026 13:50
@FranzBusch FranzBusch enabled auto-merge (squash) May 12, 2026 13:54
@FranzBusch FranzBusch disabled auto-merge May 12, 2026 14:02
@FranzBusch FranzBusch merged commit d8a30c9 into main May 12, 2026
15 of 17 checks passed
@FranzBusch FranzBusch deleted the fb-middleware-example branch May 12, 2026 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants