set up gprc server

This commit is contained in:
Ilion Beyst 2022-05-31 21:08:56 +02:00
parent ef19e3a9e7
commit af5cd69f7b
5 changed files with 60 additions and 0 deletions

View file

@ -26,9 +26,14 @@ toml = "0.5"
planetwars-matchrunner = { path = "../planetwars-matchrunner" } planetwars-matchrunner = { path = "../planetwars-matchrunner" }
config = { version = "0.12", features = ["toml"] } config = { version = "0.12", features = ["toml"] }
thiserror = "1.0.31" thiserror = "1.0.31"
prost = "0.10"
tonic = "0.7.2"
# TODO: remove me # TODO: remove me
shlex = "1.1" shlex = "1.1"
[build-dependencies]
tonic-build = "0.7.2"
[dev-dependencies] [dev-dependencies]
parking_lot = "0.11" parking_lot = "0.11"

View file

@ -0,0 +1,9 @@
extern crate tonic_build;
fn main() -> Result<(), Box<dyn std::error::Error>> {
tonic_build::configure()
.build_server(true)
.build_client(false)
.compile(&["../proto/bot_api.proto"], &["../proto"])?;
Ok(())
}

View file

@ -0,0 +1,30 @@
pub mod pb {
tonic::include_proto!("grpc.planetwars.bot_api");
}
use std::net::SocketAddr;
use tonic;
use tonic::transport::Server;
use tonic::{Request, Response, Status};
pub struct BotApiServer {}
#[tonic::async_trait]
impl pb::test_service_server::TestService for BotApiServer {
async fn greet(&self, req: Request<pb::Hello>) -> Result<Response<pb::HelloResponse>, Status> {
Ok(Response::new(pb::HelloResponse {
response: format!("hallo {}", req.get_ref().hello_message),
}))
}
}
pub async fn run_bot_api() {
let server = BotApiServer {};
let addr = SocketAddr::from(([127, 0, 0, 1], 50051));
Server::builder()
.add_service(pb::test_service_server::TestServiceServer::new(server))
.serve(addr)
.await
.unwrap()
}

View file

@ -1,5 +1,6 @@
// This module implements general domain logic, not directly // This module implements general domain logic, not directly
// tied to the database or API layers. // tied to the database or API layers.
pub mod bot_api;
pub mod bots; pub mod bots;
pub mod matches; pub mod matches;
pub mod ranking; pub mod ranking;

15
proto/bot_api.proto Normal file
View file

@ -0,0 +1,15 @@
syntax = "proto3";
package grpc.planetwars.bot_api;
message Hello {
string hello_message = 1;
}
message HelloResponse {
string response = 1;
}
service TestService {
rpc greet(Hello) returns (HelloResponse);
}