reitit/doc/ring/default_middleware.md

63 lines
2.3 KiB
Markdown
Raw Normal View History

2018-08-02 13:20:39 +00:00
# Default Middleware
```clj
2024-06-30 15:58:46 +00:00
[metosin/reitit-middleware "0.7.1"]
2018-08-02 13:20:39 +00:00
```
2021-07-27 16:33:17 +00:00
Any Ring middleware can be used with `reitit-ring`, but using data-driven middleware is preferred as they are easier to manage and in many cases yield better performance. `reitit-middleware` contains a set of common ring middleware, lifted into data-driven middleware.
2018-08-02 13:20:39 +00:00
2018-12-30 14:53:15 +00:00
* [Parameter Handling](#parameters-handling)
* [Exception Handling](#exception-handling)
* [Content Negotiation](#content-negotiation)
* [Multipart Request Handling](#multipart-request-handling)
* [Inspecting Middleware Chain](#inspecting-middleware-chain)
2018-08-02 13:20:39 +00:00
2018-12-30 14:53:15 +00:00
## Parameters Handling
2018-09-07 20:46:26 +00:00
`reitit.ring.middleware.parameters/parameters-middleware` to capture query- and form-params. Wraps
`ring.middleware.params/wrap-params`.
**NOTE**: This middleware will be factored into two parts: a query-parameters middleware and a Muuntaja format responsible for the the `application/x-www-form-urlencoded` body format. cf. https://github.com/metosin/reitit/issues/134
2018-09-07 20:46:26 +00:00
2018-12-30 14:53:15 +00:00
## Exception Handling
2018-08-02 13:20:39 +00:00
2019-05-25 12:57:34 +00:00
See [Exception Handling with Ring](exceptions.md).
2018-08-02 13:20:39 +00:00
## Content Negotiation
See [Content Negotiation](content_negotiation.md).
2018-12-30 14:53:15 +00:00
## Multipart Request Handling
2018-08-02 13:20:39 +00:00
2018-08-22 17:06:09 +00:00
Wrapper for [Ring Multipart Middleware](https://github.com/ring-clojure/ring/blob/master/ring-core/src/ring/middleware/multipart_params.clj). Emits swagger `:consumes` definitions automatically.
Expected route data:
2021-07-27 16:33:17 +00:00
2018-08-22 17:06:09 +00:00
| key | description |
| -------------|-------------|
| `[:parameters :multipart]` | mounts only if defined for a route.
2018-08-02 13:20:39 +00:00
```clj
(require '[reitit.ring.middleware.multipart :as multipart])
```
2018-08-22 17:06:09 +00:00
* `multipart/multipart-middleware` a preconfigured middleware for multipart handling
* `multipart/create-multipart-middleware` to generate with custom configuration
## Inspecting Middleware Chain
2018-12-30 14:53:15 +00:00
`reitit.ring.middleware.dev/print-request-diffs` is a [middleware chain transforming function](transforming_middleware_chain.md). It prints a request and response diff between each middleware. To use it, add the following router option:
2018-12-30 14:53:15 +00:00
```clj
:reitit.middleware/transform reitit.ring.middleware.dev/print-request-diffs
```
Partial sample output:
![Opensensors perf test](../images/ring-request-diff.png)
2018-08-02 13:20:39 +00:00
## Example app
See an example app with the default middleware in action: <https://github.com/metosin/reitit/blob/master/examples/ring-malli-swagger/src/example/server.clj>.