pub struct Embed<E, F>where
E: 'static + RustEmbed,
F: FallbackHandler,{ /* private fields */ }
Expand description
Wrapper of rust_embed for actix.
Embed
service must be registered with App::service()
method.
rust_embed documentation: https://docs.rs/rust-embed/
§Examples
use actix_web::App;
use actix_embed::Embed;
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "testdata/"]
struct Assets;
let app = App::new()
.service(Embed::new("/static", &Assets));
Implementations§
Source§impl<E> Embed<E, DefaultFallbackHandler>where
E: 'static + RustEmbed,
impl<E> Embed<E, DefaultFallbackHandler>where
E: 'static + RustEmbed,
Sourcepub fn new<P: AsRef<str>>(mount_path: P, assets: &E) -> Self
pub fn new<P: AsRef<str>>(mount_path: P, assets: &E) -> Self
Create new Embed instance.
§Arguments
The first argument (mount_path
) is the root URL at which the embed files are served.
For example, /assets
will serve files at example.com/assets/...
.
The second argument (assets
) is the instance implements rust_embed::RustEmbed.
For more information, see rust_embed documentation: https://docs.rs/rust-embed/
§Notes
If the mount path is set as the root path /
, services registered after this one will
be inaccessible. Register more specific handlers and services before it.
Source§impl<E, F> Embed<E, F>where
E: 'static + RustEmbed,
F: FallbackHandler,
impl<E, F> Embed<E, F>where
E: 'static + RustEmbed,
F: FallbackHandler,
Sourcepub fn strict_slash(self, strict_slash: bool) -> Self
pub fn strict_slash(self, strict_slash: bool) -> Self
Set whether to ignore the trailing slash of the requested path.
Defaults to false
.
If it’s set to true, then file ‘/dir/file’ cannot be accessed by request path ‘/dir/file/’.
Sourcepub fn index_file<P: AsRef<str>>(self, path: P) -> Self
pub fn index_file<P: AsRef<str>>(self, path: P) -> Self
Set the path of the index file.
By default there is no index file.
The index file is treated as the default file returned when a request visit the root directory.
Sourcepub fn fallback_handler<NF>(self, handler: NF) -> Embed<E, NF>where
NF: FallbackHandler,
pub fn fallback_handler<NF>(self, handler: NF) -> Embed<E, NF>where
NF: FallbackHandler,
Sets fallback handler which is used when no matched file could be found.
The default fallback handler returns 404 responses.
§Examples
use actix_embed::Embed;
use actix_web::HttpResponse;
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[folder = "testdata/"]
struct Assets;
let embed = Embed::new("/static", &Assets)
.index_file("index.html")
.fallback_handler(|_: &_| HttpResponse::BadRequest().body("not found"));
§Note
It is necessary to add type annotation for the closure parameters like |_: &_| ...
.
See https://github.com/rust-lang/rust/issues/41078
Trait Implementations§
Source§impl<E, F> HttpServiceFactory for Embed<E, F>where
E: 'static + RustEmbed,
F: FallbackHandler,
impl<E, F> HttpServiceFactory for Embed<E, F>where
E: 'static + RustEmbed,
F: FallbackHandler,
fn register(self, config: &mut AppService)
Source§impl<E, F> ServiceFactory<ServiceRequest> for Embed<E, F>where
E: 'static + RustEmbed,
F: FallbackHandler,
impl<E, F> ServiceFactory<ServiceRequest> for Embed<E, F>where
E: 'static + RustEmbed,
F: FallbackHandler,
Source§type Response = ServiceResponse
type Response = ServiceResponse
Source§type Future = Pin<Box<dyn Future<Output = Result<<Embed<E, F> as ServiceFactory<ServiceRequest>>::Service, <Embed<E, F> as ServiceFactory<ServiceRequest>>::InitError>>>>
type Future = Pin<Box<dyn Future<Output = Result<<Embed<E, F> as ServiceFactory<ServiceRequest>>::Service, <Embed<E, F> as ServiceFactory<ServiceRequest>>::InitError>>>>
Service
instance.gSource§fn new_service(&self, _: ()) -> Self::Future
fn new_service(&self, _: ()) -> Self::Future
Auto Trait Implementations§
impl<E, F> Freeze for Embed<E, F>where
F: Freeze,
impl<E, F> RefUnwindSafe for Embed<E, F>where
F: RefUnwindSafe,
E: RefUnwindSafe,
impl<E, F> Send for Embed<E, F>
impl<E, F> Sync for Embed<E, F>
impl<E, F> Unpin for Embed<E, F>
impl<E, F> UnwindSafe for Embed<E, F>where
F: UnwindSafe,
E: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
SF: ServiceFactory<Req>,
impl<SF, Req> IntoServiceFactory<SF, Req> for SFwhere
SF: ServiceFactory<Req>,
Source§fn into_factory(self) -> SF
fn into_factory(self) -> SF
Self
to a ServiceFactory