Skip to content

Commit

Permalink
More robust asset paths in examples (#1090)
Browse files Browse the repository at this point in the history
* More robust asset paths in examples

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

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>

* format

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
  • Loading branch information
davidpdrsn and jplatte committed Jun 15, 2022
1 parent cbb2e8a commit fd70f81
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
19 changes: 15 additions & 4 deletions examples/low-level-rustls/src/main.rs
Expand Up @@ -11,7 +11,14 @@ use hyper::server::{
conn::{AddrIncoming, Http},
};
use rustls_pemfile::{certs, pkcs8_private_keys};
use std::{fs::File, io::BufReader, net::SocketAddr, pin::Pin, sync::Arc};
use std::{
fs::File,
io::BufReader,
net::SocketAddr,
path::{Path, PathBuf},
pin::Pin,
sync::Arc,
};
use tokio::net::TcpListener;
use tokio_rustls::{
rustls::{Certificate, PrivateKey, ServerConfig},
Expand All @@ -30,8 +37,12 @@ async fn main() {
.init();

let rustls_config = rustls_server_config(
"examples/tls-rustls/self_signed_certs/key.pem",
"examples/tls-rustls/self_signed_certs/cert.pem",
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("self_signed_certs")
.join("key.pem"),
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("self_signed_certs")
.join("cert.pem"),
);

let acceptor = TlsAcceptor::from(rustls_config);
Expand Down Expand Up @@ -65,7 +76,7 @@ async fn handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> String {
addr.to_string()
}

fn rustls_server_config(key: &str, cert: &str) -> Arc<ServerConfig> {
fn rustls_server_config(key: impl AsRef<Path>, cert: impl AsRef<Path>) -> Arc<ServerConfig> {
let mut key_reader = BufReader::new(File::open(key).unwrap());
let mut cert_reader = BufReader::new(File::open(cert).unwrap());

Expand Down
23 changes: 13 additions & 10 deletions examples/sse/src/main.rs
Expand Up @@ -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};
Expand All @@ -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()
Expand All @@ -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()
Expand Down
10 changes: 7 additions & 3 deletions examples/tls-rustls/src/main.rs
Expand Up @@ -6,7 +6,7 @@

use axum::{routing::get, Router};
use axum_server::tls_rustls::RustlsConfig;
use std::net::SocketAddr;
use std::{net::SocketAddr, path::PathBuf};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

#[tokio::main]
Expand All @@ -19,8 +19,12 @@ async fn main() {
.init();

let config = RustlsConfig::from_pem_file(
"examples/tls-rustls/self_signed_certs/cert.pem",
"examples/tls-rustls/self_signed_certs/key.pem",
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("self_signed_certs")
.join("cert.pem"),
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("self_signed_certs")
.join("key.pem"),
)
.await
.unwrap();
Expand Down
6 changes: 4 additions & 2 deletions examples/websockets/src/main.rs
Expand Up @@ -16,7 +16,7 @@ use axum::{
routing::{get, get_service},
Router,
};
use std::net::SocketAddr;
use std::{net::SocketAddr, path::PathBuf};
use tower_http::{
services::ServeDir,
trace::{DefaultMakeSpan, TraceLayer},
Expand All @@ -32,7 +32,9 @@ async fn main() {
))
.with(tracing_subscriber::fmt::layer())
.init();
let assets_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets");

let assets_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets");

// build our application with some routes
let app = Router::new()
.fallback(
Expand Down

0 comments on commit fd70f81

Please sign in to comment.