ServerBuilder.swift 4.0 KB

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