Examples and/or tutorials for WAI

Hi All, I spent a few hours last night trying to hook a HaXR XML-RPC server up with WAI instead of using the built-in CGI support. Alas, I failed dismally. Is there any more thorough tutorial-style documentation for WAI? Or a good example project that does something more sophisticated than "Hello, Web!"? What tripped me up was trying to figure out the enumerator part of the API so that I can read the request body (and turn it into a String) and run the XML-RPC request handler. Any help would be appreciated! Cheers, Thomas Sutton

On Mon, Feb 7, 2011 at 7:23 AM, Thomas Sutton
Hi All,
I spent a few hours last night trying to hook a HaXR XML-RPC server up with WAI instead of using the built-in CGI support. Alas, I failed dismally. Is there any more thorough tutorial-style documentation for WAI? Or a good example project that does something more sophisticated than "Hello, Web!"?
What tripped me up was trying to figure out the enumerator part of the API so that I can read the request body (and turn it into a String) and run the XML-RPC request handler. Any help would be appreciated!
There's no tutorial on WAI at the moment. You could look at the code in yesod-core to get an idea of how to use it, but I doubt that will give you much of a return on your investment. In the future, I intend to add a nice explanation of WAI to the Yesod book, but I'm not there yet. As for your specific issue: if you don't mind reading the entire request body into memory, you can get a lazy bytestring fairly easily using the consume[1] function from the enumerator package. Converting that into a String depends on your character encoding. If you're looking to do *lazy* IO... that's a bit more complicated, and will necessarily involve some hacks, but it is possible. I'll likely be writing that kind of code in the not-too-distant future, so let me know if you're interested in that. Also, if you can think of specific examples you would like to see, let me know and I can try to add them to the repo. Michael [1] http://hackage.haskell.org/packages/archive/enumerator/0.4.6/doc/html/Data-E...

Hi Thomas,
What tripped me up was trying to figure out the enumerator part of the API so that I can read the request body (and turn it into a String) and run the XML-RPC request handler. Any help would be appreciated!
In Hoogle I use, responseLBS to construct responses (from Wai), and responseFlatten to deconstruct them: responseFlatten :: Response -> IO (Status, ResponseHeaders, LBString) responseFlatten r = responseEnumerator r $ \s hs -> do builders <- consume return (s, hs, toLazyByteString $ mconcat builders) Now you can avoid knowledge of anything to do with enumerators etc. (which I still don't understand!) Thanks, Neil

Hi Michael and Neil,
Thank you both for replying and for your suggestions. I now have code
that not only typechecks, but seems to actually work!
On 12 February 2011 17:12, Neil Mitchell
What tripped me up was trying to figure out the enumerator part of the API so that I can read the request body (and turn it into a String) and run the XML-RPC request handler. Any help would be appreciated!
The code that I have now works like this:
-- | A WAI application to serve HaXR XML-RPC methods. waiXmlRpcServer :: [(String, XmlRpcMethod)] -> Application waiXmlRpcServer ms req = do -- Get all the input as a String. inp <- consume >>= return . C.unpack . mconcat -- Run the XML-RPC server on the input. outp <- liftIO (handleCall (methods ms) inp) -- Return a response return $ ResponseBuilder status200 [] (fromLazyByteString outp)
No doubt this could be improved (I did write an Iteratee first, but then couldn't figure out how to hook that up properly using the ResponseEnumerator constructor) but I'm just happy to have something that compiles and executes. I can now use:
-- | The function to publish. add :: Int -> Int -> IO Int add x y = return (x + y)
app = waiXmlRpcServer [("example.add", fun add)]
-- | As normal main :: IO () main = run 3000 app
instead of using the CGI interface built in to HaXR Again, thanks for your help! Thomas Sutton
participants (3)
-
Michael Snoyman
-
Neil Mitchell
-
Thomas Sutton