cachmere
Types
Options for serve_static_with
.
etags
is a boolean that enables the use of entity tags. Enabling this will generate etags for all files served from the location passed toserve_static_with
.response_headers
is a list of response headers, in a tuple format. eg. “cache-control”: “max-age=31536000”.file_types
is a list of file types to apply the defined response headers to. This allows you to control the headers, such as cache-control, on a per file type basis. Unlisted file types will still be served but they won’t have the headers defined inresponse_headers
applied to them. An empty list will result in identical behaviour toserve_static
.
pub type ServeStaticOptions {
ServeStaticOptions(
etags: Bool,
response_headers: List(#(String, String)),
file_types: List(String),
)
}
Constructors
-
ServeStaticOptions( etags: Bool, response_headers: List(#(String, String)), file_types: List(String), )
Functions
pub fn default_cache_settings() -> ServeStaticOptions
Returns ServeStaticOptions
with a default config for caching
Default Settings
ServeStaticOptions(
etags: False,
file_types: ["js", "css"],
response_headers: [#("cache-control", "max-age=31536000, immutable")]
)
pub fn serve_static(
req: Request(Connection),
under prefix: String,
from directory: String,
next handler: fn() -> Response(Body),
) -> Response(Body)
A middleware function that serves files from a directory, along with a
suitable content-type
header for known file extensions.
Files are sent using the File
response body type, so they will be sent
directly to the client from the disc, without being read into memory.
The under
parameter is the request path prefix that must match for the
file to be served.
under | from | request.path | file |
---|---|---|---|
/static | /data | /static/file.txt | /data/file.txt |
`` | /data | /static/file.txt | /data/static/file.txt |
/static | `` | /static/file.txt | file.txt |
This middleware will discard any ..
path segments in the request path to
prevent the client from accessing files outside of the directory. It is
advised not to serve a directory that contains your source code, application
configuration, database, or other private files.
Examples
fn handle_request(req: Request) -> Response {
use <- cachmere.serve_static(req, under: "/static", from: "/public")
// ...
}
Typically your static assets may be kept in your project in a directory
called priv
. The priv_directory
function can be used to get a path to
this directory.
fn handle_request(req: Request) -> Response {
let assert Ok(priv) = priv_directory("my_application")
use <- cachmere.serve_static(req, under: "/static", from: priv)
// ...
}
pub fn serve_static_with(
req: Request(Connection),
under prefix: String,
from directory: String,
options options: ServeStaticOptions,
next handler: fn() -> Response(Body),
) -> Response(Body)
Functions the same as serve_static
but takes options for enabling etags and setting response headers for specific file types.
This allows for configuring headers such as Cache-Control etc.
Examples
Serve files from static folder and apply cache-control header to .js
and .css
files.
All other file will be served but they won’t have the defined response headers added to them.
fn handle_request(req: Request) -> Response {
let assert Ok(priv) = priv_directory("my_application")
use <- cachmere.serve_static_with(
req,
under: "/static",
from: priv,
options: cachmere.ServeStaticOptions(
etags: False,
file_types: ["js", "css"],
response_headers: [#("cache-control", "max-age=31536000, immutable")],
),
)
// ...
}
Serve files from static folder using etags. If files have not been edited, serve_static_with
will return a status 304 allowing the browser to use the cached version of the file.
fn handle_request(req: Request) -> Response {
let assert Ok(priv) = priv_directory("my_application")
use <- cachmere.serve_static_with(
req,
under: "/static",
from: priv,
options: cachmere.ServeStaticOptions(
etags: True,
file_types: [],
response_headers: [],
),
)
// ...
}