More robust asset paths in examples (#1090)

* More robust asset paths in examples

* Update examples/low-level-rustls/src/main.rs

Co-authored-by: Jonas Platte <[email protected]>

* format

Co-authored-by: Jonas Platte <[email protected]>
This commit is contained in:
David Pedersen
2022-06-15 22:42:49 +02:00
committed by GitHub
co-authored by Jonas Platte
parent cbb2e8a244
commit fd70f81c46
4 changed files with 39 additions and 19 deletions
+13 -10
View File
@@ -12,7 +12,7 @@ use axum::{
Router,
};
use futures::stream::{self, Stream};
use std::{convert::Infallible, net::SocketAddr, time::Duration};
use std::{convert::Infallible, net::SocketAddr, path::PathBuf, time::Duration};
use tokio_stream::StreamExt as _;
use tower_http::{services::ServeDir, trace::TraceLayer};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
@@ -27,14 +27,17 @@ async fn main() {
.with(tracing_subscriber::fmt::layer())
.init();
let static_files_service =
get_service(ServeDir::new("examples/sse/assets").append_index_html_on_directories(true))
.handle_error(|error: std::io::Error| async move {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
)
});
let assets_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets");
let static_files_service = get_service(
ServeDir::new(assets_dir).append_index_html_on_directories(true),
)
.handle_error(|error: std::io::Error| async move {
(
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
)
});
// build our application with a route
let app = Router::new()
@@ -59,7 +62,7 @@ async fn sse_handler(
// A `Stream` that repeats an event every second
let stream = stream::repeat_with(|| Event::default().data("hi!"))
.map(Ok)
.throttle(Duration::from_secs(10));
.throttle(Duration::from_secs(1));
Sse::new(stream).keep_alive(
axum::response::sse::KeepAlive::new()