Browse Source

Expose server SSL configuration in prototype of generated constructor.

Tim Burks 9 years ago
parent
commit
8c607c092f

+ 6 - 4
Examples/Echo/Swift/Echo/AppDelegate.swift

@@ -48,14 +48,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
 
     // create and start a server for handling insecure requests
     insecureServer = Echo_EchoServer(address:"localhost:8081",
-                                     handler:echoHandler,
-                                     secure:false)
+                                     handler:echoHandler)
     insecureServer.start()
 
     // create and start a server for handling secure requests
+    let certificateURL = Bundle.main.url(forResource: "ssl", withExtension: "crt")!
+    let keyURL = Bundle.main.url(forResource: "ssl", withExtension: "key")!
     secureServer = Echo_EchoServer(address:"localhost:8443",
-                                   handler:echoHandler,
-                                   secure:true)
+                                   certificateURL:certificateURL,
+                                   keyURL:keyURL,
+                                   handler:echoHandler)
     secureServer.start()
   }
 }

+ 16 - 10
Examples/Echo/Swift/Echo/echo.server.pb.swift

@@ -237,21 +237,27 @@ public class Echo_EchoServer {
   public var handler: Echo_EchoHandler!
 
   public init(address:String,
-              handler:Echo_EchoHandler,
-              secure:Bool) {
+              handler:Echo_EchoHandler) {
     gRPC.initialize()
     self.address = address
     self.handler = handler
-    if secure {
-      let certificateURL = Bundle.main.url(forResource: "ssl", withExtension: "crt")!
+    self.server = gRPC.Server(address:address)
+  }
 
-      let certificate = try! String(contentsOf: certificateURL)
-      let keyURL = Bundle.main.url(forResource: "ssl", withExtension: "key")!
-      let key = try! String(contentsOf: keyURL)
-      self.server = gRPC.Server(address:address, key:key, certs:certificate)
-    } else {
-      self.server = gRPC.Server(address:address)
+  public init?(address:String,
+               certificateURL:URL,
+               keyURL:URL,
+               handler:Echo_EchoHandler) {
+    gRPC.initialize()
+    self.address = address
+    self.handler = handler
+    guard
+      let certificate = try? String(contentsOf: certificateURL),
+      let key = try? String(contentsOf: keyURL)
+      else {
+        return nil
     }
+    self.server = gRPC.Server(address:address, key:key, certs:certificate)
   }
 
   public func start(queue:DispatchQueue = DispatchQueue.global()) {