ring-clojure / ring-defaults

A library to provide sensible Ring middleware defaults
MIT License
343 stars 32 forks source link
clojure middleware ring

Ring-Defaults Build Status

Knowing what middleware to add to a Ring application, and in what order, can be difficult and prone to error.

This library attempts to automate the process, by providing sensible and secure default configurations of Ring middleware for both websites and HTTP APIs.

Installation

Add the following dependency to your deps.edn file:

ring/ring-defaults {:mvn/version "0.5.0"}

Or to your Leiningen project file:

[ring/ring-defaults "0.5.0"]

Basic Usage

The wrap-defaults middleware sets up standard Ring middleware based on a supplied configuration:

(require '[ring.middleware.defaults :refer :all])

(def site
  (wrap-defaults handler site-defaults))

There are four configurations included with the middleware

The "api" defaults will add support for urlencoded parameters, but not much else.

The "site" defaults add support for parameters, cookies, sessions, static resources, file uploads, and a bunch of browser-specific security headers.

The "secure" defaults force SSL. Unencrypted HTTP URLs are redirected to the equivalent HTTPS URL, and various headers and flags are sent to prevent the browser sending sensitive information over insecure channels.

Proxies

If your app is sitting behind a load balancer or reverse proxy, as is often the case in cloud-based deployments, you'll want to set :proxy to true:

(assoc secure-site-defaults :proxy true)

This is particularly important when your site is secured with SSL, as the SSL redirect middleware will get caught in a redirect loop if it can't determine the correct URL scheme of the request.

Customizing

The default configurations are just maps of options, and can be customized to suit your needs. For example, if you wanted the normal site defaults, but without session support, you could use:

(wrap-defaults handler (assoc site-defaults :session false))

The following configuration keys are supported:

License

Copyright © 2024 James Reeves

Distributed under the MIT License, the same as Ring.