main.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright 2018, 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 Commander
  17. import Dispatch
  18. import Foundation
  19. import NIO
  20. import SwiftGRPCNIO
  21. // Common flags and options
  22. func addressOption(_ address: String) -> Option<String> {
  23. return Option("address", default: address, description: "address of server")
  24. }
  25. let portOption = Option("port",
  26. default: "8080",
  27. description: "port of server")
  28. Group {
  29. $0.command("serve",
  30. addressOption("0.0.0.0"),
  31. portOption,
  32. description: "Run an echo server.") { address, port in
  33. let sem = DispatchSemaphore(value: 0)
  34. let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  35. print("starting insecure server")
  36. _ = try! GRPCServer.start(hostname: address,
  37. port: Int(port)!,
  38. eventLoopGroup: eventLoopGroup,
  39. serviceProviders: [EchoProviderNIO()])
  40. .wait()
  41. // This blocks to keep the main thread from finishing while the server runs,
  42. // but the server never exits. Kill the process to stop it.
  43. _ = sem.wait()
  44. }
  45. }.run()