packages feed

wai-middleware-route (empty) → 0.1

raw patch · 4 files changed

+108/−0 lines, 4 filesdep +basedep +bytestringdep +http-typessetup-changed

Dependencies added: base, bytestring, http-types, regex-posix, wai

Files

+ LICENSE view
@@ -0,0 +1,25 @@+The following license covers this documentation, and the source code, except+where otherwise indicated.++Copyright 2010, Michael Snoyman. All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++* Redistributions of source code must retain the above copyright notice, this+  list of conditions and the following disclaimer.++* Redistributions in binary form must reproduce the above copyright notice,+  this list of conditions and the following disclaimer in the documentation+  and/or other materials provided with the distribution.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO+EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple
+main = defaultMain
+ src/Network/Wai/Middleware/Route.hs view
@@ -0,0 +1,56 @@+{-# LANGUAGE OverloadedStrings #-}
+
+{- | Route middleware for wai.
+
+It's Heavy inspired by @vhost@ middleware from @wai-extra@ but 
+gives some sugar to life.
+-}
+
+module Network.Wai.Middleware.Route where
+
+import Data.List
+import Data.ByteString (ByteString)
+import Text.Regex.Posix ((=~))
+import Control.Applicative
+import Network.HTTP.Types (Method)
+import Network.Wai
+
+-- | Routing rule 
+type Rule = Request -> Bool
+
+-- | Route for dispatch
+type Route = (Rule, Application)
+
+{- | Dispatch. Seek for first route where the 'Rule' gives a positive result.
+Uses 'find' instead 'filter' in @vhost@
+
+> dispatch [
+>     ((=="/") . rawPathInfo, auth . cache . Issues.get)
+>   , ((=="/issues") . rawPathInfo, cache . About.handle)
+>   ] defaultApp
+-}
+dispatch :: 
+       [Route]      -- ^ List of routes
+    -> Application  -- ^ Default 'Application' 
+    -> Application  -- ^ Returns founded 'Application'. If not found - returns
+                    -- default.
+dispatch routes def req =  
+    case find (\(b, _) -> b req) routes of
+        Nothing -> def req
+        Just (_, app) -> app req
+
+{- | Syntax shugar for most frequently case: HTTP Method and Request path 
+regex pattern.
+
+> ("GET" &~~ "^/issues", app)
+-}
+(&~~) :: 
+       Method       -- ^ HTTP Method 
+    -> ByteString   -- ^ Request path pattern. 
+    -> Rule         -- ^ Routing rule
+infixl 1 &~~
+method &~~ pattern = onPath method $ (=~ pattern) . rawPathInfo
+    where
+        onPath :: Method -> Rule -> Rule       
+        onPath "*" p = p
+        onPath m p = (&&) <$> ((==m) . requestMethod) <*> p
+ wai-middleware-route.cabal view
@@ -0,0 +1,25 @@+name:           wai-middleware-route
+version:        0.1
+cabal-version:  >= 1.8
+build-type:     Simple
+synopsis:       Wai dispatch middleware
+stability:      Experimental
+author:         Alexander Dorofeev
+category:       Web
+license:        BSD3
+description:    Request dispatching Middleware for Wai.
+maintainer:     aka.spin@gmail.com
+license-file:   LICENSE
+homepage:       https://github.com/akaspin/wai-middleware-route
+
+library
+  hs-source-dirs:  src
+  build-depends:   
+                   base >= 4 && < 5,
+                   wai,
+                   bytestring,
+                   regex-posix,
+                   http-types
+  ghc-options:     -Wall
+  exposed-modules: Network.Wai.Middleware.Route
+