[web-devel] Examples and/or tutorials for WAI
Thomas Sutton
thsutton at gmail.com
Tue Feb 15 12:06:48 CET 2011
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 <ndmitchell at gmail.com> wrote:
>>> 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
More information about the web-devel
mailing list