Serve.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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(address: .ipv4(host: "127.0.0.1", port: self.port))
  27. let server = GRPCServer(transport: transport, services: [EchoService()])
  28. try await withThrowingDiscardingTaskGroup { group in
  29. group.addTask { try await server.run() }
  30. let address = try await transport.listeningAddress
  31. print("server listening on \(address)")
  32. }
  33. }
  34. }
  35. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  36. struct EchoService: Echo_EchoServiceProtocol {
  37. func get(
  38. request: ServerRequest.Single<Echo_EchoRequest>
  39. ) async throws -> ServerResponse.Single<Echo_EchoResponse> {
  40. return ServerResponse.Single(message: .with { $0.text = request.message.text })
  41. }
  42. func collect(
  43. request: ServerRequest.Stream<Echo_EchoRequest>
  44. ) async throws -> ServerResponse.Single<Echo_EchoResponse> {
  45. let messages = try await request.messages.reduce(into: []) { $0.append($1.text) }
  46. let joined = messages.joined(separator: " ")
  47. return ServerResponse.Single(message: .with { $0.text = joined })
  48. }
  49. func expand(
  50. request: ServerRequest.Single<Echo_EchoRequest>
  51. ) async throws -> ServerResponse.Stream<Echo_EchoResponse> {
  52. return ServerResponse.Stream { writer in
  53. let parts = request.message.text.split(separator: " ")
  54. let messages = parts.map { part in Echo_EchoResponse.with { $0.text = String(part) } }
  55. try await writer.write(contentsOf: messages)
  56. return [:]
  57. }
  58. }
  59. func update(
  60. request: ServerRequest.Stream<Echo_EchoRequest>
  61. ) async throws -> ServerResponse.Stream<Echo_EchoResponse> {
  62. return ServerResponse.Stream { writer in
  63. for try await message in request.messages {
  64. try await writer.write(.with { $0.text = message.text })
  65. }
  66. return [:]
  67. }
  68. }
  69. }