HelloWorldAdopter.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 GRPCCore
  17. import GRPCInProcessTransport
  18. import GRPCProtobuf
  19. @main
  20. struct PluginAdopter {
  21. static func main() async throws {
  22. let inProcess = InProcessTransport()
  23. try await withGRPCServer(transport: inProcess.server, services: [Greeter()]) { server in
  24. try await withGRPCClient(transport: inProcess.client) { client in
  25. try await Self.doRPC(Helloworld_Greeter.Client(wrapping: client))
  26. }
  27. }
  28. }
  29. static func doRPC<Transport>(_ greeter: Helloworld_Greeter.Client<Transport>) async throws {
  30. do {
  31. let reply = try await greeter.sayHello(.with { $0.name = "(ignored)" })
  32. print("Reply: \(reply.message)")
  33. } catch {
  34. print("Error: \(error)")
  35. }
  36. }
  37. }
  38. struct Greeter: Helloworld_Greeter.SimpleServiceProtocol {
  39. func sayHello(
  40. request: Helloworld_HelloRequest,
  41. context: ServerContext
  42. ) async throws -> Helloworld_HelloReply {
  43. return .with { reply in
  44. reply.message = "Hello, world!"
  45. }
  46. }
  47. }