Skip to content

Commit b1cc2f3

Browse files
author
Patrick Koss
committed
working server with grpc + http
1 parent c2b9a1d commit b1cc2f3

File tree

7 files changed

+174
-158
lines changed

7 files changed

+174
-158
lines changed

server/api/server.openapi.yaml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ paths:
112112
in: path
113113
required: true
114114
schema:
115-
$ref: '#/components/schemas/GoogleProtobufAny'
115+
$ref: '#/components/schemas/GoogleProtobufValue'
116116
responses:
117117
"200":
118118
description: OK
@@ -163,14 +163,6 @@ components:
163163
properties:
164164
keyValue:
165165
$ref: '#/components/schemas/KeyValue'
166-
GoogleProtobufAny:
167-
type: object
168-
properties:
169-
'@type':
170-
type: string
171-
description: The type of the serialized message.
172-
additionalProperties: true
173-
description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message.
174166
GoogleProtobufValue:
175167
description: Represents a dynamically typed value which can be either null, a number, a string, a boolean, a recursive struct value, or a list of values.
176168
KeyValue:
@@ -180,9 +172,9 @@ components:
180172
type: object
181173
properties:
182174
key:
183-
$ref: '#/components/schemas/GoogleProtobufAny'
175+
$ref: '#/components/schemas/GoogleProtobufValue'
184176
value:
185-
$ref: '#/components/schemas/GoogleProtobufAny'
177+
$ref: '#/components/schemas/GoogleProtobufValue'
186178
UpdateResponse:
187179
required:
188180
- keyValue

server/envoy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static_resources:
2222
routes:
2323
# NOTE: by default, matching happens based on the gRPC route, and not on the incoming request path.
2424
# Reference: https://envoyproxy.io/docs/envoy/latest/configuration/http/http_filters/grpc_json_transcoder_filter#route-configs-for-transcoded-requests
25-
- match: {prefix: "/server-envoy.KeyValueService"}
25+
- match: {prefix: "/server.KeyValueService"}
2626
route: {cluster: grpc, timeout: 60s}
2727
http_filters:
2828
- name: envoy.filters.http.grpc_json_transcoder

server/proto/server-envoy.pb

8 Bytes
Binary file not shown.

server/proto/server.pb.go

Lines changed: 130 additions & 131 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/proto/server.proto

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ option (gnostic.openapi.v3.document) = {
6060
};
6161

6262
message KeyValue {
63-
google.protobuf.Any key = 1 [
63+
google.protobuf.Value key = 1 [
6464
(google.api.field_behavior) = REQUIRED,
6565
(validate.rules).any = {
6666
required: true,
6767
}
6868
];
69-
google.protobuf.Any value = 2 [
69+
google.protobuf.Value value = 2 [
7070
(google.api.field_behavior) = REQUIRED,
7171
(validate.rules).any = {
7272
required: true,
@@ -87,7 +87,7 @@ message CreateRequest {
8787
}
8888

8989
message DeleteRequest {
90-
google.protobuf.Any key = 1 [
90+
google.protobuf.Value key = 1 [
9191
(google.api.field_behavior) = REQUIRED
9292
];
9393
}

server/src/client.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2525
println!("RESPONSE={:?}", response);
2626

2727
let key_value = response.into_inner().key_value.unwrap();
28-
println!("KEY={:?}", String::from_utf8_lossy(&key_value.key.unwrap().value));
29-
println!("VALUE={:?}", String::from_utf8_lossy(&key_value.value.unwrap().value));
28+
29+
let key = match key_value.key.unwrap().kind {
30+
Some(Kind::StringValue(s)) => s,
31+
_ => String::from(""),
32+
};
33+
println!("VALUE={:?}", &key);
34+
35+
let value = match key_value.value.unwrap().kind {
36+
Some(Kind::StringValue(s)) => s,
37+
_ => String::from(""),
38+
};
39+
println!("VALUE={:?}", &value);
3040

3141
Ok(())
3242
}

server/src/server.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use tonic::{Request, Response, Status, transport::Server};
22

33
use key_value_store::{DeleteRequest, DeleteResponse, GetRequest, GetResponse, KeyValue, CreateRequest, CreateResponse, UpdateRequest, UpdateResponse};
44
use key_value_store::key_value_service_server::{KeyValueService, KeyValueServiceServer};
5-
use prost_types::Any;
5+
use prost_types::Value;
66
use std::string::String;
7+
use prost_types::value::Kind;
78

89
pub mod key_value_store {
910
tonic::include_proto!("server");
@@ -16,28 +17,42 @@ pub struct KeyValueStoreImpl {}
1617
impl KeyValueService for KeyValueStoreImpl {
1718
async fn get(&self, request: Request<GetRequest>) -> Result<Response<GetResponse>, Status> {
1819
println!("Got a request: {:?}", request);
19-
let key = Any {
20-
type_url: "type_url".to_string(),
21-
value: String::from("key").into_bytes(),
20+
let key = Value {
21+
kind: Some(Kind::StringValue("key".to_string())),
2222
};
2323

24-
let value = Any {
25-
type_url: "type_url".to_string(),
26-
value: String::from("value").into_bytes(),
24+
let value = Value {
25+
kind: Some(Kind::StringValue("value".to_string())),
2726
};
2827

2928
let reply = GetResponse {
3029
key_value: Option::from(KeyValue {
31-
key: Some(key),
32-
value: Some(value),
30+
key: key.into(),
31+
value: value.into(),
3332
})
3433
};
3534

3635
Ok(Response::new(reply))
3736
}
3837

3938
async fn create(&self, _request: Request<CreateRequest>) -> Result<Response<CreateResponse>, Status> {
40-
todo!()
39+
println!("Got a request: {:?}", _request);
40+
let key = Value {
41+
kind: Some(Kind::StringValue("key".to_string())),
42+
};
43+
44+
let value = Value {
45+
kind: Some(Kind::StringValue("value".to_string())),
46+
};
47+
48+
let reply = CreateResponse {
49+
key_value: Option::from(KeyValue {
50+
key: key.into(),
51+
value: value.into(),
52+
})
53+
};
54+
55+
Ok(Response::new(reply))
4156
}
4257

4358
async fn update(&self, _request: Request<UpdateRequest>) -> Result<Response<UpdateResponse>, Status> {

0 commit comments

Comments
 (0)