Skip to content

adetokunbo/wai-middleware-delegate

Repository files navigation

wai-middleware-delegate

GitHub CI Stackage Nightly Hackage Hackage Dependencies BSD3

wai-middleware-delegate is a WAI middleware that allows requests to be handled by a delegate application that proxies requests to another server.

Example

{-# LANGUAGE OverloadedStrings #-}

import  Network.HTTP.Client.TLS         (newTlsManager)
import  Network.HTTP.Types              (status500)
import  Network.Wai
import  Network.Wai.Handler.Warp        (run)
import  Network.Wai.Middleware.Delegate (ProxySettings (..),
                                         defaultSettings,
                                         delegateToProxy)

demoSettings :: ProxySettings
demoSettings = defaultSettings { proxyHost = "httpbin.org" }

-- | An trivial app that proxies every request to httpbin.org
httpBin :: ProxySettings -> IO Application
httpBin s = do

  -- delegate everything!
  let takeItAll = const True
      dummyApp _ respond = respond $
        responseLBS status500 [] "I should have been proxied"

  manager <- newTlsManager
  return $ delegateToProxy s manager takeItAll dummyApp

main :: IO ()
main = httpBin demoSettings >>= run 3000