Skip to main content

StaticFiles

Trait StaticFiles 

Source
pub trait StaticFiles: Send + Sync {
    // Required methods
    fn serve_index(&self, headers: &HeaderMap) -> Option<Response<Bytes>>;
    fn matches(&self, path: &str) -> bool;
    fn serve_file_only(
        &self,
        path: &str,
        headers: &HeaderMap,
    ) -> Option<Response<Bytes>>;

    // Provided methods
    fn is_spa(&self) -> bool { ... }
    fn serve_not_found(&self, _headers: &HeaderMap) -> Option<Response<Bytes>> { ... }
}
Expand description

Trait for serving static files (filesystem or embedded).

Implemented by StaticFileHandler and EmbeddedStaticFileHandler. The router stores Option<Arc<dyn StaticFiles>> to serve files without knowing the concrete handler type.

Required Methods§

Source

fn serve_index(&self, headers: &HeaderMap) -> Option<Response<Bytes>>

Serve the index page (for SPA fallback).

Source

fn matches(&self, path: &str) -> bool

Check if this handler can serve the given path.

Source

fn serve_file_only( &self, path: &str, headers: &HeaderMap, ) -> Option<Response<Bytes>>

Serve a file at the given path (no SPA fallback).

Provided Methods§

Source

fn is_spa(&self) -> bool

Whether this handler serves a Single-Page Application (SPA).

Source

fn serve_not_found(&self, _headers: &HeaderMap) -> Option<Response<Bytes>>

Serve the configured custom 404 page, if any. Distinct from Self::serve_index (the SPA fallback): this returns a 404 status with the user-authored not-found body (typically from [package.metadata.app.static].not_found). Returns None when no custom 404 is configured — the router falls back to its platform JSON 404 in that case.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§