ServerBuilder.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. extension Server {
  19. public class Builder {
  20. private let group: EventLoopGroup
  21. private var maybeTLS: Server.Configuration.TLS? { return nil }
  22. private var providers: [CallHandlerProvider] = []
  23. private var errorDelegate: ServerErrorDelegate?
  24. private var messageEncoding: ServerMessageEncoding = .disabled
  25. private var connectionKeepalive: ServerConnectionKeepalive = ServerConnectionKeepalive()
  26. private var connectionIdleTimeout: TimeAmount = .minutes(5)
  27. private var httpTargetWindowSize: Int = 65535
  28. fileprivate init(group: EventLoopGroup) {
  29. self.group = group
  30. }
  31. public class Secure: Builder {
  32. private var tls: Server.Configuration.TLS
  33. override var maybeTLS: Server.Configuration.TLS? {
  34. return self.tls
  35. }
  36. fileprivate init(group: EventLoopGroup, certificateChain: [NIOSSLCertificate], privateKey: NIOSSLPrivateKey) {
  37. self.tls = .init(
  38. certificateChain: certificateChain.map { .certificate($0) },
  39. privateKey: .privateKey(privateKey)
  40. )
  41. super.init(group: group)
  42. }
  43. }
  44. public func bind(host: String, port: Int) -> EventLoopFuture<Server> {
  45. let configuration = Server.Configuration(
  46. target: .hostAndPort(host, port),
  47. eventLoopGroup: self.group,
  48. serviceProviders: self.providers,
  49. errorDelegate: self.errorDelegate,
  50. tls: self.maybeTLS,
  51. connectionKeepalive: self.connectionKeepalive,
  52. connectionIdleTimeout: self.connectionIdleTimeout,
  53. messageEncoding: self.messageEncoding,
  54. httpTargetWindowSize: self.httpTargetWindowSize
  55. )
  56. return Server.start(configuration: configuration)
  57. }
  58. }
  59. }
  60. extension Server.Builder {
  61. /// Sets the server error delegate.
  62. @discardableResult
  63. public func withErrorDelegate(_ delegate: ServerErrorDelegate?) -> Self {
  64. self.errorDelegate = delegate
  65. return self
  66. }
  67. }
  68. extension Server.Builder {
  69. /// Sets the service providers that this server should offer. Note that calling this multiple
  70. /// times will override any previously set providers.
  71. @discardableResult
  72. public func withServiceProviders(_ providers: [CallHandlerProvider]) -> Self {
  73. self.providers = providers
  74. return self
  75. }
  76. }
  77. extension Server.Builder {
  78. @discardableResult
  79. public func withKeepalive(_ keepalive: ServerConnectionKeepalive) -> Self {
  80. self.connectionKeepalive = keepalive
  81. return self
  82. }
  83. }
  84. extension Server.Builder {
  85. /// The amount of time to wait before closing connections. The idle timeout will start only
  86. /// if there are no RPCs in progress and will be cancelled as soon as any RPCs start. Defaults to
  87. /// 5 minutes if not set.
  88. @discardableResult
  89. public func withConnectionIdleTimeout(_ timeout: TimeAmount) -> Self {
  90. self.connectionIdleTimeout = timeout
  91. return self
  92. }
  93. }
  94. extension Server.Builder {
  95. /// Sets the message compression configuration. Compression is disabled if this is not configured
  96. /// and any RPCs using compression will not be accepted.
  97. @discardableResult
  98. public func withMessageCompression(_ encoding: ServerMessageEncoding) -> Self {
  99. self.messageEncoding = encoding
  100. return self
  101. }
  102. }
  103. extension Server.Builder.Secure {
  104. /// Sets the trust roots to use to validate certificates. This only needs to be provided if you
  105. /// intend to validate certificates. Defaults to the system provided trust store (`.default`) if
  106. /// not set.
  107. @discardableResult
  108. public func withTLS(trustRoots: NIOSSLTrustRoots) -> Self {
  109. self.tls.trustRoots = trustRoots
  110. return self
  111. }
  112. /// Sets whether certificates should be verified. Defaults to `.none` if not set.
  113. @discardableResult
  114. public func withTLS(certificateVerification: CertificateVerification) -> Self {
  115. self.tls.certificateVerification = certificateVerification
  116. return self
  117. }
  118. }
  119. extension Server.Builder {
  120. /// Sets the HTTP/2 flow control target window size. Defaults to 65,535 if not explicitly set.
  121. @discardableResult
  122. public func withHTTPTargetWindowSize(_ httpTargetWindowSize: Int) -> Self {
  123. self.httpTargetWindowSize = httpTargetWindowSize
  124. return self
  125. }
  126. }
  127. extension Server {
  128. /// Returns an insecure `Server` builder which is *not configured with TLS*.
  129. public static func insecure(group: EventLoopGroup) -> Builder {
  130. return Builder(group: group)
  131. }
  132. /// Returns a `Server` builder configured with TLS.
  133. public static func secure(
  134. group: EventLoopGroup,
  135. certificateChain: [NIOSSLCertificate],
  136. privateKey: NIOSSLPrivateKey
  137. ) -> Builder.Secure {
  138. return Builder.Secure(
  139. group: group,
  140. certificateChain: certificateChain,
  141. privateKey: privateKey
  142. )
  143. }
  144. }