Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Jul 13, 2024
1 parent 7711e55 commit 7ff3181
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,14 @@ route ~= HTTPRequest(method: .GET, path: "/hello/dog/world") // true
route ~= HTTPRequest(method: .GET, path: "/hello/fish/sea") // false
```

Routes can include named [parameters](#route-parameters) that match like wildcards, allowing the values to be extracted from the request.

```swift
let route = HTTPRoute("GET /hello/:beast/world")

let beast = request.routeParameters["beast"]
```

Trailing wildcards match all trailing path components:

```swift
Expand Down Expand Up @@ -280,6 +288,30 @@ let route = HTTPRoute("POST *", body: .json(where: "food == 'fish'"))
{"side": "chips", "food": "fish"}
```

## Route Parameters

With Swift 5.9, closure routes can include parameters that are automatically extracted from the request and passed to the handler:

```swift
enum Beast: String, HTTPRouteParameterValue {
case fish
case dog
}

handler.appendRoute("GET /creature/:name?type=:beast") { (name: String, beast: Beast) -> HTTPResponse in
return HTTPResponse(statusCode: .ok)
}
```

All other handlers can extract parameters from the request:

```swift
func handle(request: HTTPRequest) async throws -> HTTPResonse {
let name = request.routeParameters["name"]
let beast = request.routeParameters["beast"]
return HTTPResponse(statusCode: .ok)
}

## WebSockets
`HTTPResponse` can switch the connection to the [WebSocket](https://datatracker.ietf.org/doc/html/rfc6455) protocol by provding a `WSHandler` within the response payload.

Expand Down

0 comments on commit 7ff3181

Please sign in to comment.