axum/examples/static_file_server.rs

34 lines
893 B
Rust
Raw Normal View History

//! Run with
//!
//! ```not_rust
//! cargo run --example static_file_server
//! ```
2021-07-22 21:12:29 +02:00
use axum::{prelude::*, routing::nest, service::ServiceExt};
2021-06-06 21:53:22 +02:00
use http::StatusCode;
use std::net::SocketAddr;
use tower_http::{services::ServeDir, trace::TraceLayer};
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
2021-07-22 21:12:29 +02:00
let app = nest(
2021-06-06 21:53:22 +02:00
"/static",
2021-07-09 21:36:14 +02:00
axum::service::get(ServeDir::new(".").handle_error(|error: std::io::Error| {
2021-07-22 15:00:33 +02:00
Ok::<_, std::convert::Infallible>((
2021-06-06 21:53:22 +02:00
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled interal error: {}", error),
2021-07-22 15:00:33 +02:00
))
2021-06-07 15:45:19 +02:00
})),
2021-06-06 21:53:22 +02:00
)
.layer(TraceLayer::new_for_http());
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
2021-06-19 12:50:33 +02:00
hyper::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
2021-06-06 21:53:22 +02:00
}