Skip to content

Commit

Permalink
Expose API for serving static files from a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Nov 19, 2024
1 parent 60da05e commit 5e33c59
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tokio.workspace = true
tokio-stream.workspace = true
toml.workspace = true
tower.workspace = true
tower-http.workspace = true
tracing.workspace = true
tracing-futures.workspace = true
tracing-subscriber.workspace = true
Expand Down
13 changes: 12 additions & 1 deletion crates/service/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Batteries included server that provides common service APIs
//! over http, with the ability to handle additional consumer
//! defined APIs
use std::io;
use std::{io, path::Path};

use thiserror::Error;
use tokio::net::ToSocketAddrs;
Expand Down Expand Up @@ -65,6 +65,17 @@ impl<'a> Server<'a> {
}
}

/// Serve static files under `route` from the provided `directory`
pub fn serve_directory(self, route: &str, directory: &Path) -> Self {
Self {
router: self.router.route_service(
route,
tower_http::services::ServeDir::new(directory).precompressed_gzip(),
),
..self
}
}

/// Start the server and perform the following:
///
/// - Sync the defined [`Config::admin`] to the service [`Database`] to ensure
Expand Down

0 comments on commit 5e33c59

Please sign in to comment.