mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-14 00:00:15 +02:00
66 lines
2.0 KiB
Rust
66 lines
2.0 KiB
Rust
//! Run with
|
|
//!
|
|
//! ```not_rust
|
|
//! cargo run -p example-sse
|
|
//! ```
|
|
|
|
use axum::{
|
|
response::sse::{Event, Sse},
|
|
routing::get,
|
|
Router,
|
|
};
|
|
use axum_extra::{headers, TypedHeader};
|
|
use futures::stream::{self, Stream};
|
|
use std::{convert::Infallible, path::PathBuf, time::Duration};
|
|
use tokio_stream::StreamExt as _;
|
|
use tower_http::{services::ServeDir, trace::TraceLayer};
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
tracing_subscriber::registry()
|
|
.with(
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
|
.unwrap_or_else(|_| "example_sse=debug,tower_http=debug".into()),
|
|
)
|
|
.with(tracing_subscriber::fmt::layer())
|
|
.init();
|
|
|
|
let assets_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets");
|
|
|
|
let static_files_service = ServeDir::new(assets_dir).append_index_html_on_directories(true);
|
|
|
|
// build our application with a route
|
|
let app = Router::new()
|
|
.fallback_service(static_files_service)
|
|
.route("/sse", get(sse_handler))
|
|
.layer(TraceLayer::new_for_http());
|
|
|
|
// run it
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
|
.await
|
|
.unwrap();
|
|
tracing::debug!("listening on {}", listener.local_addr().unwrap());
|
|
axum::serve(listener, app).await.unwrap();
|
|
}
|
|
|
|
async fn sse_handler(
|
|
TypedHeader(user_agent): TypedHeader<headers::UserAgent>,
|
|
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
|
|
println!("`{}` connected", user_agent.as_str());
|
|
|
|
// A `Stream` that repeats an event every second
|
|
//
|
|
// You can also create streams from tokio channels using the wrappers in
|
|
// https://docs.rs/tokio-stream
|
|
let stream = stream::repeat_with(|| Event::default().data("hi!"))
|
|
.map(Ok)
|
|
.throttle(Duration::from_secs(1));
|
|
|
|
Sse::new(stream).keep_alive(
|
|
axum::response::sse::KeepAlive::new()
|
|
.interval(Duration::from_secs(1))
|
|
.text("keep-alive-text"),
|
|
)
|
|
}
|