module Web.Routes.Wai where
import qualified Data.ByteString.Char8 as S
import qualified Data.ByteString.Lazy.Char8 as L
import Data.Enumerator (Iteratee)
import Network.Wai ( Application, Request, Response, rawPathInfo
, responseLBS)
import Network.HTTP.Types (status404)
import Web.Routes.Base (decodePathInfo, encodePathInfo)
import Web.Routes.PathInfo (PathInfo(..), fromPathInfo, stripOverlap
, toPathInfoParams)
import Web.Routes.RouteT (RouteT, unRouteT)
import Web.Routes.Site (Site(..))
handleWaiError :: (url -> [(String, String)] -> String)
-> (String -> Either String url)
-> String
-> (String -> Application)
-> ((url -> [(String, String)] -> String) -> url -> Application)
-> Application
handleWaiError fromUrl toUrl approot handleError handler =
\request ->
do let fUrl = toUrl $ stripOverlap approot $ S.unpack $ rawPathInfo request
case fUrl of
(Left parseError) -> handleError parseError request
(Right url) -> handler (\url params -> showString approot $ fromUrl url params) url request
handleWai_ :: (url -> [(String, String)] -> String)
-> (String -> Either String url)
-> String
-> ((url -> [(String, String)] -> String) -> url -> Application)
-> Application
handleWai_ fromUrl toUrl approot handler =
handleWaiError fromUrl toUrl approot handleError handler
where
handleError :: String -> Application
handleError parseError = \_request -> return $ responseLBS status404 [] (L.pack parseError)
handleWai :: (PathInfo url) =>
String
-> ((url -> [(String, String)] -> String) -> url -> Application)
-> Application
handleWai approot handler = handleWai_ toPathInfoParams fromPathInfo approot handler
handleWaiRouteT_ :: (url -> [(String, String)] -> String)
-> (String -> Either String url)
-> String
-> (url -> Request -> RouteT url (Iteratee S.ByteString IO) Response)
-> Application
handleWaiRouteT_ toPathInfo fromPathInfo approot handler =
handleWai_ toPathInfo fromPathInfo approot (\toPathInfo' url request -> unRouteT (handler url request) toPathInfo')
handleWaiRouteT :: (PathInfo url) =>
String
-> (url -> Request -> RouteT url (Iteratee S.ByteString IO) Response)
-> Application
handleWaiRouteT approot handler = handleWaiRouteT_ toPathInfoParams fromPathInfo approot handler
waiSite :: Site url Application
-> String
-> Application
waiSite site approot = handleWai_ formatURL (parsePathSegments site . decodePathInfo) approot (handleSite site)
where
formatURL url params =
let (paths, moreParams) = formatPathSegments site url
in encodePathInfo paths (params ++ moreParams)