ReflectionServer.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright 2023, 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 EchoImplementation
  18. import EchoModel
  19. import Foundation
  20. import GRPC
  21. import GRPCReflectionService
  22. import NIOPosix
  23. import SwiftProtobuf
  24. @available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
  25. @main
  26. struct ReflectionServer: AsyncParsableCommand {
  27. func run() async throws {
  28. // Getting the URLs of the files containing the reflection data.
  29. guard
  30. let greeterURL = Bundle.module.url(
  31. forResource: "helloworld",
  32. withExtension: "grpc.reflection.txt"
  33. ),
  34. let echoURL = Bundle.module.url(forResource: "echo", withExtension: "grpc.reflection.txt")
  35. else {
  36. print("The resource could not be loaded.")
  37. throw ExitCode.failure
  38. }
  39. let reflectionService = try ReflectionService(
  40. reflectionDataFileURLs: [greeterURL, echoURL],
  41. version: .v1
  42. )
  43. // Start the server and print its address once it has started.
  44. let server = try await Server.insecure(group: MultiThreadedEventLoopGroup.singleton)
  45. .withServiceProviders([reflectionService, GreeterProvider(), EchoProvider()])
  46. .bind(host: "localhost", port: 1234)
  47. .get()
  48. print("server started on port \(server.channel.localAddress!.port!)")
  49. // Wait on the server's `onClose` future to stop the program from exiting.
  50. try await server.onClose.get()
  51. }
  52. }