Skip to content

Commit 7117d2d

Browse files
authored
Update README.md
1 parent ceda81b commit 7117d2d

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,31 @@ The server runs within the the current task. To stop the server, cancel the task
3939

4040
```swift
4141
let task = Task { try await server.start() }
42-
4342
task.cancel()
4443
```
4544

4645
## Handlers
4746

48-
Handlers can be added to the server for a corresponding route:
47+
Handlers can be added to the server by implementing `HTTPHandler`:
48+
49+
```swift
50+
public protocol HTTPHandler: Sendable {
51+
func handleRequest(_ request: HTTPRequest) async throws -> HTTPResponse
52+
}
53+
```
54+
55+
Handlers can be added to the server against a corresponding route:
56+
57+
```swift
58+
await server.appendHandler(for: "/hello", handler: handler)
59+
```
60+
61+
Closures can be added to the server to handle requests:
4962

5063
```swift
51-
await server.appendHandler(for: "/hello") { request in
52-
try await Task.sleep(nanoseconds: 1_000_000_000)
53-
return HTTPResponse(statusCode: .ok,
54-
body: "Hello World!".data(using: .utf8)!)
64+
await server.appendHandler(for: "/hello") { request in
65+
try await Task.sleep(nanoseconds: 1_000_000_000)
66+
return HTTPResponse(statusCode: .ok)
5567
}
5668
```
5769

0 commit comments

Comments
 (0)