ServerBuilder.swift 4.9 KB

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