Expand description
§hyper_echo
hyper_echo
provides an async echo server based on tokio, tower, hyper and fastwebsockets.
§Features
- Async and efficient
- Always runs on ip
127.0.0.1
- Configurable port
- Supports both HTTP (versions 1 and 2) and WebSocket
- Configurable HTTP log level. Could log uri, headers and body of a request.
- Two implementations of http logging: a custom one and one based on Trace from tower_http
- Logging of WebSocket messages (if enabled)
- Configurable ping interval for WebSocket connections and automatic disconnection of inactive clients
- Supports graceful shutdown by cancellation token
§Example
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let echo_server = hyper_echo::EchoServer::new(None, hyper_echo::HttpLogLevel::None, false).await?;
println!("Starting echo server on {}", echo_server.local_addr());
let cancellation_token = tokio_util::sync::CancellationToken::new();
tokio::spawn({
let cancellation_token = cancellation_token.clone();
async move {
let _guard = cancellation_token.drop_guard();
let _ = tokio::signal::ctrl_c().await;
}
});
echo_server.run(cancellation_token).await.map_err(Into::into)
}
§HTTP logging implementation
There are two crate’s features controlling HTTP logging:
tower_trace
(default) is based on Trace from tower_http cratecustom_trace
written from scratch logging layer for tower service
Both implementations are almost identical from a user perspective.
There is no real reason to use logging implementation provided by custom_trace
feature.
It was created to learn how to create a custom tower layer and how to handle multiple features in one crate.
But if in some case you want to use it, please don’t forget to add default-features = false
if you are using custom_trace
because
it is possible to use only one logging implementation at a time.
Structs§
- Echo
Server - Asynchronous echo server supporting HTTP and WebSocket
Enums§
- Http
LogLevel - Level of logging requests and responses