1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
mod grpc_server {
#![allow(unused_qualifications, missing_docs)]
tonic::include_proto!("grpc");
}
pub use grpc_server::rpc_service_server::{RpcService, RpcServiceServer};
pub use grpc_server::{
pricing_request::ServiceType, PricingRequest, PricingRequests, PricingResponse, ReadyRequest,
ReadyResponse,
};
use crate::shutdown_signal;
use crate::Config;
use std::fmt::Debug;
use std::net::SocketAddr;
use tonic::transport::Server;
use tonic::{Request, Response, Status};
use crate::engine::*;
#[derive(Debug, Default, Copy, Clone)]
pub struct ServerImpl {}
#[cfg(not(feature = "stub_server"))]
#[tonic::async_trait]
impl RpcService for ServerImpl {
async fn get_pricing(
&self,
request: Request<PricingRequests>,
) -> Result<Response<PricingResponse>, Status> {
grpc_info!("(get_pricing) pricing server.");
grpc_debug!("(get_pricing) request: {:?}", request);
let query = request.into_inner();
let prices = get_pricing(query);
match prices {
Ok(prices) => {
let response = PricingResponse { prices };
Ok(tonic::Response::new(response))
}
Err(e) => {
let error = tonic::Status::internal(format!("Error getting pricing: {}", e));
Err(error)
}
}
}
async fn is_ready(
&self,
request: Request<ReadyRequest>,
) -> Result<Response<ReadyResponse>, Status> {
grpc_info!("(is_ready) pricing server.");
grpc_debug!("(is_ready) request: {:?}", request);
let response = ReadyResponse { ready: true };
Ok(Response::new(response))
}
}
#[cfg(not(tarpaulin_include))]
pub async fn grpc_server(config: Config, shutdown_rx: Option<tokio::sync::oneshot::Receiver<()>>) {
grpc_debug!("(grpc_server) entry.");
let grpc_port = config.docker_port_grpc;
let full_grpc_addr: SocketAddr = match format!("[::]:{}", grpc_port).parse() {
Ok(addr) => addr,
Err(e) => {
grpc_error!("(grpc_server) Failed to parse gRPC address: {}", e);
return;
}
};
let imp = ServerImpl::default();
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter
.set_serving::<RpcServiceServer<ServerImpl>>()
.await;
grpc_info!(
"(grpc_server) Starting gRPC services on: {}.",
full_grpc_addr
);
match Server::builder()
.add_service(health_service)
.add_service(RpcServiceServer::new(imp))
.serve_with_shutdown(full_grpc_addr, shutdown_signal("grpc", shutdown_rx))
.await
{
Ok(_) => grpc_info!("(grpc_server) gRPC server running at: {}.", full_grpc_addr),
Err(e) => {
grpc_error!("(grpc_server) Could not start gRPC server: {}", e);
}
};
}
#[cfg(feature = "stub_server")]
#[tonic::async_trait]
impl RpcService for ServerImpl {
async fn get_pricing(
&self,
request: Request<PricingRequests>,
) -> Result<Response<PricingResponse>, Status> {
grpc_info!("(get_pricing MOCK) pricing server.");
grpc_debug!("(get_pricing MOCK) request: {:?}", request);
let query = request.into_inner();
let prices = get_pricing(query);
match prices {
Ok(prices) => {
let response = PricingResponse { prices };
Ok(Response::new(response))
}
Err(e) => {
let error = Status::internal(format!("Error getting pricing: {}", e));
Err(error)
}
}
}
async fn is_ready(
&self,
request: Request<ReadyRequest>,
) -> Result<Response<ReadyResponse>, Status> {
grpc_warn!("(is_ready MOCK) pricing server.");
grpc_debug!("(is_ready MOCK) request: {:?}", request);
let response = ReadyResponse { ready: true };
Ok(Response::new(response))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_grpc_server_is_ready() {
let imp = ServerImpl::default();
let result = imp.is_ready(Request::new(ReadyRequest {})).await;
assert!(result.is_ok());
let result: ReadyResponse = result.unwrap().into_inner();
assert_eq!(result.ready, true);
}
}