Serve.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2024, 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 ArgumentParser
  17. import GRPCCore
  18. import GRPCHTTP2Core
  19. import GRPCHTTP2TransportNIOPosix
  20. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  21. struct Serve: AsyncParsableCommand {
  22. static let configuration = CommandConfiguration(abstract: "Starts an echo server.")
  23. @Option(help: "The port to listen on")
  24. var port: Int = 1234
  25. func run() async throws {
  26. let transport = HTTP2ServerTransport.Posix(
  27. address: .ipv4(host: "127.0.0.1", port: self.port),
  28. config: .defaults(transportSecurity: .plaintext)
  29. )
  30. let server = GRPCServer(transport: transport, services: [EchoService()])
  31. try await withThrowingDiscardingTaskGroup { group in
  32. group.addTask { try await server.run() }
  33. let address = try await transport.listeningAddress
  34. print("server listening on \(address)")
  35. }
  36. }
  37. }
  38. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  39. struct EchoService: Echo_EchoServiceProtocol {
  40. func get(
  41. request: ServerRequest.Single<Echo_EchoRequest>
  42. ) async throws -> ServerResponse.Single<Echo_EchoResponse> {
  43. return ServerResponse.Single(message: .with { $0.text = request.message.text })
  44. }
  45. func collect(
  46. request: ServerRequest.Stream<Echo_EchoRequest>
  47. ) async throws -> ServerResponse.Single<Echo_EchoResponse> {
  48. let messages = try await request.messages.reduce(into: []) { $0.append($1.text) }
  49. let joined = messages.joined(separator: " ")
  50. return ServerResponse.Single(message: .with { $0.text = joined })
  51. }
  52. func expand(
  53. request: ServerRequest.Single<Echo_EchoRequest>
  54. ) async throws -> ServerResponse.Stream<Echo_EchoResponse> {
  55. return ServerResponse.Stream { writer in
  56. let parts = request.message.text.split(separator: " ")
  57. let messages = parts.map { part in Echo_EchoResponse.with { $0.text = String(part) } }
  58. try await writer.write(contentsOf: messages)
  59. return [:]
  60. }
  61. }
  62. func update(
  63. request: ServerRequest.Stream<Echo_EchoRequest>
  64. ) async throws -> ServerResponse.Stream<Echo_EchoResponse> {
  65. return ServerResponse.Stream { writer in
  66. for try await message in request.messages {
  67. try await writer.write(.with { $0.text = message.text })
  68. }
  69. return [:]
  70. }
  71. }
  72. }