wai-middleware-consul-0.1.0.2: Wai Middleware for Consul

Copyright(c) FPComplete, 2015
LicenseMIT
MaintainerTim Dysinger <tim@fpcomplete.com>
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Network.Wai.Middleware.Consul

Description

This module helps you proxy information to Consul from the internet & also react to changes to K/V data coming from Consul.

      ┌─────────┐      ┌─────────┐
      │  JSON   │      │  LOAD   │
      │  HTTP   │─────▶│ BALANCR │
      │  POST   │      │         │
      └─────────┘      └─────────┘
                            │
        ┌────────────┬──────┘─ ─ ─
        │                         │
        ▼            ▼            ▼
   ┌─────────┐  ┌─────────┐  ┌─────────┐
   │         │  │         │  │         │
┌──│ WAI App │  │ WAI App │  │ WAI App │
│  │         │  │         │  │         │
│  └─────────┘  └─────────┘  └─────────┘
│                    ▲            ▲
│                    │            │
│       ┌────────────┴────────────┘
│       │
│       │
│  ┌─────────┐  ┌─────────┐  ┌─────────┐
│  │         │  │         │  │         │
└─▶│ Consul  │──│ Consul  │──│ Consul  │
   │         │  │         │  │         │
   └─────────┘  └─────────┘  └─────────┘

Synopsis

Documentation

data ConsulSettings Source

Consul Settings for watching & proxying Consul data

mkConsulProxy :: (MonadIO m, Functor m) => ConsulSettings -> m Middleware Source

Create WAI middleware that can be used to proxy incoming data into Consul (one-way). This function initiates our consul client and returns the middleware for WAI to use. The middleware will filter incoming requests that match ConsulSettings csFilter. If there is a match it will create a make the key value put call for Consul using the incoming request body as the data for the Consul K/V.

mkConsulWatch :: (MonadBaseControl IO m, MonadLoggerIO m) => ConsulSettings -> m Void Source

Creates a background process to receive notifications. Notifications happen via blocking HTTP request. (The HTTP client manager used has been configured to wait forever for a response.) The ConsulSettings (csHost, csPort & csKey) are used to connect to Consul and watch for key-value changes. When Consul's value changes, it will respond to the HTTP request. Upon receiving a good changed-value response, we fire the csCallback function to allow for a reaction to the data change. If there there is a problem with the request/response cycle or an exception in the supplied callback function, we just re-make the rquest & wait patiently for changes again.

withConsul :: (Monad m, MonadBaseControl IO m, MonadLoggerIO m) => ConsulSettings -> (Middleware -> m a) -> m a Source

Creates a complete Consul middleware for the cluster. Combines mkConsulWatch async function (watches Consul data for updates) & mkConsulProxy (proxys data from the internet to Consul) into one common-use function. This will probably be the function you want. See the example/ application for more insight.