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 forall a b. (a -> b) -> a -> b
$ \Request
req -> Request -> Maybe Middleware
f Request
req 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 forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const 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 = forall a. a -> Maybe a -> a
fromMaybe Middleware
passthroughMiddleware (MiddlewareSelection -> Request -> Maybe Middleware
applySelectedMiddleware MiddlewareSelection
selection Request
request)
passthroughMiddleware :: Middleware
passthroughMiddleware :: Middleware
passthroughMiddleware = 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 forall a b. (a -> b) -> a -> b
$ \Request
request ->
if Request -> Bool
doesApply Request
request
then forall a. a -> Maybe a
Just Middleware
mw
else forall a. Maybe a
Nothing
selectMiddlewareOnRawPathInfo :: ByteString -> Middleware -> MiddlewareSelection
selectMiddlewareOnRawPathInfo :: ByteString -> Middleware -> MiddlewareSelection
selectMiddlewareOnRawPathInfo ByteString
path = (Request -> Bool) -> Middleware -> MiddlewareSelection
selectMiddlewareOn ((forall a. Eq a => a -> a -> Bool
== ByteString
path) 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 ((forall a. Eq a => a -> a -> Bool
/= ByteString
path) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Request -> ByteString
rawPathInfo)