cachmere
Types
Options for adding response headers to a statically served file
-
Variant
ResponseHeaders
takes a list of response headers, in tuple format. eg. “cache-control”: “max-age=31536000”. Headers defined will be added to all statically served files. An empty list will result in identical behaviour toserve_static
. -
Variant
ResponseHeadersFor
allows for filtering by file type. It takes a list of response headers and a list of file types. eg. [“js”, “css”]. Response headers will only be added to files with the same file type. Unlisted file types will still be served but they won’t have the headers defined inheaders
applied to them.
pub type ResponseHeaderOptions {
ResponseHeaders(List(#(String, String)))
ResponseHeadersFor(
headers: List(#(String, String)),
file_types: List(String),
)
}
Constructors
-
ResponseHeaders(List(#(String, String)))
-
ResponseHeadersFor( headers: List(#(String, String)), file_types: List(String), )
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 aResponseHeaderOptions
type. This allows for defining a list of response headers, in a tuple format. eg. “cache-control”: “max-age=31536000”. As well as optionally filtering by file type. Giving you control over which file types get the supplied headers added to them.
See ResponseHeaderOptions
type for more details.
pub type ServeStaticOptions {
ServeStaticOptions(
etags: Bool,
response_headers: ResponseHeaderOptions,
)
}
Constructors
-
ServeStaticOptions( etags: Bool, response_headers: ResponseHeaderOptions, )
Values
pub fn default_cache_settings() -> ServeStaticOptions
Deprecated: Use wisp.serve_static instead of serve_static_with
Returns ServeStaticOptions
with a default config for caching
Default Settings
ServeStaticOptions(
etags: False,
response_headers: ResponseHeadersFor(
headers: [#("cache-control", "max-age=31536000, immutable, private")],
file_types: ["js", "css"],
),
)
pub fn serve_static(
req: Request(Connection),
under prefix: String,
from directory: String,
next handler: fn() -> Response(Body),
) -> Response(Body)
Deprecated: Use wisp.serve_static instead
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)
Deprecated: Use wisp.serve_static instead
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,
response_headers: cachmere.ResponseHeadersFor(
headers: [#("cache-control", "max-age=31536000, immutable, private")],
file_types: ["js", "css"],
),
),
)
// ...
}
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,
response_headers: cachmere.ResponseHeaders([]),
),
)
// ...
}