ServerBuilder.swift 4.3 KB

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