2.0 KiB
title | menu | weight |
---|---|---|
Static Files | docs_advanced | 230 |
Individual file
It is possible to serve static files with a custom path pattern and NamedFile
. To
match a path tail, we can use a [.*]
regex.
{{< include-example example="static-files" file="main.rs" section="individual-file" >}}
Directory
To serve files from specific directories and sub-directories, Files
can be used.
Files
must be registered with an App::service()
method, otherwise
it will be unable to serve sub-paths.
{{< include-example example="static-files" file="directory.rs" section="directory" >}}
By default files listing for sub-directories is disabled. Attempt to load directory listing will return 404 Not Found response. To enable files listing, use Files::show_files_listing() method.
Instead of showing files listing for directory, it is possible to redirect to a specific index file. Use the Files::index_file() method to configure this redirect.
Configuration
Generic trait StaticFileConfig
can be used to specify various options
for serving files:
content_disposition_map
- function to be used for mapping file's mime to correspondingContent-Disposition
typeis_use_etag
- specifies whetherETag
shall be calculated and included in headers.is_use_last_modifier
- specifies whether file modified timestamp should be used and added toLast-Modified
header.is_method_allowed
- allows to control which HTTP methods are allowed to be used when accessing file.
All of the above methods are optional and provided with the best defaults. But it is possible to customize any of them by implementing the trait onto own struct.
{{< include-example example="static-files" file="configuration.rs" section="config-one" >}}
The Configuration cal also be applied to directory service:
{{< include-example example="static-files" file="configuration_two.rs" section="config-two" >}}