-- | matching a list of regexp agains a path_info, if matched, consponding app
--   is used, otherwise, pass the env down to lower middleware

module Hack.Contrib.Middleware.RegexpRouter (regexp_router) where

import Data.Maybe
import Hack
import Hack.Contrib.Utils
import List (find)
import MPS
import Prelude hiding ((.), (^), (>))

type RoutePath = (String, Application)

regexp_router :: [RoutePath] -> Middleware
regexp_router h app = \env ->
  let path = env.path_info
  case h.find (fst > flip match path > isJust) of
    Nothing -> app env
    Just (_, found_app) -> found_app env