ReflectionServer.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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",
  33. subdirectory: "Generated"
  34. ),
  35. let echoURL = Bundle.module.url(
  36. forResource: "echo",
  37. withExtension: "grpc.reflection",
  38. subdirectory: "Generated"
  39. )
  40. else {
  41. print("The resource could not be loaded.")
  42. throw ExitCode.failure
  43. }
  44. let reflectionService = try ReflectionService(
  45. reflectionDataFileURLs: [greeterURL, echoURL],
  46. version: .v1
  47. )
  48. // Start the server and print its address once it has started.
  49. let server = try await Server.insecure(group: MultiThreadedEventLoopGroup.singleton)
  50. .withServiceProviders([reflectionService, GreeterProvider(), EchoProvider()])
  51. .bind(host: "localhost", port: 1234)
  52. .get()
  53. print("server started on port \(server.channel.localAddress!.port!)")
  54. // Wait on the server's `onClose` future to stop the program from exiting.
  55. try await server.onClose.get()
  56. }
  57. }