module Network.Wai.Middleware.Router (Route,router,dir) where
import Data.Text (Text,splitOn)
import Network.Wai (Request(..),Application)
type Route = ([Text] -> Maybe Application)
router :: [Route] -> Application -> Application
router routes d req = case router' (pathInfo req) routes of
Nothing -> d req
Just a -> a req
router' :: [Text] -> [Route] -> Maybe Application
router' _ [] = Nothing
router' ps (r:rs) = case r ps of
Nothing -> router' ps rs
Just a -> Just a
dir :: Text -> Application -> Route
dir path a = let ps = pathPieces path
in ( \xs -> if ps == xs
then Just a
else Nothing )
pathPieces :: Text -> [Text]
pathPieces path = filter (""/=) $ splitOn "/" path