I have the following code, which base 64 encodes the responses from a web server [1]. I can do it with a simple "process it all in one go" approach - which is shown by proxy1 below - but I'd like to try it using a streaming approach using conduit (since it's already installed in my sandbox), but can't see how to connect a Conduit to Scotty's stream routine, which accepts WAI's StreamingBody.
Thanks for any pointers,
Doug
[1] as to why I'm doing this, it involves a yak and a sharp blade, but even if the reason is rather silly, I'd still like to know how to do it ;-)
--
-- Simplified code below; hopefully without too many typos
--
import Data.ByteString.Base64.Lazy (encode)
import Data.Default (def)
import Network.HTTP.Client (defaultManagerSettings)
import Network.HTTP.Conduit (Manager, newManager
, parseUrl, httpLbs, http
, responseBody)
import Web.Scotty
main :: IO ()
main = do
mgr <- newManager defaultManagerSettings
scottyOpts def $ webApp mgr
webApp :: Manager -> ScottyM ()
webapp mgr = do
get "/proxy/:imgnum" $ do
imgnum <- param "imgnum"
proxy1 mgr imgnum
-- This works, and given the image sizes I am dealing with, is not a
-- terrible solution, but can it be improved (which is what proxy2 is
-- meant to be)?
--
proxy1 :: Manager -> Int -> ActionM ()
proxy1 mgr i = do
let url = "
http://example.com/img" ++ show i ++ ".png"
req <- liftIO $ parseUrl url
rsp <- liftIO $ httpLbs req mgr
setHeader "Content-Type" "text/plain; charset=utf-8"
raw $ encode $ responseBody rsp
-- I'd like to change from httpLbs to http and deal with it using
-- encodeBase64 from conduit-combinator, but how do I turn it
-- into the Builder -> IO ()/IO () pair needed to form a StreamingBody?
--
proxy2 :: Manager -> Int -> ActionM ()
proxy2 mgr i = do
let url = "
http://example.com/img" ++ show i
req <- liftIO $ parseUrl url
rsp <- liftIO $ http req mgr
setHeader "Content-Type" "text/plain; charset=utf-8"
--
-- I think want to pass responseBody rsp to
-- Data.Conduit.Combinators.encodeBase64
-- and then to Scotty's
-- steam :: StreamingBody -> ActionM ()
-- but I can't see how to plumb it all
-- together
stream $ undefined