rest-wai (empty) → 0.1.0.0
raw patch · 5 files changed
+140/−0 lines, 5 filesdep +basedep +bytestringdep +case-insensitivesetup-changed
Dependencies added: base, bytestring, case-insensitive, containers, http-types, mime-types, mtl, rest-core, rest-types, text, unordered-containers, utf8-string, wai
Files
- CHANGELOG.md +1/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- rest-wai.cabal +38/−0
- src/Rest/Driver/Wai.hs +69/−0
+ CHANGELOG.md view
@@ -0,0 +1,1 @@+# Changelog
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2014, Silk++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.++ * Neither the name of Silk nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"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+OWNER OR CONTRIBUTORS 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
+ rest-wai.cabal view
@@ -0,0 +1,38 @@+name: rest-wai+version: 0.1.0.0+description: Rest driver for WAI applications.+synopsis: Rest driver for WAI applications.+maintainer: code@silk.co+category: Web+build-type: Simple+cabal-version: >= 1.8+license: BSD3+license-file: LICENSE++extra-source-files:+ CHANGELOG.md+ LICENSE++source-repository head+ type: git+ location: https://github.com/silkapp/rest.git++library+ ghc-options: -Wall+ hs-source-dirs: src+ exposed-modules: Rest.Driver.Wai+ build-depends:+ base == 4.*+ , bytestring == 0.10.*+ , case-insensitive == 1.2.*+ , containers >= 0.4 && < 0.6+ , http-types == 0.8.*+ , mime-types == 0.1.*+ , mtl >= 2.0 && < 2.3+ , rest-core >= 0.28 && < 0.31+ , rest-types == 1.10.*+ , text == 1.1.*+ , unordered-containers == 0.2.*+ , utf8-string == 0.3.*+ , wai == 2.1.*+
+ src/Rest/Driver/Wai.hs view
@@ -0,0 +1,69 @@+{-# OPTIONS_GHC -fno-warn-orphans #-}+{-# LANGUAGE GeneralizedNewtypeDeriving, OverloadedStrings, RankNTypes #-}+module Rest.Driver.Wai (apiToApplication) where++import Control.Applicative+import Control.Arrow ((***))+import Control.Monad.Trans (lift)+import Network.HTTP.Types.Status (status200)+import Network.Mime (defaultMimeMap)+import Network.Wai++import qualified Data.ByteString.Char8 as Char8+import qualified Data.ByteString.Lazy as Lazy+import qualified Data.CaseInsensitive as CI+import qualified Data.HashMap.Strict as HashMap+import qualified Data.Map as Map+import qualified Data.Text as Text++import Rest.Api (Api)+import Rest.Driver.Types (Run)+import Rest.Driver.RestM (RestInput(..), RestOutput(..), runRestM)++import qualified Rest.Run as Rest+import qualified Rest.Driver.Types as Rest++apiToApplication :: Run m IO -> Api m -> Application+apiToApplication run api req =+ do ri <- toRestInput req+ (bs, ro) <- runRestM ri (Rest.apiToHandler' (lift . run) api)+ return (fromRestOutput ro bs)++toRestInput :: Request -> IO RestInput+toRestInput req =+ do bs <- lazyRequestBody req+ return $ RestInput+ { headers = HashMap.fromList+ . map (string . CI.original *** string)+ . requestHeaders+ $ req++ , parameters = HashMap.fromList+ . map (string *** maybe "" string)+ . queryString+ $ req++ , body = bs++ , method = case requestMethod req of+ "GET" -> Rest.GET+ "POST" -> Rest.POST+ "PUT" -> Rest.PUT+ "DELETE" -> Rest.DELETE+ other -> Rest.Unknown (string other)++ , paths = text <$> pathInfo req++ , mimeTypes = HashMap.fromList+ . fmap (text *** string)+ $ Map.toList defaultMimeMap+ }+ where string = Char8.unpack+ text = Text.unpack++fromRestOutput :: RestOutput -> Lazy.ByteString -> Response+fromRestOutput (RestOutput hs rc) bs =+ responseLBS (maybe status200 toEnum rc)+ ((CI.mk . Char8.pack *** Char8.pack) <$> HashMap.toList hs)+ bs+