-
-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add servant-io-streams #1660
Open
shlevy
wants to merge
2
commits into
haskell-servant:master
Choose a base branch
from
shlevy:servant-io-streams
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add servant-io-streams #1660
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
synopsis: Add servant-io-streams package | ||
prs: #1660 | ||
|
||
description: { | ||
Instances of `ToSourceIO` and `FromSourceIO` for `InputStream` from `io-streams`. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Copyright (c) 2023, Servant Contributors | ||
|
||
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 Servant Contributors 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# servant-io-streams - Servant Stream support for io-streams | ||
|
||
![servant](https://raw.githubusercontent.com/haskell-servant/servant/master/servant.png) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Distribution.Simple | ||
main = defaultMain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
module Main (main) where | ||
|
||
import Prelude () | ||
import Prelude.Compat | ||
|
||
import Control.Concurrent | ||
(threadDelay) | ||
import Control.Monad.IO.Class | ||
(MonadIO (..)) | ||
import qualified Data.ByteString as BS | ||
import Data.Maybe | ||
(fromMaybe) | ||
import Network.HTTP.Client | ||
(defaultManagerSettings, newManager) | ||
import System.Environment | ||
(getArgs, lookupEnv) | ||
import System.IO | ||
(IOMode (..), openFile, hClose) | ||
import Text.Read | ||
(readMaybe) | ||
|
||
import qualified System.IO.Streams as IOS | ||
import System.IO.Streams.Combinators | ||
(atEndOfInput) | ||
import System.IO.Streams.Handle | ||
(handleToInputStream) | ||
import Servant | ||
import Servant.Client.Streaming | ||
import Servant.IO.Streams () | ||
|
||
import qualified Network.Wai.Handler.Warp as Warp | ||
|
||
type FastAPI = "get" :> Capture "num" Int :> StreamGet NewlineFraming JSON (IOS.InputStream Int) | ||
|
||
type API = FastAPI | ||
:<|> "slow" :> Capture "num" Int :> StreamGet NewlineFraming JSON (IOS.InputStream Int) | ||
:<|> "readme" :> StreamGet NoFraming OctetStream (IOS.InputStream BS.ByteString) | ||
-- we can have streaming request body | ||
:<|> "proxy" | ||
:> StreamBody NoFraming OctetStream (IOS.InputStream BS.ByteString) | ||
:> StreamPost NoFraming OctetStream (IOS.InputStream BS.ByteString) | ||
|
||
api :: Proxy API | ||
api = Proxy | ||
|
||
server :: Server API | ||
server = fast :<|> slow :<|> readme :<|> proxy | ||
where | ||
fast n = liftIO $ do | ||
putStrLn ("/get/" ++ show n) | ||
IOS.fromGenerator $ fastGenerator n | ||
|
||
slow n = liftIO $ do | ||
putStrLn ("/slow/" ++ show n) | ||
IOS.fromGenerator $ slowGenerator n | ||
|
||
readme = liftIO $ do | ||
putStrLn "/readme" | ||
h <- openFile "README.md" ReadMode | ||
is <- handleToInputStream h | ||
atEndOfInput (hClose h) is | ||
|
||
proxy c = liftIO $ do | ||
putStrLn "/proxy" | ||
return c | ||
|
||
fastGenerator n | ||
| n < 0 = return () | ||
| otherwise = IOS.yield n >> fastGenerator (n - 1) | ||
|
||
slowGenerator n | ||
| n < 0 = return () | ||
| otherwise = IOS.yield n >> liftIO (threadDelay 1000000) >> slowGenerator (n - 1) | ||
|
||
app :: Application | ||
app = serve api server | ||
|
||
cli :: Client ClientM FastAPI | ||
cli :<|> _ :<|> _ :<|> _ = client api | ||
|
||
main :: IO () | ||
main = do | ||
args <- getArgs | ||
case args of | ||
("server":_) -> do | ||
putStrLn "Starting servant-io-streams:example at http://localhost:8000" | ||
port <- fromMaybe 8000 . (>>= readMaybe) <$> lookupEnv "PORT" | ||
Warp.run port app | ||
("client":ns:_) -> do | ||
n <- maybe (fail $ "not a number: " ++ ns) pure $ readMaybe ns | ||
mgr <- newManager defaultManagerSettings | ||
burl <- parseBaseUrl "http://localhost:8000/" | ||
withClientM (cli n) (mkClientEnv mgr burl) $ \me -> case me of | ||
Left err -> print err | ||
Right s -> do | ||
x <- IOS.fold (\c _ -> c + 1) (0 :: Int) s | ||
print x | ||
_ -> do | ||
putStrLn "Try:" | ||
putStrLn "cabal new-run servant-io-streams:example server" | ||
putStrLn "cabal new-run servant-io-streams:example client 10" | ||
putStrLn "time curl -H 'Accept: application/json' localhost:8000/slow/5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
cabal-version: 2.2 | ||
name: servant-io-streams | ||
version: 0.1 | ||
|
||
synopsis: Servant Stream support for io-streams | ||
category: Servant, Web, io-streams | ||
description: Servant Stream support for io-streams. | ||
. | ||
Provides 'ToSourceIO' and 'FromSourceIO' instances for 'InputStream'. | ||
|
||
homepage: http://docs.servant.dev/ | ||
bug-reports: http://github.com/haskell-servant/servant/issues | ||
license: BSD-3-Clause | ||
license-file: LICENSE | ||
author: Servant Contributors | ||
maintainer: [email protected] | ||
copyright: 2023 Servant Contributors | ||
build-type: Simple | ||
tested-with: GHC ==8.6.5 || ==8.8.4 || ==8.10.2 | ||
|
||
extra-source-files: | ||
CHANGELOG.md | ||
|
||
source-repository head | ||
type: git | ||
location: http://github.com/haskell-servant/servant.git | ||
|
||
library | ||
exposed-modules: Servant.IO.Streams | ||
build-depends: | ||
base >=4.9 && <5 | ||
, io-streams ^>=1.5 | ||
, servant >=0.15 && <0.20 | ||
hs-source-dirs: src | ||
default-language: Haskell2010 | ||
ghc-options: -Wall | ||
|
||
test-suite example | ||
type: exitcode-stdio-1.0 | ||
main-is: Main.hs | ||
hs-source-dirs: | ||
example | ||
ghc-options: -Wall -rtsopts -threaded | ||
build-depends: | ||
base | ||
, base-compat | ||
, bytestring | ||
, http-media | ||
, servant | ||
, servant-io-streams | ||
, io-streams ^>= 1.5 | ||
, servant-server >=0.15 && <0.20 | ||
, servant-client >=0.15 && <0.20 | ||
, wai >=3.2.1.2 && <3.3 | ||
, warp >=3.2.25 && <3.4 | ||
, http-client | ||
default-language: Haskell2010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
-- | This module exports 'ToSourceIO' and 'FromSourceIO' for 'IOStreams.InputStream' | ||
module Servant.IO.Streams where | ||
|
||
import Control.Monad.IO.Class (liftIO) | ||
import qualified System.IO.Streams.Core as IOS | ||
import Servant.API.Stream | ||
import qualified Servant.Types.SourceT as S | ||
|
||
instance ToSourceIO a (IOS.InputStream a) where | ||
toSourceIO src = S.SourceT ($ go) | ||
where | ||
go = S.Effect $ trans <$> IOS.read src | ||
|
||
trans Nothing = S.Stop | ||
trans (Just c) = S.Yield c go | ||
|
||
instance FromSourceIO a (IOS.InputStream a) where | ||
fromSourceIO src = S.unSourceT src $ IOS.fromGenerator . gen | ||
where | ||
gen S.Stop = pure () | ||
gen (S.Error s) = liftIO $ fail s | ||
gen (S.Skip s) = gen s | ||
gen (S.Yield a s) = IOS.yield a >> gen s | ||
gen (S.Effect ms) = liftIO ms >>= gen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These bounds are not correct, will need to update to whatever the new major version will be that includes #1661