Skip to content

Commit 199c96f

Browse files
committed
Simplified demo
1 parent d063ded commit 199c96f

File tree

7 files changed

+36
-42
lines changed

7 files changed

+36
-42
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@ before_install:
6767
fi
6868

6969
if [ "$AESONVER" = "0.6.0.0" ]; then
70-
echo "constraints:attoparsec==0.8.6.1,blaze-builder==0.2.1.4,bytestring==0.9.1.8,happstack-server==6.2.4,hashable==1.1.2.0,mtl==1.1.1.0,network==2.3.0.1,test-framework-hunit==0.3.0,text==0.11.1.1,unordered-containers==0.1.3.0,vector==0.7.1,zlib==0.5.2.0" >> cabal.config;
71-
fi
72-
73-
if [[ $HPVER == 2012.* ]]; then
74-
echo "constraints:happstack-server==7.3.0" >> cabal.config;
70+
echo "constraints:attoparsec==0.8.6.1,blaze-builder==0.2.1.4,bytestring==0.9.1.8,hashable==1.1.2.0,mtl==1.1.1.0,network==2.3.0.1,test-framework-hunit==0.3.0,text==0.11.1.1,unordered-containers==0.1.3.0,vector==0.7.1,zlib==0.5.2.0" >> cabal.config;
7571
fi
7672

