Skip to content

Commit

Permalink
Remove code wrapping ServeDir
Browse files Browse the repository at this point in the history
Made possible by tower-rs/tower-http#283
  • Loading branch information
leotaku committed Feb 25, 2023
1 parent 56d6d41 commit 7261c49
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
13 changes: 2 additions & 11 deletions examples/axum-in-process/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
use axum::{http, routing::get_service, Router};
use axum::Router;
use notify::Watcher;
use std::path::Path;
use tower_http::services::ServeDir;
use tower_livereload::LiveReloadLayer;

fn serve_dir(path: &Path) -> axum::routing::MethodRouter {
get_service(ServeDir::new(path)).handle_error(|error| async move {
(
http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
)
})
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let livereload = LiveReloadLayer::new();
let reloader = livereload.reloader();
let app = Router::new()
.nest_service("/", serve_dir(Path::new("assets")))
.nest_service("/", ServeDir::new(Path::new("assets")))
.layer(livereload);

let mut watcher = notify::recommended_watcher(move |_| reloader.reload())?;
Expand Down
13 changes: 2 additions & 11 deletions examples/livehttpd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use axum::{http, routing::get_service, Router};
use axum::{http, Router};
use clap::Parser;
use notify::Watcher;
use tower::layer::util::Stack;
Expand All @@ -22,15 +22,6 @@ struct Command {
directory: std::path::PathBuf,
}

fn serve_dir(path: &std::path::Path) -> axum::routing::MethodRouter {
get_service(ServeDir::new(path)).handle_error(|error| async move {
(
http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
)
})
}

type Srhl = SetResponseHeaderLayer<http::HeaderValue>;

fn no_cache_layer() -> Stack<Srhl, Stack<Srhl, Srhl>> {
Expand Down Expand Up @@ -65,7 +56,7 @@ async fn try_main() -> Result<(), Box<dyn std::error::Error>> {
let livereload = LiveReloadLayer::new();
let reloader = livereload.reloader();
let app = Router::new()
.nest_service("/", serve_dir(&args.directory))
.nest_service("/", ServeDir::new(&args.directory))
.layer(livereload)
.layer(no_cache_layer());

Expand Down

0 comments on commit 7261c49

Please sign in to comment.