packages feed

yaml-rpc-snap (empty) → 1.0.3

raw patch · 6 files changed

+158/−0 lines, 6 filesdep +aesondep +basedep +bytestringsetup-changed

Dependencies added: aeson, base, bytestring, containers, http-types, snap, text, transformers, yaml, yaml-rpc

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2012, IlyaPortnov++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 IlyaPortnov 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.
+ Network/YAML/Snap.hs view
@@ -0,0 +1,35 @@+{-# LANGUAGE OverloadedStrings #-}++module Network.YAML.Snap (handleApi, handleApiPost) where++import qualified Data.ByteString as B+import qualified Data.Text.Encoding as TE+import Network.YAML+import qualified Data.Aeson as Json+import Snap++errorMsg :: Int -> B.ByteString -> Snap ()+errorMsg status msg = do+  modifyResponse $ setResponseStatus status msg+  writeBS msg+  finishWith =<< getResponse++-- | Snap handler for POST method+handleApiPost :: Dispatcher IO -> Snap ()+handleApiPost dispatcher = method POST $ handleApi dispatcher++-- | Snap handler for any method+handleApi :: Dispatcher IO -> Snap ()+handleApi dispatcher = do+  maybeMethod <- getParam "method"+  case maybeMethod of+    Nothing -> errorMsg 400 "No method name specified"+    Just methodName -> case dispatcher (TE.decodeUtf8 methodName) of+                         Nothing -> errorMsg 404 "No such method"+                         Just method -> do+                          body <- readRequestBody 16384+                          case Json.decode body of+                            Nothing -> errorMsg 400 "Invalid JSON in request"+                            Just json -> do+                              result <- liftIO $ method json+                              writeLBS $ Json.encode result
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ Test/Server.hs view
@@ -0,0 +1,21 @@+{-# LANGUAGE TemplateHaskell, OverloadedStrings, DeriveGeneric, StandaloneDeriving #-}++module Test.Server where++import Data.Monoid+import qualified Data.ByteString as B+import Snap++import Network.YAML+import Network.YAML.Snap++import Test.TestAPIImpl++$(generateDispatcher api)++main :: IO ()+main = httpServe (setPort 3000 mempty) site++site :: Snap ()+site = route [("/:method", handleApiPost dispatcher)]+
+ Test/TestAPIImpl.hs view
@@ -0,0 +1,33 @@+{-# LANGUAGE TemplateHaskell, OverloadedStrings, DeriveGeneric, StandaloneDeriving #-}++module Test.TestAPIImpl where++import GHC.Generics+import qualified Data.Text as T+import Data.Aeson++import Network.YAML++data User = User {fullName :: T.Text, login :: T.Text}+  deriving (Eq, Show)++deriving instance Generic User+instance FromJSON User+instance ToJSON User++data Something = Something {smthText :: T.Text, smthList :: [T.Text]}+  deriving (Eq, Show, Generic)++instance FromJSON Something+instance ToJSON Something++sayHello :: User -> IO T.Text+sayHello user = return $ "Hello, " `T.append` fullName user `T.append` "!"++testSmth :: T.Text -> Something -> IO User+testSmth t s = return $ User {login = t `T.append` smthText s, fullName = T.intercalate " " (smthList s)}++api :: API+api = $(makeAPI "http://home.iportnov.ru/test.api" [''User, ''Something] ['sayHello, 'testSmth])++$(writeAPI "test.api" "http://home.iportnov.ru/test.api" [''User, ''Something] ['sayHello, 'testSmth])
+ yaml-rpc-snap.cabal view
@@ -0,0 +1,37 @@+Name:                yaml-rpc-snap++Version:             1.0.3++Synopsis:            Snap server backend for yaml-rpc+Description:         This package provides Snap-based server backend for yaml-rpc package.+Homepage:            http://redmine.iportnov.ru/projects/yaml-rpc+License:             BSD3+License-file:        LICENSE+Author:              Ilya V. Portnov+Maintainer:          portnov84@rambler.ru+Category:            Network+Build-type:          Simple+Cabal-version:       >= 1.8++Library+  Exposed-modules:   Network.YAML.Snap+  Build-depends:       base >= 3 && <= 5,+                       transformers >= 0.3.0.0,+                       bytestring >= 0.10.0.2,+                       aeson >= 0.7.0.6,+                       yaml,+                       containers,+                       text >= 1.1.1.3,+                       http-types >= 0.8.5,+                       snap >= 0.13.2.7,+                       yaml-rpc >= 1.0.3++  Other-modules:     Test.Server,+                     Test.TestAPIImpl++Source-repository head+  type: git+  location: git@github.com:portnov/yaml-rpc.git+++