mirror of
https://github.com/unclechu/gRPC-haskell.git
synced 2024-11-27 05:29:43 +01:00
169dbb7fff
* add basic tutorial, re-export client stuff in HighLevel.Generated * add a relative link in README.md * forgot to document the language extensions * cabal file fixup * more context to compile-proto-file * haskell syntax highlighting in markdown * link to gRPC official tutorials for basic concepts * add a note on how to build the examples * prominent notice of required gRPC version * fix typo * do some error handling, show how to run the example executables * use mapM
50 lines
1.8 KiB
Haskell
50 lines
1.8 KiB
Haskell
{-# LANGUAGE DataKinds #-}
|
|
{-# LANGUAGE GADTs #-}
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE BangPatterns #-}
|
|
|
|
import Arithmetic
|
|
import Network.GRPC.HighLevel.Generated
|
|
|
|
import Data.String (fromString)
|
|
|
|
handlers :: Arithmetic ServerRequest ServerResponse
|
|
handlers = Arithmetic { arithmeticAdd = addHandler
|
|
, arithmeticRunningSum = runningSumHandler
|
|
}
|
|
|
|
addHandler :: ServerRequest 'Normal TwoInts OneInt
|
|
-> IO (ServerResponse 'Normal OneInt)
|
|
addHandler (ServerNormalRequest _metadata (TwoInts x y)) = do
|
|
let answer = OneInt (x + y)
|
|
return (ServerNormalResponse answer
|
|
[("metadata_key_one", "metadata_value")]
|
|
StatusOk
|
|
"addition is easy!")
|
|
|
|
|
|
runningSumHandler :: ServerRequest 'ClientStreaming OneInt OneInt
|
|
-> IO (ServerResponse 'ClientStreaming OneInt)
|
|
runningSumHandler req@(ServerReaderRequest metadata recv) =
|
|
loop 0
|
|
where loop !i =
|
|
do msg <- recv
|
|
case msg of
|
|
Left err -> return (ServerReaderResponse
|
|
Nothing
|
|
[]
|
|
StatusUnknown
|
|
(fromString (show err)))
|
|
Right (Just (OneInt x)) -> loop (i + x)
|
|
Right Nothing -> return (ServerReaderResponse
|
|
(Just (OneInt i))
|
|
[]
|
|
StatusOk
|
|
"")
|
|
|
|
options :: ServiceOptions
|
|
options = defaultServiceOptions
|
|
|
|
main :: IO ()
|
|
main = arithmeticServer handlers options
|