Added layer to builder. Comments

This commit is contained in:
Martin Berg Alstad 2024-08-19 16:43:31 +02:00
parent 7eb675c210
commit 2c8577a11d
2 changed files with 38 additions and 7 deletions

View File

@ -1,9 +1,15 @@
use std::net::IpAddr;
use { use {
axum::{extract::Request, handler::Handler, Router, ServiceExt}, axum::{
std::{io, net::Ipv4Addr, net::SocketAddr}, extract::Request, handler::Handler, response::IntoResponse, routing::Route, Router,
ServiceExt,
},
std::{
convert::Infallible,
io,
net::{IpAddr, Ipv4Addr, SocketAddr},
},
tokio::net::TcpListener, tokio::net::TcpListener,
tower::layer::Layer, tower::{layer::Layer, Service},
tower_http::{ tower_http::{
cors::CorsLayer, cors::CorsLayer,
normalize_path::NormalizePathLayer, normalize_path::NormalizePathLayer,
@ -48,6 +54,19 @@ impl AppBuilder {
self self
} }
/// Adds a layer to the previously added routes
pub fn layer<L>(mut self, layer: L) -> Self
where
L: Layer<Route> + Clone + Send + 'static,
L::Service: Service<Request> + Clone + Send + 'static,
<L::Service as Service<Request>>::Response: IntoResponse + 'static,
<L::Service as Service<Request>>::Error: Into<Infallible> + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
{
self.router = self.router.layer(layer);
self
}
pub fn socket<IP: Into<IpAddr>>(mut self, socket: impl Into<(IP, u16)>) -> Self { pub fn socket<IP: Into<IpAddr>>(mut self, socket: impl Into<(IP, u16)>) -> Self {
let (ip, port) = socket.into(); let (ip, port) = socket.into();
self.socket = Some((ip.into(), port)); self.socket = Some((ip.into(), port));
@ -87,6 +106,13 @@ impl AppBuilder {
self self
} }
/// Build the app and start the server
/// # Default Options
/// - IP == 0.0.0.0
/// - Port == 8000
/// - Cors == None
/// - Normalize Path == true
/// - Tracing == Default compact
pub async fn serve(self) -> io::Result<()> { pub async fn serve(self) -> io::Result<()> {
let _ = fmt_trace(); // Allowed to fail let _ = fmt_trace(); // Allowed to fail
let listener = self.listener().await?; let listener = self.listener().await?;
@ -148,7 +174,7 @@ mod tests {
let handler = tokio::spawn(async { let handler = tokio::spawn(async {
AppBuilder::new().serve().await.unwrap(); AppBuilder::new().serve().await.unwrap();
}); });
sleep(Duration::from_secs(1)).await; sleep(Duration::from_millis(250)).await;
handler.abort(); handler.abort();
} }
@ -162,11 +188,12 @@ mod tests {
.cors(CorsLayer::new()) .cors(CorsLayer::new())
.normalize_path(true) .normalize_path(true)
.tracing(TraceLayer::new_for_http()) .tracing(TraceLayer::new_for_http())
.layer(TraceLayer::new_for_http())
.serve() .serve()
.await .await
.unwrap(); .unwrap();
}); });
sleep(Duration::from_secs(1)).await; sleep(Duration::from_millis(250)).await;
handler.abort(); handler.abort();
} }
} }

View File

@ -1,5 +1,9 @@
#[cfg(feature = "io")] #[cfg(feature = "io")]
use {crate::io::file, axum::body::Body, axum::response::Html, std::io}; use {
crate::io::file,
axum::{body::Body, response::Html},
std::io,
};
/// Load an HTML file from the given file path, relative to the current directory. /// Load an HTML file from the given file path, relative to the current directory.
/// # Arguments /// # Arguments