Skip to content
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

Add example of extracting the IP from the RequestContextSource #51

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
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
26 changes: 26 additions & 0 deletions Hummingbird.docc/Articles/RequestContexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ struct MyMiddleware: MiddlewareProtocol {

Now anything run after `MyMiddleware` can access the `additionalData` set in `MyMiddleware`.

## Using RequestContextSource

You can also use the RequestContext to store information from the ``RequestContextSource``. If you are running a Hummingbird server then this contains the Swift NIO `Channel` that generated the request. Below is an example of extracting the remote IP from the Channel and passing it to an endpoint.

```swift
/// RequestContext that includes a copy of the Channel that created it
struct AppRequestContext: RequestContext {
var coreContext: CoreRequestContextStorage
let channel: Channel

init(source: Source) {
self.coreContext = .init(source: source)
self.channel = source.channel
}

/// Extract Remote IP from Channel
var remoteAddress: SocketAddress? { self.channel.remoteAddress }
}

let router = Router(context: AppRequestContext.self)
router.get("ip") { _, context in
guard let ip = context.remoteAddress else { throw HTTPError(.badRequest) }
return "Your IP is \(ip)"
}
```

## Authentication Middleware

The most obvious example of this is passing user authentication information forward. The authentication framework from ``HummingbirdAuth`` makes use of this. If you want to use the authentication and sessions middleware your context will also need to conform to ``HummingbirdAuth/AuthRequestContext``.
Expand Down