waitra (empty) → 0.0.1.0
raw patch · 5 files changed
+160/−0 lines, 5 filesdep +basedep +http-typesdep +regex-applicativesetup-changed
Dependencies added: base, http-types, regex-applicative, text, wai
Files
- LICENSE +20/−0
- README.md +18/−0
- Setup.hs +2/−0
- src/Network/Waitra.hs +87/−0
- waitra.cabal +33/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Futurice++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,18 @@+# Waitra++Waitra is a very simple router.+It's useful for writing simple API web-services,+when you don't want to use the whole Yesod stack.++[](https://travis-ci.org/futurice/waitra)++## Synopsis++```hs+echoRoute :: Route+echoRoute = routeGet (string "/api/echo/" *> many anySym) echoApp+ where echoApp msg _req respond = respond $ responseLBS status200 [] (fromString msg)++app :: Application+app = compile [echoRoute] fallbackApp+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Network/Waitra.hs view
@@ -0,0 +1,87 @@+{-# LANGUAGE GADTs #-}+{-# LANGUAGE RankNTypes #-}+{-# LANGUAGE OverloadedStrings #-}+-----------------------------------------------------------------------------+-- |+-- Module : Network.Waitra+-- Copyright : (c) 2015 Futurice+-- License : MIT (see the file LICENSE)+-- Maintainer : Oleg Grenrus <oleg.grenrus@iki.fi>+-- Stability : experimental+-- Portability : GADTs and RankNTypes+--+-- @Network.Waitra@ is a very simple router.+-- It's useful for writing simple API web-services,+-- when you don't want to use the whole Yesod stack.+----------------------------------------------------------------------------+module Network.Waitra+ (+ -- * Types+ Path+ , Route(..)+ -- * Static paths routes+ , simpleRoute+ , simpleGet+ , simplePost+ , simplePut+ , simpleDelete+ -- * Regex paths routes+ , routeGet+ , routePost+ , routePut+ , routeDelete+ -- * Compilation+ , compile+ , compileRoute+ ) where++import qualified Data.Text as T+import qualified Network.HTTP.Types as H+import Network.Wai+import Text.Regex.Applicative++-- | We use strings, as - unluckily - `Text.Regex.Applicative` doesn't work with `Text` directly.+type Path = String++data Route where+ Route :: forall a. H.Method -> RE Char a -> (a -> Application) -> Route++simpleRoute :: H.Method -> Path -> Application -> Route+simpleRoute method r app = Route method (string r) (const app)++simpleGet :: Path -> Application -> Route+simpleGet = simpleRoute H.methodGet++simplePost :: Path -> Application -> Route+simplePost = simpleRoute H.methodPost++simplePut :: Path -> Application -> Route+simplePut = simpleRoute H.methodPut++simpleDelete :: Path -> Application -> Route+simpleDelete = simpleRoute H.methodDelete++routeGet :: RE Char a -> (a -> Application) -> Route+routeGet = Route H.methodGet++routePost :: RE Char a -> (a -> Application) -> Route+routePost = Route H.methodPost++routeDelete :: RE Char a -> (a -> Application) -> Route+routeDelete = Route H.methodDelete++routePut :: RE Char a -> (a -> Application) -> Route+routePut = Route H.methodPut++path :: Request -> Path+path req = T.unpack . T.intercalate "/" $ "" : pathInfo req++compileRoute :: Route -> Middleware+compileRoute (Route method re routeApp) app req =+ case (requestMethod req == method, path req =~ re) of+ (True, Just m') -> routeApp m' req+ _ -> app req++-- | Turn the list of routes into `Middleware`+compile :: [Route] -> Middleware+compile = foldr (.) id . map compileRoute
+ waitra.cabal view
@@ -0,0 +1,33 @@+name: waitra+version: 0.0.1.0+synopsis: A very simple Wai router+description:+ Waitra is a very simple router.+ It's useful for writing simple API web-services,+ when you don't want to use the whole Yesod stack.+homepage: https://github.com/futurice/waitra+license: MIT+license-file: LICENSE+author: Oleg Grenrus <oleg.grenrus@iki.fi>+maintainer: Oleg Grenrus <oleg.grenrus@iki.fi>+copyright: (c) 2015 Futurice+category: Web+build-type: Simple+extra-source-files: README.md+cabal-version: >=1.10++library+ exposed-modules: Network.Waitra+ other-extensions: RankNTypes, GADTs, OverloadedStrings+ build-depends: base >=4.6 && <4.9,+ http-types >=0.8.6,+ regex-applicative >=0.3.1,+ text >=1.1.0.0,+ wai >=3.0.2.3+ hs-source-dirs: src+ ghc-options: -Wall+ default-language: Haskell2010++source-repository head+ type: git+ location: git://github.com/futurice/waitra.git