2021-08-02 23:09:09 +02:00
|
|
|
//! Run with
|
|
|
|
//!
|
|
|
|
//! ```not_rust
|
|
|
|
//! cargo run --example sse --features=headers
|
|
|
|
//! ```
|
|
|
|
|
2021-08-01 21:49:17 +02:00
|
|
|
use axum::{extract::TypedHeader, prelude::*, routing::nest, service::ServiceExt, sse::Event};
|
|
|
|
use futures::stream::{self, Stream};
|
|
|
|
use http::StatusCode;
|
|
|
|
use std::{convert::Infallible, net::SocketAddr, time::Duration};
|
|
|
|
use tokio_stream::StreamExt as _;
|
|
|
|
use tower_http::{services::ServeDir, trace::TraceLayer};
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2021-08-05 11:25:03 +02:00
|
|
|
// Set the RUST_LOG, if it hasn't been explicitly defined
|
|
|
|
if std::env::var("RUST_LOG").is_err() {
|
|
|
|
std::env::set_var("RUST_LOG", "sse=debug,tower_http=debug")
|
|
|
|
}
|
|
|
|
tracing_subscriber::fmt::fmt()
|
|
|
|
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
|
|
|
.init();
|
2021-08-01 21:49:17 +02:00
|
|
|
|
|
|
|
// build our application with a route
|
|
|
|
let app = nest(
|
|
|
|
"/",
|
|
|
|
axum::service::get(
|
|
|
|
ServeDir::new("examples/sse")
|
|
|
|
.append_index_html_on_directories(true)
|
|
|
|
.handle_error(|error: std::io::Error| {
|
|
|
|
Ok::<_, std::convert::Infallible>((
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
2021-08-04 12:09:39 +02:00
|
|
|
format!("Unhandled internal error: {}", error),
|
2021-08-01 21:49:17 +02:00
|
|
|
))
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.route("/sse", axum::sse::sse(make_stream))
|
|
|
|
.layer(TraceLayer::new_for_http());
|
|
|
|
|
|
|
|
// run it
|
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
|
|
|
tracing::debug!("listening on {}", addr);
|
2021-08-04 15:38:51 +02:00
|
|
|
axum::Server::bind(&addr)
|
2021-08-01 21:49:17 +02:00
|
|
|
.serve(app.into_make_service())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn make_stream(
|
|
|
|
// sse handlers can also use extractors
|
|
|
|
TypedHeader(user_agent): TypedHeader<headers::UserAgent>,
|
|
|
|
) -> Result<impl Stream<Item = Result<Event, Infallible>>, Infallible> {
|
|
|
|
println!("`{}` connected", user_agent.as_str());
|
|
|
|
|
|
|
|
// A `Stream` that repeats an event every second
|
|
|
|
let stream = stream::repeat_with(|| Event::default().data("hi!"))
|
|
|
|
.map(Ok)
|
|
|
|
.throttle(Duration::from_secs(1));
|
|
|
|
|
|
|
|
Ok(stream)
|
|
|
|
}
|