reitit/doc/ring/static.md

65 lines
2.1 KiB
Markdown
Raw Normal View History

2018-04-23 05:27:16 +00:00
# Static Resources (Clojure Only)
2018-04-25 18:12:49 +00:00
Static resources can be served with a help of `reitit.ring/create-resource-handler`. It takes optionally an options map and returns a ring handler to serve files from Classpath.
2018-04-23 05:27:16 +00:00
There are two options to serve the files.
## Internal routes
2018-04-23 05:33:44 +00:00
This is good option if static files can be from non-conflicting paths, e.g. `"/assets/*"`.
2018-04-23 05:27:16 +00:00
```clj
(require '[reitit.ring :as ring])
(ring/ring-handler
(ring/router
[["/ping" (constantly {:status 200, :body "pong"})]
["/assets/*" (ring/create-resource-handler)]])
(ring/create-default-handler))
```
2018-04-24 18:17:25 +00:00
To serve static files with conflicting routes, e.g. `"/*#`, one needs to disable the conflict resolution:
2018-04-23 05:33:44 +00:00
```clj
(require '[reitit.ring :as ring])
(ring/ring-handler
(ring/router
[["/ping" (constantly {:status 200, :body "pong"})]
["/*" (ring/create-resource-handler)]]
{:conflicts (constantly nil)})
(ring/create-default-handler))
```
2018-04-23 05:27:16 +00:00
## External routes
2018-04-23 05:33:44 +00:00
To serve files from conflicting paths, e.g. `"/*"`, one option is to mount them to default-handler branch of `ring-handler`. This way, they are only served if none of the actual routes have matched.
2018-04-23 05:27:16 +00:00
```clj
(ring/ring-handler
(ring/router
["/ping" (constantly {:status 200, :body "pong"})])
(ring/routes
(ring/create-resource-handler {:path "/"})
(ring/create-default-handler)))
```
## Configuration
`reitit.ring/create-resource-handler` takes optionally an options map to configure how the files are being served.
2018-04-25 05:32:01 +00:00
| key | description |
| -----------------|-------------|
| :parameter | optional name of the wildcard parameter, defaults to unnamed keyword `:`
| :root | optional resource root, defaults to `"public"`
| :path | optional path to mount the handler to. Works only if mounted outside of a router.
| :loader | optional class loader to resolve the resources
| :allow-symlinks? | allow symlinks that lead to paths outside the root classpath directories, defaults to `false`
2018-04-23 05:27:16 +00:00
### TODO
2018-04-25 18:12:49 +00:00
* support for things like `:cache`, `:etag`, `:last-modified?`, `:index-files` and `:gzip`
2018-04-25 05:32:01 +00:00
* support for ClojureScript
* serve from file-system