ServerBuilder.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2020, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import NIO
  17. import NIOSSL
  18. import Logging
  19. extension Server {
  20. public class Builder {
  21. private let group: EventLoopGroup
  22. private var maybeTLS: Server.Configuration.TLS? { return nil }
  23. private var providers: [CallHandlerProvider] = []
  24. private var errorDelegate: ServerErrorDelegate?
  25. private var messageEncoding: ServerMessageEncoding = .disabled
  26. private var connectionKeepalive: ServerConnectionKeepalive = ServerConnectionKeepalive()
  27. private var connectionIdleTimeout: TimeAmount = .minutes(5)
  28. private var httpTargetWindowSize: Int = 65535
  29. private var logger: Logger = Logger(label: "io.grpc", factory: { _ in SwiftLogNoOpLogHandler() })
  30. fileprivate init(group: EventLoopGroup) {
  31. self.group = group
  32. }
  33. public class Secure: Builder {
  34. private var tls: Server.Configuration.TLS
  35. override var maybeTLS: Server.Configuration.TLS? {
  36. return self.tls
  37. }
  38. fileprivate init(group: EventLoopGroup, certificateChain: [NIOSSLCertificate], privateKey: NIOSSLPrivateKey) {
  39. self.tls = .init(
  40. certificateChain: certificateChain.map { .certificate($0) },
  41. privateKey: .privateKey(privateKey)
  42. )
  43. super.init(group: group)
  44. }
  45. }
  46. public func bind(host: String, port: Int) -> EventLoopFuture<Server> {
  47. let configuration = Server.Configuration(
  48. target: .hostAndPort(host, port),
  49. eventLoopGroup: self.group,
  50. serviceProviders: self.providers,
  51. errorDelegate: self.errorDelegate,
  52. tls: self.maybeTLS,
  53. connectionKeepalive: self.connectionKeepalive,
  54. connectionIdleTimeout: self.connectionIdleTimeout,
  55. messageEncoding: self.messageEncoding,
  56. httpTargetWindowSize: self.httpTargetWindowSize,
  57. logger: self.logger
  58. )
  59. return Server.start(configuration: configuration)
  60. }
  61. }
  62. }
  63. extension Server.Builder {
  64. /// Sets the server error delegate.
  65. @discardableResult
  66. public func withErrorDelegate(_ delegate: ServerErrorDelegate?) -> Self {
  67. self.errorDelegate = delegate
  68. return self
  69. }
  70. }
  71. extension Server.Builder {
  72. /// Sets the service providers that this server should offer. Note that calling this multiple
  73. /// times will override any previously set providers.
  74. @discardableResult
  75. public func withServiceProviders(_ providers: [CallHandlerProvider]) -> Self {
  76. self.providers = providers
  77. return self
  78. }
  79. }
  80. extension Server.Builder {
  81. @discardableResult
  82. public func withKeepalive(_ keepalive: ServerConnectionKeepalive) -> Self {
  83. self.connectionKeepalive = keepalive
  84. return self
  85. }
  86. }
  87. extension Server.Builder {
  88. /// The amount of time to wait before closing connections. The idle timeout will start only
  89. /// if there are no RPCs in progress and will be cancelled as soon as any RPCs start. Defaults to
  90. /// 5 minutes if not set.
  91. @discardableResult
  92. public func withConnectionIdleTimeout(_ timeout: TimeAmount) -> Self {
  93. self.connectionIdleTimeout = timeout
  94. return self
  95. }
  96. }
  97. extension Server.Builder {
  98. /// Sets the message compression configuration. Compression is disabled if this is not configured
  99. /// and any RPCs using compression will not be accepted.
  100. @discardableResult
  101. public func withMessageCompression(_ encoding: ServerMessageEncoding) -> Self {
  102. self.messageEncoding = encoding
  103. return self
  104. }
  105. }
  106. extension Server.Builder.Secure {
  107. /// Sets the trust roots to use to validate certificates. This only needs to be provided if you
  108. /// intend to validate certificates. Defaults to the system provided trust store (`.default`) if
  109. /// not set.
  110. @discardableResult
  111. public func withTLS(trustRoots: NIOSSLTrustRoots) -> Self {
  112. self.tls.trustRoots = trustRoots
  113. return self
  114. }
  115. /// Sets whether certificates should be verified. Defaults to `.none` if not set.
  116. @discardableResult
  117. public func withTLS(certificateVerification: CertificateVerification) -> Self {
  118. self.tls.certificateVerification = certificateVerification
  119. return self
  120. }
  121. }
  122. extension Server.Builder {
  123. /// Sets the HTTP/2 flow control target window size. Defaults to 65,535 if not explicitly set.
  124. @discardableResult
  125. public func withHTTPTargetWindowSize(_ httpTargetWindowSize: Int) -> Self {
  126. self.httpTargetWindowSize = httpTargetWindowSize
  127. return self
  128. }
  129. }
  130. extension Server.Builder {
  131. /// Sets the root server logger. Accepted connections will branch from this logger and RPCs on
  132. /// each connection will use a logger branched from the connections logger. This logger is made
  133. /// available to service providers via `context`. Defaults to a no-op logger.
  134. @discardableResult
  135. public func withLogger(_ logger: Logger) -> Self {
  136. self.logger = logger
  137. return self
  138. }
  139. }
  140. extension Server {
  141. /// Returns an insecure `Server` builder which is *not configured with TLS*.
  142. public static func insecure(group: EventLoopGroup) -> Builder {
  143. return Builder(group: group)
  144. }
  145. /// Returns a `Server` builder configured with TLS.
  146. public static func secure(
  147. group: EventLoopGroup,
  148. certificateChain: [NIOSSLCertificate],
  149. privateKey: NIOSSLPrivateKey
  150. ) -> Builder.Secure {
  151. return Builder.Secure(
  152. group: group,
  153. certificateChain: certificateChain,
  154. privateKey: privateKey
  155. )
  156. }
  157. }