File tree Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Expand file tree Collapse file tree 1 file changed +18
-6
lines changed Original file line number Diff line number Diff line change @@ -39,19 +39,31 @@ The server runs within the the current task. To stop the server, cancel the task
3939
4040``` swift
4141let task = Task { try await server.start () }
42-
4342task.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
You can’t perform that action at this time.
0 commit comments