• Stars
    star
    28
  • Rank 882,216 (Top 18 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created about 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Express relations between Pedestal interceptors and decouple scope from execution order

angel-interceptor

Clojars Project

Express relations between Pedestal interceptors and decouple scope from execution order. Include or exclude interceptors based on predicates.

Requires Pedestal 0.5.0 or greater.

(require '[angel.interceptor :as angel]
         '[io.pedestal.http :as bootstrap]
         '[io.pedestal.http.route.definition :refer [defroutes]])

(defn- not-prod? []
  (not= :prod (:env (get-settings))))

(defroutes routes
  ["/api" ^:interceptors [(angel/requires rate-limiter :account)
                          (angel/conditional show-stacktraces not-prod?)]
    ["/slack" ^:interceptors [(angel/provides slack-auth :account)] ...]
    ["/hipchat" ^:interceptors [(angel/provides hipchat-auth :account)] ...]])

(def service
  (-> {::bootstrap/routes routes}
      angel/satisfy
      bootstrap/default-interceptors))

rate-limiter will run after slack-auth or hipchat-auth but still run before the handler.

Introduction

Pedestal interceptors are applied sequentially in the order in which they are specified, starting with those defined at the service level and progressing on through those defined at route branch and finally route leaf level.

Angel Interceptor allows you to express relations between interceptors and conditions of inclusion to gain maximum reuse without repetition.

Dependent Interceptors

Imagine you are building an API for integration with multiple chat services. You would naturally put slack-auth and hipchat-auth as interceptors on the appropriate route branches, as below.

(require '[io.pedestal.http.route.definition :refer [defroutes]])

(defroutes routes
  ["/api" ^:interceptors [rate-limiter]
    ["/slack" ^:interceptors [slack-auth] ...]
    ["/hipchat" ^:interceptors [hipchat-auth] ...]])

You want to limit the number of times each account can use your API, so you add an interceptor - rate-limiter - which applies to all routes in the API. It requires an account-id to see if the account has exceeded its limit and runs before the handler to avoid performing mutating or expensive operations.

In Pedestal the rate-limiter interceptor will run before the slack-auth or hipchat-auth interceptors, so the account-id will not be available.

Angel Interceptor allows you to express a provides and requires relation between interceptors:

(require '[angel.interceptor :as angel]
         '[io.pedestal.http.route.definition :refer [defroutes]])

(defroutes routes
  ["/api" ^:interceptors [(angel/requires rate-limiter :account)]
    ["/slack" ^:interceptors [(angel/provides slack-auth :account)] ...]
    ["/hipchat" ^:interceptors [(angel/provides hipchat-auth :account)] ...]])

Angel Interceptor will then reorder your interceptors such that rate-limiter will run immediately after slack-auth or hipchat-auth. To do this, simply run angel/satisfy over the service map:

(def service
  (angel/satisfy
    {:io.pedestal.http/routes routes}))

Conditional Interceptors

Sometimes interceptors may apply, or not apply, depending on some external factor e.g. you are running in a non-production environment. Angel Interceptor allows you to express predicates for including interceptors:

(require '[angel.interceptor :as angel]
         '[io.pedestal.http.route.definition :refer [defroutes]])

(defn- not-prod? []
  (not= :prod (:env (get-settings))))

(defroutes routes
  ["/api" ^:interceptors [(angel/conditional show-stacktraces not-prod?)]
    ["/slack" ...]
    ["/hipchat" ...]])

If the conditional evaluates to false the interceptor is removed from the chain when angel/satisfy is run.

Build

CircleCI

Bugs

Please raise issues or send pull requests on GitHub

License

Copyright © 2015 Oliver Hine

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

More Repositories

1

martian

The HTTP abstraction library for Clojure/script, supporting OpenAPI, Swagger, Schema, re-frame and more
Clojure
470
star
2

re-graph

A graphql client for clojurescript and clojure
Clojure
447
star
3

superlifter

A DataLoader for Clojure/script
Clojure
158
star
4

re-learn

A library for integrating tutorials into your re-frame/reagent application
Clojure
138
star
5

pedestal-api

Easily build APIs in Pedestal using Schema and Swagger
Clojure
106
star
6

kamera

UI testing via image comparison and devcards
Clojure
86
star
7

lacinia-gen

Generators for GraphQL
Clojure
69
star
8

re-jump.el

emacs navigation for re-frame projects
Emacs Lisp
69
star
9

locksmith

Want to use GraphQL with Clojure/script but don't want keBab or snake_keys everywhere? Use locksmith to change all the keys!
Clojure
61
star
10

slacky

Memes as a Slack Service
Clojure
33
star
11

doo-chrome-devprotocol

A runner for doo which runs tests in Chrome, using the Chrome Dev Protocol with no need for karma or npm.
Clojure
27
star
12

carmine-streams

Utility functions for working with Redis streams in carmine
Clojure
26
star
13

fixa

Better test fixtures for clojure
Clojure
26
star
14

oxbow

A Server Sent Events (SSE) client for Clojurescript based on js/fetch
Clojure
24
star
15

spa-skeleton

A skeleton project for a ClojureScript Single Page Application backed by a Swagger API
Clojure
21
star
16

re-partee

How I build Clojurescript apps
Clojure
15
star
17

carve.el

Emacs plugin for borkdude/carve
Emacs Lisp
9
star
18

alrightee

Tee for re-frame
Clojure
7
star
19

learning-clojure

Learning materials for Clojure
Clojure
5
star
20

tinybeans-archive

Create an archive of a tinybeans journal
Clojure
4
star
21

cljockwork

A REST API for cron4j, written in Clojure
Clojure
4
star
22

stardev-feedback

Capturing feedback for https://stardev.io
3
star
23

haproxy-cert-jwt

A Lua extension for HAProxy to turn an SSL client certificate into a JWT for the backend
Lua
2
star
24

one-route

A Ring webserver with one route
HTML
2
star
25

slacky-bot

All the memes for Slack
Clojure
2
star
26

cljs-webapp-from-scratch

Clojure
2
star
27

ingred

Search recipes by ingredient - a REST api written in Clojure with data scraped from the BBC
Clojure
2
star
28

sunshine

Clojure
2
star
29

fast-feedback

A presentation giving guidance on how to optimise your feedback loop and improve efficiency
HTML
1
star
30

a-taste-of-clojure

A talk to introduce (Java) developers to Clojure
JavaScript
1
star
31

sanakone

Learn Finnish
Clojure
1
star
32

masvn

Subversion integration for emacs based on dsvn and inspired by magit
Emacs Lisp
1
star