Skip to content

Commit

Permalink
serve_static rename
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Apr 21, 2022
1 parent 3b0f149 commit 50f5ee6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 31 deletions.
8 changes: 4 additions & 4 deletions examples/compression/src/main.rs
Expand Up @@ -7,24 +7,24 @@ async fn main() {
tracing_subscriber::fmt().init();

let router = Router::new()
.push(Router::with_path("ws_chat").get(StaticFile::new("examples/ws_chat.rs")))
.push(Router::with_path("ws_chat").get(FileHandler::new("examples/ws_chat.rs")))
.push(
Router::new()
.hoop(compression::deflate())
.path("sse_chat")
.get(StaticFile::new("examples/sse_chat.rs")),
.get(FileHandler::new("examples/sse_chat.rs")),
)
.push(
Router::new()
.hoop(compression::brotli())
.path("todos")
.get(StaticFile::new("examples/todos.rs")),
.get(FileHandler::new("examples/todos.rs")),
)
.push(
Router::new()
.hoop(compression::gzip())
.path("examples/<*path>")
.get(StaticDir::new("examples/")),
.get(DirHandler::new("examples/")),
);
tracing::info!("Listening on http://0.0.0.0:7878");
Server::new(TcpListener::bind("0.0.0.0:7878")).serve(router).await;
Expand Down
6 changes: 3 additions & 3 deletions examples/file-list/src/main.rs
@@ -1,11 +1,11 @@
use salvo::extra::serve_static::{Options, StaticDir};
use salvo::extra::serve_static::{Options, DirHandler};
use salvo::prelude::*;

#[tokio::main]
async fn main() {
tracing_subscriber::fmt().init();

let router = Router::with_path("<**path>").get(StaticDir::width_options(
let router = Router::with_path("<**path>").get(DirHandler::width_options(
vec!["examples/static/boy", "examples/static/girl"],
Options {
dot_files: false,
Expand All @@ -31,7 +31,7 @@ mod tests {
}
#[tokio::test]
async fn test_serve_static_files() {
let router = Router::with_path("<**path>").get(StaticDir::width_options(
let router = Router::with_path("<**path>").get(DirHandler::width_options(
vec!["static/test"],
Options {
dot_files: false,
Expand Down
4 changes: 2 additions & 2 deletions examples/unix-socket/src/main.rs
@@ -1,10 +1,10 @@
#[cfg(target_os = "linux")]
#[tokio::main]
async fn main() {
use salvo::extra::serve_static::StaticDir;
use salvo::extra::serve_static::DirHandler;
use salvo::prelude::*;

let router = Router::with_path("files/<*path>").get(StaticDir::new("./static"));
let router = Router::with_path("files/<*path>").get(DirHandler::new("./static"));
Server::new(UnixListener::bind("/tmp/salvo.sock")).serve(router).await;
}

Expand Down
12 changes: 6 additions & 6 deletions extra/src/compression/mod.rs
Expand Up @@ -184,11 +184,11 @@ impl Handler for CompressionHandler {
/// ```
/// use salvo_core::prelude::*;
/// use salvo_extra::compression;
/// use salvo_extra::serve_static::StaticFile;
/// use salvo_extra::serve_static::FileHandler;
///
/// let router = Router::new()
/// .hoop(compression::gzip())
/// .get(StaticFile::new("./README.md"));
/// .get(FileHandler::new("./README.md"));
/// ```
pub fn gzip() -> CompressionHandler {
CompressionHandler::new(CompressionAlgo::Gzip)
Expand All @@ -202,11 +202,11 @@ pub fn gzip() -> CompressionHandler {
/// ```
/// use salvo_core::prelude::*;
/// use salvo_extra::compression;
/// use salvo_extra::serve_static::StaticFile;
/// use salvo_extra::serve_static::FileHandler;
///
/// let router = Router::new()
/// .hoop(compression::deflate())
/// .get(StaticFile::new("./README.md"));
/// .get(FileHandler::new("./README.md"));
/// ```
pub fn deflate() -> CompressionHandler {
CompressionHandler::new(CompressionAlgo::Deflate)
Expand All @@ -220,11 +220,11 @@ pub fn deflate() -> CompressionHandler {
/// ```
/// use salvo_core::prelude::*;
/// use salvo_extra::compression;
/// use salvo_extra::serve_static::StaticFile;
/// use salvo_extra::serve_static::FileHandler;
///
/// let router = Router::new()
/// .hoop(compression::brotli())
/// .get(StaticFile::new("./README.md"));
/// .get(FileHandler::new("./README.md"));
/// ```
pub fn brotli() -> CompressionHandler {
CompressionHandler::new(CompressionAlgo::Brotli)
Expand Down
16 changes: 8 additions & 8 deletions extra/src/serve_static/dir.rs
Expand Up @@ -89,23 +89,23 @@ impl StaticRoots for Path {
vec![PathBuf::from(self)]
}
}
/// StaticDir
/// DirHandler
#[derive(Clone)]
pub struct StaticDir {
pub struct DirHandler {
roots: Vec<PathBuf>,
options: Options,
chunk_size: Option<u64>,
}
impl StaticDir {
/// Create new `StaticDir`.
impl DirHandler {
/// Create new `DirHandler`.
#[inline]
pub fn new<T: StaticRoots + Sized>(roots: T) -> Self {
StaticDir::width_options(roots, Options::default())
DirHandler::width_options(roots, Options::default())
}
/// Create new `StaticDir` with options.
/// Create new `DirHandler` with options.
#[inline]
pub fn width_options<T: StaticRoots + Sized>(roots: T, options: Options) -> Self {
StaticDir {
DirHandler {
roots: roots.collect(),
options,
chunk_size: None,
Expand Down Expand Up @@ -164,7 +164,7 @@ impl DirInfo {
}

#[async_trait]
impl Handler for StaticDir {
impl Handler for DirHandler {
async fn handle(&self, req: &mut Request, depot: &mut Depot, res: &mut Response, _ctrl: &mut FlowCtrl) {
let param = req.params().iter().find(|(key, _)| key.starts_with('*'));
let req_path = req.uri().path();
Expand Down
12 changes: 6 additions & 6 deletions extra/src/serve_static/fs.rs
Expand Up @@ -7,14 +7,14 @@ use salvo_core::routing::FlowCtrl;
use salvo_core::Handler;
use salvo_core::{Depot, Request, Response, Writer};

/// StaticFile
/// FileHandler
#[derive(Clone)]
pub struct StaticFile(NamedFileBuilder);
pub struct FileHandler(NamedFileBuilder);

impl StaticFile {
/// Create a new `StaticFile`.
impl FileHandler {
/// Create a new `FileHandler`.
pub fn new(path: impl Into<PathBuf>) -> Self {
StaticFile(NamedFile::builder(path))
FileHandler(NamedFile::builder(path))
}

/// During the file chunk read, the maximum read size at one time will affect the
Expand All @@ -26,7 +26,7 @@ impl StaticFile {
}

#[async_trait]
impl Handler for StaticFile {
impl Handler for FileHandler {
async fn handle(&self, req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl) {
match self.0.clone().build().await {
Ok(file) => file.write(req, depot, res).await,
Expand Down
4 changes: 2 additions & 2 deletions extra/src/serve_static/mod.rs
Expand Up @@ -3,5 +3,5 @@
mod dir;
mod fs;

pub use dir::{Options, StaticDir};
pub use fs::StaticFile;
pub use dir::{Options, DirHandler};
pub use fs::FileHandler;

0 comments on commit 50f5ee6

Please sign in to comment.