Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web service frameworks in haskell

First of all I'm quite new to Haskell - but I must say I've fallen in love with the language since I started playing with it. I've done extensive C, Java, python and perl. Haskell is definitely growing on me. I've written a web application/services in perl/python for one of my personal projects a while back - I was wondering if I can move it to haskell as a fun project and do some haskell hackery to see how it evolves.

I know there are some outstanding frameworks for web applications in haskell. What I'd like to do is have a service written in haskell that will respond with data in different formats (SOAP, REST-xml, REST-json). I'd use javascript to build DOM etc. So my question is are there any libraries that I could use to convert the format of the data on the fly already? Or given the scenario how would you go about doing it in haskell?

I haven't played with this project since 2008, and my initial thought was to use apacheCXF from java community and code it all in java. But I would love to do it in haskell. Any hints please?

like image 549
opensourcegeek Avatar asked Jul 12 '11 12:07

opensourcegeek


1 Answers

I have written something similar using Happstack.

What I did was create a type to represent all the possible responses of my web application.

data AppResponse = Foo String Int | Bar [String] | etc

then wrote my handlers to return values of this type:

home :: ServerPart AppResponse
user :: UserId -> ServerPart AppResponse

etc,

Then I wrote functions that would render the response in different formats:

jsonResponse :: AppResponse -> JSON
xmlResponse  :: AppResponse -> XML

etc.

Then there is a simple filter that looks at the Accept header and decides which of those conversion functions to use.

This approach is nice because:

  1. most of the code does not need to know anything about the response format (xml, json, etc)
  2. to add a new format you just write new function like, newFormatResponse :: AppResponse -> NewFormat. The AppResponse type details every possible response, so you don't have to hunt all over the code figuring out what responses are even possible.
like image 134
stepcut Avatar answered Oct 05 '22 19:10

stepcut