cachmere
Types
Options for serve_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 tocachmere.serve_static
.
pub type ServeStaticOptions {
ServeStaticOptions(
response_headers: List(#(String, String)),
file_types: List(String),
)
}
Constructors
-
ServeStaticOptions( 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(
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 you 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 setting response headers for specific file types.
This allows for configuring headers such as Cache-Control etc.
Examples
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(
file_types: ["js", "css"],
response_headers: [#("cache-control", "max-age=31536000, immutable")],
),
)
// ...
}
ServeStaticOptions
The options for cachmere.ServeStaticOptions
are as follows:
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 tocachmere.serve_static
.
See: cachmere.default_cache_settings()
for a default config for caching.