Static files
plumeria.static/wrap-static serves a directory. It's enough for a built site or a single-page app with nothing in front of it:
(require '[clojure.string :as str]
'[plumeria.static :as static])
(def app
(static/wrap-static api-routes "dist"
{:spa-fallback "index.html"
;; hashed build output can be cached for good; anything
;; else (nil) gets no Cache-Control and revalidates
:cache-control (fn [path] (when (str/starts-with? path "assets/")
"public, max-age=31536000, immutable"))}))
A GET or HEAD for a file under the root gets a Content-Type, an ETag and Last-Modified, and 304, 206 (one byte range, so video seeks and curl -C - resumes work) or 416 as the request asks. A directory serves its index.html, and a directory asked for without its slash gets a 301 to it. Everything else goes to your handler: another method, a path outside :prefix, a missing file, and any path that isn't safe (.., an encoded / or \, a NUL, a dotfile other than /.well-known/, a symlink). An unsafe path looks exactly like a missing one.
With :spa-fallback, a GET that your handler answers 404, from a browser asking for HTML (Accept: text/html) for a path whose last segment has no dot, gets that file instead, with Cache-Control: no-cache (only a :cache-control fn can override it). Your routes still win, an API path keeps its 404, and a missing /app.js is never answered with HTML.
Files up to 1 MiB are kept in memory and re-checked at most once a second, with a gzipped copy of text types for clients that accept it (a Range request always gets the plain bytes). Larger files are sent from disk.
| option | default | |
|---|---|---|
:prefix | nil | mount point, e.g. "/assets" |
:index-files | ["index.html"] | tried in order for a directory |
:allow-symlinks? | false | follow symlinks that stay inside the root |
:cache-control | nil | a String for every file, or a fn of the path under the root |
:spa-fallback | nil | a file under the root for the browser navigations above |
:mime-types | extension to Content-Type, merged over default-mime-types | |
:cache? | true | keep small files in memory |
:cache | one per mount | a (static/new-cache) to share between mounts |
:stat-window-ms | 1000 | how long a cached file is trusted without a stat |
:max-cache-file-bytes | 1 MiB | largest file held in memory |
:max-cache-bytes | 64 MiB | all held files together (gzip copies come on top, so memory can reach about twice this) |
:max-cache-entries | 4096 | |
:gzip? | true | keep and send gzip copies of held text files |
:gzip-min-size | 1024 | smallest file worth gzipping |
A file counts as changed only when its size or modification time does, as with nginx. A deploy that keeps both, say a build that sets every file to one date for reproducibility (SOURCE_DATE_EPOCH), has to touch the files it changed or use content-hashed names, or browsers and the cache keep the old copy.
Files your handler picks
For a file your handler chooses itself, such as a download behind a login, return it as the :body and wrap the handler in plumeria.files/wrap-file-conditions to get the same validators, 304s and ranges. A handler can also answer part of a file directly with (plumeria.response/file-range file offset length).
The disk pool
Files are opened and read on a disk pool the server keeps, never on the connection fibers, so a slow disk can't stall other requests. Its size is :file-threads (default one per CPU), and each request carries the pool as :plumeria/file-pool. File bodies still go out through the connection's own writer, so they work over HTTPS the same way.
Over loopback a cached 12 KB stylesheet ran at 22-27k req/s and a 64 MiB file at about 3.3 GB/s. While 20 clients downloaded that file, a plain endpoint kept about a tenth of its throughput with the default pool and about half with :file-threads 2, which made the downloads 11 to 16% slower. Loopback is the worst case for this, since the clients share the server's cores.