chore(example): Autoreloading server example (#316)
* Create an example of an autoreloading server * Document the autoreloading server example * Format autoreloading server example * Add note about cargo-watch in the examples readme
This commit is contained in:
@@ -106,6 +106,10 @@ path = "src/hyper_warp/server.rs"
|
|||||||
name = "health-server"
|
name = "health-server"
|
||||||
path = "src/health/server.rs"
|
path = "src/health/server.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "autoreload-server"
|
||||||
|
path = "src/autoreload/server.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tonic = { path = "../tonic", features = ["tls"] }
|
tonic = { path = "../tonic", features = ["tls"] }
|
||||||
prost = "0.6"
|
prost = "0.6"
|
||||||
@@ -132,6 +136,7 @@ http-body = "0.3"
|
|||||||
pin-project = "0.4"
|
pin-project = "0.4"
|
||||||
# Health example
|
# Health example
|
||||||
tonic-health = { path = "../tonic-health" }
|
tonic-health = { path = "../tonic-health" }
|
||||||
|
listenfd = "0.3"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
tonic-build = { path = "../tonic-build", features = ["prost"] }
|
tonic-build = { path = "../tonic-build", features = ["prost"] }
|
||||||
|
|||||||
@@ -80,6 +80,13 @@ $ cargo run --bin tls-server
|
|||||||
$ cargo run --bin health-server
|
$ cargo run --bin health-server
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Autoreloading Server
|
||||||
|
|
||||||
|
### Server
|
||||||
|
```bash
|
||||||
|
systemfd --no-pid -s http::[::1]:50051 -- cargo watch -x 'run --bin autoreload-server'
|
||||||
|
```
|
||||||
|
|
||||||
### Notes:
|
### Notes:
|
||||||
|
|
||||||
If you are using the `codegen` feature, then the following dependencies are
|
If you are using the `codegen` feature, then the following dependencies are
|
||||||
@@ -89,3 +96,7 @@ If you are using the `codegen` feature, then the following dependencies are
|
|||||||
* [prost](https://crates.io/crates/prost)
|
* [prost](https://crates.io/crates/prost)
|
||||||
* [prost-derive](https://crates.io/crates/prost-derive)
|
* [prost-derive](https://crates.io/crates/prost-derive)
|
||||||
|
|
||||||
|
The autoload example requires the following crates installed globally:
|
||||||
|
|
||||||
|
* [systemfd](https://crates.io/crates/systemfd)
|
||||||
|
* [cargo-watch](https://crates.io/crates/cargo-watch)
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
use tonic::{transport::Server, Request, Response, Status};
|
||||||
|
|
||||||
|
use hello_world::greeter_server::{Greeter, GreeterServer};
|
||||||
|
use hello_world::{HelloReply, HelloRequest};
|
||||||
|
|
||||||
|
pub mod hello_world {
|
||||||
|
tonic::include_proto!("helloworld");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct MyGreeter {}
|
||||||
|
|
||||||
|
#[tonic::async_trait]
|
||||||
|
impl Greeter for MyGreeter {
|
||||||
|
async fn say_hello(
|
||||||
|
&self,
|
||||||
|
request: Request<HelloRequest>,
|
||||||
|
) -> Result<Response<HelloReply>, Status> {
|
||||||
|
println!("Got a request from {:?}", request.remote_addr());
|
||||||
|
|
||||||
|
let reply = hello_world::HelloReply {
|
||||||
|
message: format!("Hello {}!", request.into_inner().name),
|
||||||
|
};
|
||||||
|
Ok(Response::new(reply))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let addr = "[::1]:50051".parse().unwrap();
|
||||||
|
let greeter = MyGreeter::default();
|
||||||
|
|
||||||
|
println!("GreeterServer listening on {}", addr);
|
||||||
|
|
||||||
|
let server = Server::builder().add_service(GreeterServer::new(greeter));
|
||||||
|
|
||||||
|
match listenfd::ListenFd::from_env().take_tcp_listener(0)? {
|
||||||
|
Some(listener) => {
|
||||||
|
let mut listener = tokio::net::TcpListener::from_std(listener)?;
|
||||||
|
|
||||||
|
server.serve_with_incoming(listener.incoming()).await?;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
server.serve(addr).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user