Skip to content

Commit 43406c9

Browse files
authored
Update README.md
1 parent be2e0ce commit 43406c9

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,33 @@ public protocol HTTPHandler: Sendable {
5252
}
5353
```
5454

55-
Handlers can be added to the server against a corresponding route:
55+
Routes can be added to the server delegating requests to a handler:
5656

5757
```swift
58-
await server.appendHandler(for: "/hello", handler: handler)
58+
await server.appendRoute("/hello", to: handler)
5959
```
6060

61-
Closures can be added to the server to handle requests:
61+
Routes can also be added to closures:
6262

6363
```swift
64-
await server.appendHandler(for: "/hello") { request in
64+
await server.appendRoute("/hello") { request in
6565
try await Task.sleep(nanoseconds: 1_000_000_000)
6666
return HTTPResponse(statusCode: .ok)
6767
}
6868
```
6969

70-
Incoming requests are routed to the first handler with a matching route.
70+
Incoming requests are routed to the handler of the first matching route.
7171

72-
Handlers can throw `HTTPUnhandledError` if after inspecting the request, they cannot handle it. The next matched handler is then used.
72+
Handlers can throw `HTTPUnhandledError` if after inspecting the request, they cannot handle it. The next matching route is then used.
7373

74-
Unhandled requests receive `HTTP 404`.
74+
Requests that do not match any handled route receive `HTTP 404`.
7575

7676
### FileHTTPHandler
7777

78-
Requests can be routed to static files via `FileHTTPHandler`:
78+
Requests can be routed to static files with `FileHTTPHandler`:
7979

8080
```swift
81-
await server.appendHandler(for: "GET /mock", handler: .file(named: "mock.json"))
81+
await server.appendRoute("GET /mock", to: .file(named: "mock.json"))
8282
```
8383

8484
`FileHTTPHandler` will return `HTTP 404` if the file does not exist.
@@ -88,7 +88,7 @@ await server.appendHandler(for: "GET /mock", handler: .file(named: "mock.json"))
8888
Requests can be proxied via a base URL:
8989

9090
```swift
91-
await server.appendHandler(for: "GET *", handler: .proxy(via: "https://pie.dev"))
91+
await server.appendRoute("GET *", to: .proxy(via: "https://pie.dev"))
9292
// GET /get?fish=chips ----> GET https://pie.dev/get?fish=chips
9393
```
9494

@@ -97,23 +97,23 @@ await server.appendHandler(for: "GET *", handler: .proxy(via: "https://pie.dev")
9797
Requests can be redirected to a URL:
9898

9999
```swift
100-
await server.appendHandler(for: "GET /fish/*", handler: .redirect(to: "https://pie.dev/get"))
100+
await server.appendRoute("GET /fish/*", to: .redirect(to: "https://pie.dev/get"))
101101
// GET /fish/chips ---> HTTP 301
102102
// Location: https://pie.dev/get
103103
```
104104

105-
### CompositeHTTPHandler
105+
### RoutedHTTPHandler
106106

107-
Multiple handlers can be grouped with requests matched against `HTTPRoute` using `CompositeHTTPHandler`.
107+
Multiple handlers can be grouped with requests and matched against `HTTPRoute` using `RoutedHTTPHandler`.
108108

109109
```swift
110-
var handlers = CompositeHTTPHandler()
111-
handlers.appendHandler(for: "GET /fish/chips", handler: .file(named: "chips.json"))
112-
handlers.appendHandler(for: "GET /fish/mushy_peas", handler: .file(named: "mushy_peas.json"))
113-
await server.appendHandler(for: "GET /fish/*", handler: handlers)
110+
var routes = RoutedHTTPHandler()
111+
routes.appendRoute("GET /fish/chips", to: .file(named: "chips.json"))
112+
routes.appendRoute("GET /fish/mushy_peas", to: .file(named: "mushy_peas.json"))
113+
await server.appendRoute(for: "GET /fish/*", to: routes)
114114
```
115115

116-
`HTTPUnhandledError` is thrown if `CompositeHTTPHandler` is unable to handle the request with any of its registered handlers. `HTTP 404` is returned as the response.
116+
`HTTPUnhandledError` is thrown if `RoutedHTTPHandler` is unable to handle the request with any of its registered handlers. `HTTP 404` is returned as the response.
117117

118118
### Wildcards
119119

0 commit comments

Comments
 (0)