rest-snap (empty) → 0.1.17.4
raw patch · 4 files changed
+118/−0 lines, 4 filesdep +basedep +bytestringdep +case-insensitivesetup-changed
Dependencies added: base, bytestring, case-insensitive, containers, fclabels, hxt, json, json-schema, mtl, regular-xmlpickler, rest-core, rest-types, safe, snap-core, split, text, transformers, unordered-containers, uri-encode, utf8-string
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- rest-snap.cabal +42/−0
- src/Rest/Driver/Snap.hs +44/−0
+ 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-snap.cabal view
@@ -0,0 +1,42 @@+Name: rest-snap+Version: 0.1.17.4+Description: Rest driver for Snap.+Synopsis: Rest driver for Snap.+Maintainer: code@silk.co+Category: Web+Build-Type: Simple+Cabal-Version: >= 1.8+License: BSD3+License-File: LICENSE++Library+ GHC-Options: -Wall+ Hs-Source-Dirs: src+ Build-Depends:++ base == 4.*+ , bytestring >= 0.9 && < 0.11+ , case-insensitive >= 0.4 && < 1.2+ , containers >= 0.4 && < 0.6+ , snap-core == 0.9.*+ , hxt >= 9.2 && < 9.4+ , fclabels == 2.0.*+ , json >= 0.4 && < 0.8+ , json-schema == 0.4.*+ , mtl >= 2.0 && < 2.2+ , regular-xmlpickler == 0.2.*+ , rest-types == 1.9.0.1+ , safe >= 0.2 && < 0.4+ , split >= 0.1 && < 0.3+ , text == 0.11.*+ , transformers == 0.3.*+ , unordered-containers == 0.2.*+ , uri-encode == 1.5.*+ , utf8-string == 0.3.*+ , rest-core == 0.27.*++ Exposed-Modules: Rest.Driver.Snap++Source-repository head+ Type: Git+ Location: https://github.com/silkapp/rest.git
+ src/Rest/Driver/Snap.hs view
@@ -0,0 +1,44 @@+{-# OPTIONS_GHC -fno-warn-orphans #-}+{-# LANGUAGE RankNTypes #-}+module Rest.Driver.Snap (apiToHandler, apiToHandler') where++import Safe+import Snap.Core+import Snap.Util.FileServe (defaultMimeTypes)++import qualified Data.ByteString.Char8 as Char8+import qualified Data.ByteString.UTF8 as UTF8+import qualified Data.CaseInsensitive as CI+import qualified Data.HashMap.Strict as M+import qualified Network.URI.Encode as URI+import qualified Snap.Core as Snap++import Rest.Api (Api)+import Rest.Driver.Perform (Rest (..))+import Rest.Driver.Types (Run)++import qualified Rest.Run as Rest+import qualified Rest.Driver.Types as Rest++apiToHandler :: Api Snap -> Snap ()+apiToHandler = apiToHandler' id++apiToHandler' :: Run m Snap -> Api m -> Snap ()+apiToHandler' run api = Rest.apiToHandler' run api >>= writeLBS++instance Rest Snap where+ getHeader nm = getsRequest (fmap UTF8.toString . Snap.getHeader (CI.mk . UTF8.fromString $ nm))+ getParameter nm = getsRequest (fmap UTF8.toString . (>>= headMay) . rqParam (UTF8.fromString nm))+ getBody = readRequestBody (1 * 1024 * 1024)+ getMethod = getsRequest (toRestMethod . rqMethod)+ getPaths = getsRequest (map (UTF8.toString . URI.decodeByteString) . Char8.split '/' . rqPathInfo)+ lookupMimeType = return . fmap UTF8.toString . flip M.lookup defaultMimeTypes+ setHeader nm v = modifyResponse (Snap.setHeader (CI.mk . UTF8.fromString $ nm) (UTF8.fromString v))+ setResponseCode cd = modifyResponse (Snap.setResponseCode cd)++toRestMethod :: Snap.Method -> Rest.Method+toRestMethod Snap.GET = Rest.GET+toRestMethod Snap.POST = Rest.POST+toRestMethod Snap.PUT = Rest.PUT+toRestMethod Snap.DELETE = Rest.DELETE+toRestMethod mthd = Rest.Unknown (show mthd)