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 rs rq k = case lookup (fromList rs) segs of
Just e -> value e (captured $ captures e) rq k
Nothing -> k notFound
where
segs = segments (rawPathInfo rq)
notFound = responseLBS status404 [] L.empty