Skip to content
Merged
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
28 changes: 28 additions & 0 deletions Hummingbird.docc/Articles/RequestContexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ struct MyRequestContext: RequestContext {
}
```

### Limiting memory usage

You can limit the amount of memory uploaded to your server when decoding requests by setting the value ``Hummingbird/RequestContext/maxUploadSize`` in your `RequestContext`. If someone sends a request larger than the `maxUploadSize` value defined the server will respond with a response with status code `(413) Content too large`. The `maxUploadSize` value defaults to 2MB. The request decoders provided with Hummingbird for JSON and URL Encoded Forms both use this limit.

You can find out more about request decoding and response encoding in <doc:RequestDecoding> and <doc:ResponseEncoding>.

## Passing data forward
Expand Down Expand Up @@ -116,6 +120,30 @@ router.get("ip") { _, context in
}
```

### RequestContext transformation

The `RequestContext` can be transformed for the routes in a route group. The `RequestContext` you are converting to needs to conform to ``ChildRequestContext``. This requires a parent context ie the `RequestContext` you are converting from and a ``ChildRequestContext/init(context:)`` function to perform the conversion.

```swift
struct MyNewRequestContext: ChildRequestContext {
typealias ParentContext = MyRequestContext
init(context: ParentContext) throws {
self.coreContext = context.coreContext
...
}
}
```
Once you have defined how to perform the transform from your original `RequestContext` the conversion is added as follows

```swift
let app = Application(context: MyRequestContext.self)
router.group("/todos", context: MyNewRequestContext.self)
.put(use: createTodo)
.get(use: listTodos)
```

Transforming the `RequestContext` is a powerful way of enforcing compile-time guarantees that requests adhere to certain requirements. And by expressing these requirements as protocol conformances, you can compose these properties and flexibly express those requirements.

## 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
22 changes: 0 additions & 22 deletions Hummingbird.docc/Articles/RouterGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,28 +172,6 @@ router.group("/todos")
.delete("{id}", deleteTodo)
```

### RequestContext transformation

The `RequestContext` can be transformed for the routes in a route group. The `RequestContext` you are converting to needs to conform to ``ChildRequestContext``. This requires a parent context ie the `RequestContext` you are converting from and a ``ChildRequestContext/init(context:)`` function to perform the conversion.

```swift
struct MyNewRequestContext: ChildRequestContext {
typealias ParentContext = MyRequestContext
init(context: ParentContext) throws {
self.coreContext = context.coreContext
...
}
}
```
Once you have defined how to perform the transform from your original `RequestContext` the conversion is added as follows

```swift
let app = Application(context: MyRequestContext.self)
router.group("/todos", context: MyNewRequestContext.self)
.put(use: createTodo)
.get(use: listTodos)
```

### Route Collections

A ``RouteCollection`` is a collection of routes and middleware that can be added to a `Router` in one go. It has the same API as `RouterGroup`, so can have groups internal to the collection to allow for Middleware to applied to only sub-sections of the `RouteCollection`.
Expand Down