module Network.Wai.Middleware.Select
(
MiddlewareSelection (..),
selectMiddleware,
selectMiddlewareOn,
selectMiddlewareOnRawPathInfo,
selectMiddlewareExceptRawPathInfo,
passthroughMiddleware,
)
where
import Control.Applicative ((<|>))
import Data.ByteString (ByteString)
import Data.Maybe (fromMaybe)
import Network.Wai
newtype MiddlewareSelection = MiddlewareSelection
{ MiddlewareSelection -> Request -> Maybe Middleware
applySelectedMiddleware :: Request -> Maybe Middleware
}
instance Semigroup MiddlewareSelection where
MiddlewareSelection Request -> Maybe Middleware
f <> :: MiddlewareSelection -> MiddlewareSelection -> MiddlewareSelection
<> MiddlewareSelection Request -> Maybe Middleware
g =
(Request -> Maybe Middleware) -> MiddlewareSelection
MiddlewareSelection ((Request -> Maybe Middleware) -> MiddlewareSelection)
-> (Request -> Maybe Middleware) -> MiddlewareSelection
forall a b. (a -> b) -> a -> b
$ \Request
req -> Request -> Maybe Middleware
f Request
req Maybe Middleware -> Maybe Middleware -> Maybe Middleware
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Request -> Maybe Middleware
g Request
req
instance Monoid MiddlewareSelection where
mempty :: MiddlewareSelection
mempty = (Request -> Maybe Middleware) -> MiddlewareSelection
MiddlewareSelection ((Request -> Maybe Middleware) -> MiddlewareSelection)
-> (Request -> Maybe Middleware) -> MiddlewareSelection
forall a b. (a -> b) -> a -> b
$ Maybe Middleware -> Request -> Maybe Middleware
forall a b. a -> b -> a
const Maybe Middleware
forall a. Maybe a
Nothing
selectMiddleware :: MiddlewareSelection -> Middleware
selectMiddleware :: MiddlewareSelection -> Middleware
selectMiddleware MiddlewareSelection
selection Application
app Request
request Response -> IO ResponseReceived
respond =
Middleware
mw Application
app Request
request Response -> IO ResponseReceived
respond
where
mw :: Middleware
mw :: Middleware
mw = Middleware -> Maybe Middleware -> Middleware
forall a. a -> Maybe a -> a
fromMaybe Middleware
passthroughMiddleware (MiddlewareSelection -> Request -> Maybe Middleware
applySelectedMiddleware MiddlewareSelection
selection Request
request)
passthroughMiddleware :: Middleware
passthroughMiddleware :: Middleware
passthroughMiddleware = Middleware
forall a. a -> a
id
selectMiddlewareOn :: (Request -> Bool) -> Middleware -> MiddlewareSelection
selectMiddlewareOn :: (Request -> Bool) -> Middleware -> MiddlewareSelection
selectMiddlewareOn Request -> Bool
doesApply Middleware
mw = (Request -> Maybe Middleware) -> MiddlewareSelection
MiddlewareSelection ((Request -> Maybe Middleware) -> MiddlewareSelection)
-> (Request -> Maybe Middleware) -> MiddlewareSelection
forall a b. (a -> b) -> a -> b
$ \Request
request ->
if Request -> Bool
doesApply Request
request
then Middleware -> Maybe Middleware
forall a. a -> Maybe a
Just Middleware
mw
else Maybe Middleware
forall a. Maybe a
Nothing
selectMiddlewareOnRawPathInfo :: ByteString -> Middleware -> MiddlewareSelection
selectMiddlewareOnRawPathInfo :: ByteString -> Middleware -> MiddlewareSelection
selectMiddlewareOnRawPathInfo ByteString
path = (Request -> Bool) -> Middleware -> MiddlewareSelection
selectMiddlewareOn ((ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
path) (ByteString -> Bool) -> (Request -> ByteString) -> Request -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Request -> ByteString
rawPathInfo)
selectMiddlewareExceptRawPathInfo :: ByteString -> Middleware -> MiddlewareSelection
selectMiddlewareExceptRawPathInfo :: ByteString -> Middleware -> MiddlewareSelection
selectMiddlewareExceptRawPathInfo ByteString
path = (Request -> Bool) -> Middleware -> MiddlewareSelection
selectMiddlewareOn ((ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= ByteString
path) (ByteString -> Bool) -> (Request -> ByteString) -> Request -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Request -> ByteString
rawPathInfo)