{-# LANGUAGE OverloadedStrings #-}
module Network.Wai.Route
( Handler
, route
) where
import Data.ByteString (ByteString)
import Network.HTTP.Types
import Network.Wai
import Network.Wai.Route.Tree
import Prelude hiding (lookup)
import qualified Data.ByteString.Lazy as L
type Handler m = [(ByteString, ByteString)]
-> Request
-> (Response -> m ResponseReceived)
-> m ResponseReceived
route :: Monad m
=> [(ByteString, Handler m)]
-> Request
-> (Response -> m ResponseReceived)
-> m ResponseReceived
route :: forall (m :: * -> *).
Monad m =>
[(ByteString, Handler m)]
-> Request
-> (Response -> m ResponseReceived)
-> m ResponseReceived
route [(ByteString, Handler m)]
rs Request
rq Response -> m ResponseReceived
k = case Tree (Handler m) -> [ByteString] -> Maybe (Payload (Handler m))
forall a. Tree a -> [ByteString] -> Maybe (Payload a)
lookup ([(ByteString, Handler m)] -> Tree (Handler m)
forall a. [(ByteString, a)] -> Tree a
fromList [(ByteString, Handler m)]
rs) [ByteString]
segs of
Just Payload (Handler m)
e -> Payload (Handler m) -> Handler m
forall a. Payload a -> a
value Payload (Handler m)
e (Captures -> [(ByteString, ByteString)]
captured (Captures -> [(ByteString, ByteString)])
-> Captures -> [(ByteString, ByteString)]
forall a b. (a -> b) -> a -> b
$ Payload (Handler m) -> Captures
forall a. Payload a -> Captures
captures Payload (Handler m)
e) Request
rq Response -> m ResponseReceived
k
Maybe (Payload (Handler m))
Nothing -> Response -> m ResponseReceived
k Response
notFound
where
segs :: [ByteString]
segs = ByteString -> [ByteString]
segments (Request -> ByteString
rawPathInfo Request
rq)
notFound :: Response
notFound = Status -> ResponseHeaders -> ByteString -> Response
responseLBS Status
status404 [] ByteString
L.empty