forked from apple/swift-http-api-proposal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleMiddlewareServer.swift
More file actions
82 lines (75 loc) · 2.82 KB
/
Copy pathExampleMiddlewareServer.swift
File metadata and controls
82 lines (75 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift HTTP API Proposal open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift HTTP API Proposal project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import ExampleMiddleware
import HTTPAPIs
import Logging
import Middleware
/// This is an example server that wraps an HTTP server inside a middleware.
@available(anyAppleOS 26.0, *)
struct ExampleMiddlewareServer<
Server: HTTPServer,
ServerMiddleware: Middleware & Sendable
>: ~Copyable
where
Server.RequestConcludingReader: ~Copyable,
Server.RequestConcludingReader.Underlying: ~Copyable,
Server.ResponseConcludingWriter: ~Copyable,
Server.ResponseConcludingWriter.Underlying: ~Copyable,
ServerMiddleware.Input: ~Copyable,
ServerMiddleware.NextInput: ~Copyable,
ServerMiddleware.Input == HTTPServerMiddlewareInput<Server.RequestConcludingReader, Server.ResponseConcludingWriter>
{
typealias RequestConcludingReader = Server.RequestConcludingReader
typealias ResponseConcludingWriter = Server.ResponseConcludingWriter
private let server: Server
private let middleware: ServerMiddleware
init(
server: Server,
@MiddlewareBuilder
middlewareBuilder: (RequestMiddleware<Server>) -> ServerMiddleware
) {
self.server = server
self.middleware = middlewareBuilder(RequestMiddleware<Server>())
}
consuming func serve() async throws {
let middleware = self.middleware
try await self.server.serve { request, requestContext, requestBodyAndTrailers, responseSender in
let input: ServerMiddleware.Input = ServerMiddleware.Input(
request: request,
requestContext: requestContext,
requestReader: requestBodyAndTrailers,
responseSender: responseSender
)
return try await middleware.intercept(
input: input
) { _ in }
}
}
}
@available(anyAppleOS 26.0, *)
struct RequestMiddleware<Server: HTTPServer>: Middleware
where
Server.RequestConcludingReader: ~Copyable,
Server.RequestConcludingReader.Underlying: ~Copyable,
Server.ResponseConcludingWriter: ~Copyable,
Server.ResponseConcludingWriter.Underlying: ~Copyable
{
typealias Input = HTTPServerMiddlewareInput<Server.RequestConcludingReader, Server.ResponseConcludingWriter>
typealias NextInput = Input
func intercept<Return: ~Copyable>(
input: consuming Input,
next: (consuming NextInput) async throws -> Return
) async throws -> Return {
try await next(input)
}
}