7773
- sudo add-apt-repository -y ppa:hvr/ghc

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ json-rpc-server
22
===============
33
[![Build Status](https://travis-ci.org/grayjay/json-rpc-server.svg?branch=master)](https://travis-ci.org/grayjay/json-rpc-server)
44

5-
An implementation of the server side of JSON RPC 2.0. See <http://www.jsonrpc.org/specification>. This library uses ByteString for input and output, leaving the choice of transport up to the user. The documentation is on Hackage: <http://hackage.haskell.org/package/json-rpc-server>.
5+
An implementation of the server side of JSON-RPC 2.0. See <http://www.jsonrpc.org/specification>. This library uses ByteString for input and output, leaving the choice of transport up to the user. The documentation is on Hackage: <http://hackage.haskell.org/package/json-rpc-server>.

demo/Demo.hs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,35 @@
33
module Main (main) where
44

55
import Network.JsonRpc.Server
6-
import Happstack.Server.SimpleHTTP( ServerPartT, simpleHTTP, nullConf
7-
, askRq, rqBody, unBody, toResponse)
6+
import qualified Data.ByteString.Lazy.Char8 as B
87
import Data.List (intercalate)
98
import Data.Maybe (fromMaybe)
10-
import Control.Monad (when)
9+
import Control.Monad (forM_, when)
1110
import Control.Monad.Trans (liftIO)
1211
import Control.Monad.Error (throwError)
1312
import Control.Monad.Reader (ReaderT, ask, runReaderT)
14-
import Control.Concurrent.MVar (MVar, newMVar, readMVar, modifyMVar)
13+
import Control.Concurrent.MVar (MVar, newMVar, modifyMVar)
1514

1615
main :: IO ()
17-
main = newMVar 0 >>= \count ->
18-
simpleHTTP nullConf $ do
19-
request <- askRq
20-
body <- liftIO $ getBody request
21-
result <- runReaderT (call methods body) count
22-
let resultStr = fromMaybe "" result
23-
return $ toResponse resultStr
24-
where getBody r = unBody `fmap` readMVar (rqBody r)
16+
main = do
17+
contents <- B.getContents
18+
count <- newMVar 0
19+
forM_ (B.lines contents) $ \request -> do
20+
response <- runReaderT (call methods request) count
21+
B.putStrLn $ fromMaybe "" response
2522

26-
type Server = ReaderT (MVar Integer) (ServerPartT IO)
23+
type Server = ReaderT (MVar Integer) IO
2724

2825
methods :: Methods Server
29-
methods = toMethods [printSequence, getCount, add]
26+
methods = toMethods [add, printSequence, increment]
3027

31-
printSequence, getCount, add :: Method Server
28+
add, printSequence, increment :: Method Server
3229

33-
printSequence = toMethod "print" f params
30+
add = toMethod "add" f (Required "x" :+: Required "y" :+: ())
31+
where f :: Double -> Double -> RpcResult Server Double
32+
f x y = liftIO $ return (x + y)
33+
34+
printSequence = toMethod "print_sequence" f params
3435
where params = Required "string" :+:
3536
Optional "count" 1 :+:
3637
Optional "separator" ',' :+: ()
@@ -40,11 +41,7 @@ printSequence = toMethod "print" f params
4041
liftIO $ print $ intercalate [sep] $ replicate count str
4142
negativeCount = rpcError (-32000) "negative count"
4243

43-
getCount = toMethod "get_count" f ()
44+
increment = toMethod "increment_and_get_count" f ()
4445
where f :: RpcResult Server Integer
4546
f = ask >>= \count -> liftIO $ modifyMVar count inc
4647
where inc x = return (x + 1, x + 1)
47-
48-
add = toMethod "add" f (Required "x" :+: Required "y" :+: ())
49-
where f :: Double -> Double -> RpcResult Server Double
50-
f x y = liftIO $ return (x + y)

json-rpc-server.cabal

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ license: MIT
77
license-file: LICENSE
88
category: Network, JSON
99
maintainer: Kristen Kozak <[email protected]>
10-
synopsis: JSON RPC 2.0 on the server side.
10+
synopsis: JSON-RPC 2.0 on the server side.
1111
build-type: Simple
1212
extra-source-files: README.md
1313
cabal-version: >=1.8
1414
tested-with: GHC == 7.0.1, GHC == 7.4.1, GHC == 7.6.2, GHC == 7.6.3, GHC == 7.8.3
15-
description: An implementation of the server side of JSON RPC 2.0.
15+
description: An implementation of the server side of JSON-RPC 2.0.
1616
See <http://www.jsonrpc.org/specification>. This
1717
library uses 'ByteString' for input and output,
1818
leaving the choice of transport up to the user.
@@ -23,7 +23,7 @@ source-repository head
2323
location: https://github.com/grayjay/json-rpc-server
2424

2525
flag demo
26-
description: Builds the demo Happstack JSON RPC server.
26+
description: Builds the JSON-RPC demo.
2727
default: False
2828
manual: True
2929

@@ -46,8 +46,8 @@ executable demo
4646
if flag (demo)
4747
build-depends: base >=4.3 && <4.8,
4848
json-rpc-server,
49-
mtl >=1.1.1 && <2.3,
50-
happstack-server >=6.2.4 && <7.4
49+
bytestring >=0.9 && <0.11,
50+
mtl >=1.1.1 && <2.3
5151
ghc-options: -Wall
5252
else
5353
buildable: False

src/Network/JsonRpc/Server.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{-# OPTIONS_GHC -fno-warn-deprecations #-}
99
#endif
1010

11-
-- | Functions for implementing the server side of JSON RPC 2.0.
11+
-- | Functions for implementing the server side of JSON-RPC 2.0.
1212
-- See <http://www.jsonrpc.org/specification>.
1313
module Network.JsonRpc.Server (
1414
-- ** Instructions
@@ -66,8 +66,9 @@ import Control.Monad.Error (runErrorT, throwError)
6666
-- optional parameters.
6767

6868
-- $example
69-
-- Here is an example of a simple Happstack server with three methods.
70-
-- Compile it with the build flag @demo@.
69+
-- Here is an example with three JSON-RPC methods. It reads requests
70+
-- from stdin and writes responses to stdout. Compile it with the
71+
-- build flag @demo@.
7172
--
7273
-- > <insert Demo.hs>
7374
--
@@ -83,21 +84,21 @@ toMethods :: [Method m] -> Methods m
8384
toMethods fs = Methods $ H.fromList $ map pair fs
8485
where pair mth@(Method name _) = (name, mth)
8586

86-
-- | Handles one JSON RPC request. It is the same as
87+
-- | Handles one JSON-RPC request. It is the same as
8788
-- @callWithBatchStrategy sequence@.
8889
call :: Monad m => Methods m -- ^ Choice of methods to call.
89-
-> B.ByteString -- ^ JSON RPC request.
90+
-> B.ByteString -- ^ JSON-RPC request.
9091
-> m (Maybe B.ByteString) -- ^ The response wrapped in 'Just', or
9192
-- 'Nothing' in the case of a notification,
9293
-- all wrapped in the given monad.
9394
call = callWithBatchStrategy sequence
9495

95-
-- | Handles one JSON RPC request.
96+
-- | Handles one JSON-RPC request.
9697
callWithBatchStrategy :: Monad m =>
9798
(forall a . [m a] -> m [a]) -- ^ Function specifying the
9899
-- evaluation strategy.
99100
-> Methods m -- ^ Choice of methods to call.
100-
-> B.ByteString -- ^ JSON RPC request.
101+
-> B.ByteString -- ^ JSON-RPC request.
101102
-> m (Maybe B.ByteString) -- ^ The response wrapped in 'Just', or
102103
-- 'Nothing' in the case of a notification,
103104
-- all wrapped in the given monad.
@@ -139,7 +140,7 @@ lookupMethod name = maybe notFound return . H.lookup name
139140
where notFound = throwError $ rpcError (-32601) $ "Method not found: " `append` name
140141

141142
throwInvalidRpc :: Monad m => Text -> RpcResult m a
142-
throwInvalidRpc = throwError . rpcErrorWithData (-32600) "Invalid JSON RPC 2.0 request"
143+
throwInvalidRpc = throwError . rpcErrorWithData (-32600) "Invalid JSON-RPC 2.0 request"
143144

144145
batchCall :: Monad m => (forall a. [m a] -> m [a])
145146
-> Methods m

src/Network/JsonRpc/Types.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ instance A.FromJSON Request where
121121
parseParams (A.Array ar) = return $ Right ar
122122
parseParams _ = empty
123123
checkVersion ver = when (ver /= jsonRpcVersion) $
124-
fail $ "Wrong JSON RPC version: " ++ unpack ver
124+
fail $ "Wrong JSON-RPC version: " ++ unpack ver
125125
-- (.:?) parses Null value as Nothing so parseId needs
126126
-- to use both (.:?) and (.:) to handle all cases
127127
parseId = x .:? idKey >>= \optional ->

tests/TestSuite.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ errorHandlingTests :: [Test]
3232
errorHandlingTests = [ testCase "invalid JSON" $
3333
assertSubtractResponse (A.String "5") $ nullIdErrRsp (-32700)
3434

35-
, testCase "invalid JSON RPC" $
35+
, testCase "invalid JSON-RPC" $
3636
assertSubtractResponse (A.object ["id" .= A.Number 10]) $ nullIdErrRsp (-32600)
3737

3838
, testCase "empty batch call" $

0 commit comments

Comments
 (0)