-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation for supporting gRPC Web directly from Swift gRPC services.
- Loading branch information
1 parent
0195dfd
commit 24c8e5d
Showing
16 changed files
with
476 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ build | |
/protoc-gen-swiftgrpc | ||
third_party/** | ||
/Echo | ||
/EchoNIO | ||
/test.out | ||
/echo.pid | ||
/SwiftGRPC.xcodeproj | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2018, gRPC Authors All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import Foundation | ||
import NIO | ||
import SwiftGRPCNIO | ||
|
||
class EchoProviderNIO: Echo_EchoProvider_NIO { | ||
func get(request: Echo_EchoRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Echo_EchoResponse> { | ||
var response = Echo_EchoResponse() | ||
response.text = "Swift echo get: " + request.text | ||
return context.eventLoop.newSucceededFuture(result: response) | ||
} | ||
|
||
func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus> { | ||
var endOfSendOperationQueue = context.eventLoop.newSucceededFuture(result: ()) | ||
let parts = request.text.components(separatedBy: " ") | ||
for (i, part) in parts.enumerated() { | ||
var response = Echo_EchoResponse() | ||
response.text = "Swift echo expand (\(i)): \(part)" | ||
endOfSendOperationQueue = endOfSendOperationQueue.then { context.sendResponse(response) } | ||
} | ||
return endOfSendOperationQueue.map { GRPCStatus.ok } | ||
} | ||
|
||
func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> { | ||
var parts: [String] = [] | ||
return context.eventLoop.newSucceededFuture(result: { event in | ||
switch event { | ||
case .message(let message): | ||
parts.append(message.text) | ||
|
||
case .end: | ||
var response = Echo_EchoResponse() | ||
response.text = "Swift echo collect: " + parts.joined(separator: " ") | ||
context.responsePromise.succeed(result: response) | ||
} | ||
}) | ||
} | ||
|
||
func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> { | ||
var endOfSendOperationQueue = context.eventLoop.newSucceededFuture(result: ()) | ||
var count = 0 | ||
return context.eventLoop.newSucceededFuture(result: { event in | ||
switch event { | ||
case .message(let message): | ||
var response = Echo_EchoResponse() | ||
response.text = "Swift echo update (\(count)): \(message.text)" | ||
endOfSendOperationQueue = endOfSendOperationQueue.then { context.sendResponse(response) } | ||
count += 1 | ||
|
||
case .end: | ||
endOfSendOperationQueue | ||
.map { GRPCStatus.ok } | ||
.cascade(promise: context.statusPromise) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../Tests/SwiftGRPCNIOTests/echo.pb.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../../Tests/SwiftGRPCNIOTests/echo_nio.grpc.swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
all: | ||
swift build -c debug --product EchoNIO | ||
cp .build/debug/EchoNIO . | ||
|
||
project: | ||
swift package generate-xcodeproj | ||
|
||
clean : | ||
rm -rf Packages googleapis .build | ||
rm -f Package.pins Echo google.json | ||
rm -rf Package.resolved EchoNIO.xcodeproj EchoNIO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# EchoNIO, a gRPC NIO Sample App | ||
|
||
This directory contains a simple echo server that demonstrates | ||
all four gRPC API styles (Unary, Server Streaming, Client | ||
Streaming, and Bidirectional Streaming) using the NIO based | ||
Swift gRPC implementation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/sh | ||
# | ||
# Use this to run the swift-proto generator | ||
# | ||
protoc echo.proto \ | ||
--swift_out=Generated \ | ||
--swiftgrpc_out=Client=false,Server=true,NIO=true:Generated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../Echo/echo.proto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2018, gRPC Authors All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import Commander | ||
import Dispatch | ||
import Foundation | ||
import NIO | ||
import SwiftGRPCNIO | ||
|
||
// Common flags and options | ||
func addressOption(_ address: String) -> Option<String> { | ||
return Option("address", default: address, description: "address of server") | ||
} | ||
|
||
let portOption = Option("port", | ||
default: "8080", | ||
description: "port of server") | ||
|
||
Group { | ||
$0.command("serve", | ||
addressOption("0.0.0.0"), | ||
portOption, | ||
description: "Run an echo server.") { address, port in | ||
let sem = DispatchSemaphore(value: 0) | ||
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
|
||
print("starting insecure server") | ||
_ = try! GRPCServer.start(hostname: address, | ||
port: Int(port)!, | ||
eventLoopGroup: eventLoopGroup, | ||
serviceProviders: [EchoProviderNIO()]) | ||
.wait() | ||
|
||
// This blocks to keep the main thread from finishing while the server runs, | ||
// but the server never exits. Kill the process to stop it. | ||
_ = sem.wait() | ||
} | ||
|
||
}.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.