1{-# LANGUAGE DataKinds #-}
2{-# LANGUAGE DeriveGeneric #-}
3{-# LANGUAGE TypeOperators #-}
4module Main (main) where
9import Control.Concurrent
11import Control.Monad.IO.Class
13import qualified Data.ByteString as BS
16import Network.HTTP.Client
17 (defaultManagerSettings, newManager)
20import System.Environment
27import qualified Pipes as P
28import Pipes.ByteString as PBS
29import qualified Pipes.Prelude as P
32import qualified Pipes.Safe.Prelude as P
34import Servant.Client.Streaming
35import Servant.Pipes ()
37import qualified Network.Wai.Handler.Warp as Warp
39type FastAPI = "get" :> Capture "num" Int :> StreamGet NewlineFraming JSON (P.Producer Int IO ())
41-- TODO: Change IO to something with MonadError ServantUnrenderError
43 :<|> "slow" :> Capture "num" Int :> StreamGet NewlineFraming JSON (P.Producer Int IO ())
44 -- monad can be SafeT IO too.
45 :<|> "readme" :> StreamGet NoFraming OctetStream (P.Producer BS.ByteString (SafeT IO) ())
46 -- we can have streaming request body
48 :> StreamBody NoFraming OctetStream (P.Producer BS.ByteString IO ())
49 :> StreamPost NoFraming OctetStream (P.Producer BS.ByteString IO ())
55server = fast :<|> slow :<|> readme :<|> proxy
58 putStrLn ("/get/" ++ show n)
62 putStrLn ("/slow/" ++ show n)
67 return $ P.withFile "README.md" ReadMode PBS.fromHandle
73 -- for some reason unfold leaks?
76 | otherwise = P.yield m >> fastPipe (m - 1)
78 slowPipe m = fastPipe m P.>-> P.mapM (<$ threadDelay 1000000)
83cli :: Client ClientM FastAPI
84cli :<|> _ :<|> _ :<|> _ = client api
91 putStrLn "Starting servant-pipes:example at http://localhost:8000"
92 port <- fromMaybe 8000 . (>>= readMaybe) <$> lookupEnv "PORT"
95 n <- maybe (fail $ "not a number: " ++ ns) pure $ readMaybe ns
96 mgr <- newManager defaultManagerSettings
97 burl <- parseBaseUrl "http://localhost:8000/"
98 withClientM (cli n) (mkClientEnv mgr burl) $ \me -> case me of
101 x <- P.fold (\c _ -> c + 1) (0 :: Int) id p
105 putStrLn "cabal new-run servant-pipes:example server"
106 putStrLn "cabal new-run servant-pipes:example client 10"
107 putStrLn "time curl -H 'Accept: application/json' localhost:8000/slow/5"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list