web-routes-wai 0.19.2 → 0.20.0
raw patch · 2 files changed
+71/−36 lines, 2 filesdep +enumeratordep +http-typesdep ~bytestringdep ~waidep ~web-routes
Dependencies added: enumerator, http-types
Dependency ranges changed: bytestring, wai, web-routes
Files
- Web/Routes/Wai.hs +64/−34
- web-routes-wai.cabal +7/−2
Web/Routes/Wai.hs view
@@ -2,47 +2,77 @@ import qualified Data.ByteString.Char8 as S import qualified Data.ByteString.Lazy.Char8 as L-import Network.Wai-import Network.Wai.Enumerator-import Web.Routes.Base-import Web.Routes.PathInfo -import Web.Routes.RouteT (RouteT, runRouteT)-import Web.Routes.Site--handleWai_ :: (url -> String) -> (String -> Either String url) -> String -> ((url -> String) -> url -> Application) -> Application-handleWai_ fromUrl toUrl approot handler =- \request ->- do let fUrl = toUrl $ stripOverlap approot $ S.unpack $ pathInfo request- case fUrl of- (Left parseError) -> return $ Response Status404 [] $ Right $ fromLBS (L.pack parseError)- (Right url) -> handler (showString approot . fromUrl) url request+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(..)) -handleWai_1 :: (url -> String) -> (String -> Either String url) -> String -> (String -> Application) -> ((url -> String) -> url -> Application) -> Application-handleWai_1 fromUrl toUrl approot handleError handler =+-- | a low-level function for convert a parser, printer, and routing function into an 'Application'+handleWaiError :: (url -> [(String, String)] -> String) -- ^ function to convert a 'url' + params into path info + query string+ -> (String -> Either String url) -- ^ function to parse path info into 'url'+ -> String -- ^ app root+ -> (String -> Application) -- ^ function to call if there is a decoding error, argument is the parse error+ -> ((url -> [(String, String)] -> String) -> url -> Application) -- ^ routing function+ -> Application+handleWaiError fromUrl toUrl approot handleError handler = \request ->- do let fUrl = toUrl $ stripOverlap approot $ S.unpack $ pathInfo request+ do let fUrl = toUrl $ stripOverlap approot $ S.unpack $ rawPathInfo request case fUrl of (Left parseError) -> handleError parseError request- (Right url) -> handler (showString approot . fromUrl) url request-{- -handleWai_2 :: (url -> String) -> (String -> Failing url) -> String -> ((url -> String) -> url -> Application) -> (Request -> IO (Failing Response))-handleWai_2 fromUrl toUrl approot handler =- \request ->- do let fUrl = toUrl $ stripOverlap approot $ S.unpack $ pathInfo request- case fUrl of- (Failure errs) -> return (Failure errs)- (Success url) -> fmap Success $ handler (showString approot . fromUrl) url request --}+ (Right url) -> handler (\url params -> showString approot $ fromUrl url params) url request -handleWai :: (PathInfo url) => String -> ((url -> String) -> url -> Application) -> Application-handleWai approot handler = handleWai_ toPathInfo fromPathInfo approot handler+-- | a low-level function for convert a parser, printer, and routing function into an 'Application'+--+-- returns 404 if the url parse fails.+handleWai_ :: (url -> [(String, String)] -> String) -- ^ function to convert a 'url' + params into path info + query string+ -> (String -> Either String url) -- ^ function to parse path info into 'url'+ -> String -- ^ app root+ -> ((url -> [(String, String)] -> String) -> url -> Application) -- ^ routing function+ -> 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) -handleWaiRouteT_ :: (url -> String) -> (String -> Either String url) -> String -> (url -> Request -> RouteT url IO Response) -> Application+-- | function to convert a routing function into an Application by+-- leveraging 'PathInfo' to do the url conversion+handleWai :: (PathInfo url) => + String -- ^ approot+ -> ((url -> [(String, String)] -> String) -> url -> Application) -- ^ routing function+ -> Application+handleWai approot handler = handleWai_ toPathInfoParams fromPathInfo approot handler++-- | a function to convert a parser, printer and routing function into an 'Application'.+--+-- This is similar to 'handleWai_' expect that it expects the routing function to use 'RouteT'.+handleWaiRouteT_ :: (url -> [(String, String)] -> String) -- ^ function to convert a 'url' + params into path info + query string+ -> (String -> Either String url) -- ^ function to parse path info into 'url'+ -> String -- ^ app root+ -> (url -> Request -> RouteT url (Iteratee S.ByteString IO) Response) -- ^ routing function+ -> 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 IO Response) -> Application-handleWaiRouteT approot handler = handleWaiRouteT_ toPathInfo fromPathInfo approot handler -waiSite :: Site url Application -> String -> Application-waiSite site approot = handleWai_ (encodePathInfo . formatPathSegments site) (parsePathSegments site . decodePathInfo) approot (handleSite site) +-- | convert a 'RouteT' based routing function into an 'Application' using 'PathInfo' to do the url conversion+handleWaiRouteT :: (PathInfo url) => + String -- ^ app root+ -> (url -> Request -> RouteT url (Iteratee S.ByteString IO) Response) -- ^ routing function+ -> Application+handleWaiRouteT approot handler = handleWaiRouteT_ toPathInfoParams fromPathInfo approot handler++-- |convert a 'Site url Application' into a plain-old 'Application'+waiSite :: Site url Application -- ^ Site+ -> String -- ^ approot, e.g. http://www.example.org/app+ -> 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)
web-routes-wai.cabal view
@@ -1,5 +1,5 @@ Name: web-routes-wai-Version: 0.19.2+Version: 0.20.0 License: BSD3 License-File: LICENSE Author: jeremy@seereason.com@@ -12,7 +12,12 @@ Build-type: Simple Library- Build-Depends: base >= 4 && < 5, bytestring, wai, web-routes >= 0.22+ Build-Depends: base >= 4 && < 5,+ bytestring == 0.9.*,+ enumerator == 0.4.*,+ http-types == 0.6.*,+ wai == 0.4.*,+ web-routes >= 0.25.2 && < 0.26 Exposed-Modules: Web.Routes.Wai source-repository head