pub struct Files { /* private fields */ }
Expand description
Static files handling
Files
service must be registered with App::service()
method.
use std::{
convert::TryInto,
sync::Arc,
};
use actix_web::App;
use espresso::{
files::Files,
config::ServerConfig,
};
use tokio::sync::RwLock;
let mut server_config = ServerConfig::default();
let serve_dir = Arc::new(RwLock::new(Some(".".into())));
let root_path_context = Arc::new((&server_config).try_into().unwrap());
let path_contexts = Arc::new(vec![]);
let app = App::new()
.service(Files::new(
"/static",
serve_dir,
root_path_context,
path_contexts,
));
Implementations
sourceimpl Files
impl Files
sourcepub fn new(
path: &str,
directory: Arc<RwLock<Option<PathBuf>>>,
root_path_context: Arc<PathContext>,
path_contexts: Arc<Vec<PathContext>>
) -> Files
pub fn new(
path: &str,
directory: Arc<RwLock<Option<PathBuf>>>,
root_path_context: Arc<PathContext>,
path_contexts: Arc<Vec<PathContext>>
) -> Files
Create new Files
instance for specified base directory.
File
uses ThreadPool
for blocking filesystem operations.
By default pool with 5x threads of available cpus is used.
Pool size can be changed by setting ACTIX_THREADPOOL
environment variable.
sourcepub fn redirect_to_slash_directory(self) -> Self
pub fn redirect_to_slash_directory(self) -> Self
Redirects to a slash-ended path when browsing a directory.
By default never redirect.
sourcepub fn guard<G: Guard + 'static>(self, guard: G) -> Self
pub fn guard<G: Guard + 'static>(self, guard: G) -> Self
Adds a routing guard.
Use this to allow multiple chained file services that respond to strictly different properties of a request. Due to the way routing works, if a guard check returns true and the request starts being handled by the file service, it will not be able to back-out and try the next service, you will simply get a 404 (or 405) error response.
To allow POST
requests to retrieve files, see Files::use_guards
.
Examples
use std::{
convert::TryInto,
sync::Arc,
};
use actix_web::{guard::Header, App};
use espresso::{
files::Files,
config::ServerConfig,
};
use tokio::sync::RwLock;
let mut server_config = ServerConfig::default();
let serve_dir = Arc::new(RwLock::new(Some(".".into())));
let root_path_context = Arc::new((&server_config).try_into().unwrap());
let path_contexts = Arc::new(vec![]);
App::new().service(
Files::new("/", serve_dir, root_path_context, path_contexts)
.guard(Header("Host", "example.com"))
);
sourcepub fn method_guard<G: Guard + 'static>(self, guards: G) -> Self
pub fn method_guard<G: Guard + 'static>(self, guards: G) -> Self
Specifies custom guards to use for directory listings and files.
Default behaviour allows GET and HEAD.
sourcepub fn default_handler<F, U>(self, f: F) -> Selfwhere
F: IntoServiceFactory<U, ServiceRequest>,
U: ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse, Error = Error> + 'static,
pub fn default_handler<F, U>(self, f: F) -> Selfwhere
F: IntoServiceFactory<U, ServiceRequest>,
U: ServiceFactory<ServiceRequest, Config = (), Response = ServiceResponse, Error = Error> + 'static,
Sets default handler which is used when no matched file could be found.
Trait Implementations
sourceimpl ServiceFactory<ServiceRequest> for Files
impl ServiceFactory<ServiceRequest> for Files
type Response = ServiceResponse<BoxBody>
type Response = ServiceResponse<BoxBody>
type Error = Error
type Error = Error
type Service = FilesService
type Service = FilesService
Service
created by this factory.type Future = Pin<Box<dyn Future<Output = Result<<Files as ServiceFactory<ServiceRequest>>::Service, <Files as ServiceFactory<ServiceRequest>>::InitError>> + 'static, Global>>
type Future = Pin<Box<dyn Future<Output = Result<<Files as ServiceFactory<ServiceRequest>>::Service, <Files as ServiceFactory<ServiceRequest>>::InitError>> + 'static, Global>>
Service
instance.gsourcefn new_service(&self, _: ()) -> Self::Future
fn new_service(&self, _: ()) -> Self::Future
Auto Trait Implementations
impl !RefUnwindSafe for Files
impl !Send for Files
impl !Sync for Files
impl Unpin for Files
impl !UnwindSafe for Files
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
SF: ServiceFactory<Req>,
impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
SF: ServiceFactory<Req>,
fn into_factory(self) -> SF
fn into_factory(self) -> SF
Self
to a ServiceFactory