Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 88 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"
members = [
"examples/grpc",
"examples/grpc-streaming",
"examples/hello-world",
"examples/http-axum-router",
"examples/http-concurrent-outbound-calls",
Expand Down
19 changes: 19 additions & 0 deletions examples/grpc-streaming/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "grpc-streaming"
version = "0.1.0"
edition = "2024"

[lib]
crate-type = ["cdylib"]

[dependencies]
anyhow = "1"
async-stream = "0.3"
futures = "0.3"
prost = "0.13"
spin-sdk = { path = "../../crates/spin-sdk", features = ["grpc"] }
tokio-stream = "0.1"
tonic = { version = "0.12", default-features = false, features = ["codegen", "prost", "router"] }

[build-dependencies]
tonic-build = { version = "0.12", default-features = false, features = ["prost"] }
48 changes: 48 additions & 0 deletions examples/grpc-streaming/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# gRPC Streaming

A Spin HTTP component that serves gRPC endpoints using [tonic](https://github.com/hyperium/tonic),
demonstrating all combinations of unary and streaming requests and responses.

## Prerequisites

- [protoc](https://grpc.io/docs/protoc-installation/) (Protocol Buffers compiler)
- `wasm32-wasip2` target: `rustup target add wasm32-wasip2`

## Build and Run

```sh
spin up --build --sqlite @db.sql
```

## Test with grpcurl

> **Note:** These commands pass `-import-path` and `-proto` because the server
> does not implement gRPC server reflection.

Unary call:

```sh
grpcurl -plaintext -import-path proto -proto route_guide.proto -d '{"latitude":18,"longitude":19}' localhost:3000 routeguide.RouteGuide/GetFeature
```

Server-streaming call:

```sh
grpcurl -plaintext -import-path proto -proto route_guide.proto -d '{"lo":{"latitude":12,"longitude":10},"hi":{"latitude":28,"longitude":25}}' localhost:3000 routeguide.RouteGuide/ListFeatures
```

Client-streaming call:

```sh
grpcurl -plaintext -import-path proto -proto route_guide.proto -d '{"latitude":18,"longitude":19}{"latitude":12,"longitude":20}{"latitude":13,"longitude":17}{"latitude":14,"longitude":18}' localhost:3000 routeguide.RouteGuide/RecordRoute
```

Client- and server-streaming:

```sh
grpcurl -plaintext -import-path proto -proto route_guide.proto -d '{"location":{"latitude":18,"longitude":19},"message":"hello from fang rock!"}{"location":{"latitude":12,"longitude":20},"message":"i summited mt hobbes!"}' localhost:3000 routeguide.RouteGuide/RouteChat
grpcurl -plaintext -import-path proto -proto route_guide.proto -d '{"location":{"latitude":18,"longitude":19},"message":"farewell from fang rock!"}' localhost:3000 routeguide.RouteGuide/RouteChat
grpcurl -plaintext -import-path proto -proto route_guide.proto -d '{"location":{"latitude":12,"longitude":20},"message":"i was impaled on sharp claws at mt hobbes!"}{"location":{"latitude":18,"longitude":19},"message":"i got nibbled by rutans at fang rock!"}' localhost:3000 routeguide.RouteGuide/RouteChat
```

(Multiple commands because the routeguide scenario needs you to build up some history at a location to see interesting responses.)
7 changes: 7 additions & 0 deletions examples/grpc-streaming/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
tonic_build::configure()
.type_attribute("routeguide.Point", "#[derive(Hash)]")
.build_transport(false)
.compile_protos(&["proto/route_guide.proto"], &[""])
.unwrap();
}
21 changes: 21 additions & 0 deletions examples/grpc-streaming/db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE IF NOT EXISTS features (
lat NUMBER,
long NUMBER,
name TEXT
);

DELETE FROM features;
INSERT INTO features(lat, long, name) VALUES (12, 20, 'Mount Hobbes');
INSERT INTO features(lat, long, name) VALUES (30, 8, 'Upper Rosie');
INSERT INTO features(lat, long, name) VALUES (14, 18, 'Slats'' Food Crater');
INSERT INTO features(lat, long, name) VALUES (25, 30, 'Forest of Smoke');
INSERT INTO features(lat, long, name) VALUES (22, 7, 'The Great Splodge');
INSERT INTO features(lat, long, name) VALUES (35, 21, 'Kiki Point');
INSERT INTO features(lat, long, name) VALUES (18, 19, 'Fang Rock');

CREATE TABLE IF NOT EXISTS route_notes(
seq_no INTEGER PRIMARY KEY AUTOINCREMENT,
lat NUMBER,
long NUMBER,
msg_text TEXT
);
110 changes: 110 additions & 0 deletions examples/grpc-streaming/proto/route_guide.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2015 gRPC authors.
//
// 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.

syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.routeguide";
option java_outer_classname = "RouteGuideProto";

package routeguide;

// Interface exported by the server.
service RouteGuide {
// A simple RPC.
//
// Obtains the feature at a given position.
//
// A feature with an empty name is returned if there's no feature at the given
// position.
rpc GetFeature(Point) returns (Feature) {}

// A server-to-client streaming RPC.
//
// Obtains the Features available within the given Rectangle. Results are
// streamed rather than returned at once (e.g. in a response message with a
// repeated field), as the rectangle may cover a large area and contain a
// huge number of features.
rpc ListFeatures(Rectangle) returns (stream Feature) {}

// A client-to-server streaming RPC.
//
// Accepts a stream of Points on a route being traversed, returning a
// RouteSummary when traversal is completed.
rpc RecordRoute(stream Point) returns (RouteSummary) {}

// A Bidirectional streaming RPC.
//
// Accepts a stream of RouteNotes sent while a route is being traversed,
// while receiving other RouteNotes (e.g. from other users).
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
}

// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest integer).
// Latitudes should be in the range +/- 90 degrees and longitude should be in
// the range +/- 180 degrees (inclusive).
message Point {
int32 latitude = 1;
int32 longitude = 2;
}

// A latitude-longitude rectangle, represented as two diagonally opposite
// points "lo" and "hi".
message Rectangle {
// One corner of the rectangle.
Point lo = 1;

// The other corner of the rectangle.
Point hi = 2;
}

// A feature names something at a given point.
//
// If a feature could not be named, the name is empty.
message Feature {
// The name of the feature.
string name = 1;

// The point where the feature is detected.
Point location = 2;
}

// A RouteNote is a message sent while at a given point.
message RouteNote {
// The location from which the message is sent.
Point location = 1;

// The message to be sent.
string message = 2;
}

// A RouteSummary is received in response to a RecordRoute rpc.
//
// It contains the number of individual points received, the number of
// detected features, and the total distance covered as the cumulative sum of
// the distance between each point.
message RouteSummary {
// The number of points received.
int32 point_count = 1;

// The number of known features passed while traversing the route.
int32 feature_count = 2;

// The distance covered in metres.
int32 distance = 3;

// The duration of the traversal in seconds.
int32 elapsed_time = 4;
}
Loading
Loading