14

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?

opensourcegeek
  • 5,552
  • 7
  • 43
  • 64

2 Answers2

9

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.
stepcut
  • 1,502
  • 8
  • 10
7

There is a haskellwiki page dedicated to this topic. Among these HappStack and Yesod are the most mature. For a beginner I would recommend HappStack since Yesod uses quite a bit of QuasiQuotes magic.

HappStack only has some magic in it's state module. From what I have heard this will be changed in HappStack 7 where it will be changed to use MACID store wich is a lot less magical and has less boilerplate.

If you want something plain you should have a look at Snap framework.

HaskellElephant
  • 9,819
  • 4
  • 38
  • 67
  • 6
    Do note that most of Yesod's magic is: 1) Optional 2) Safely usable without worrying about how it works 3) Really convenient. It's a matter of taste more than anything else. – C. A. McCann Jul 12 '11 at 14:19
  • 1
    @camccan I agree with you; there is nothing wrong with Yesods magic. I though it might be a problem for a beginner to handle both Haskell and an Extension of its syntax. That might not be the case, and it might just be my preference to vanilla Haskell shining through. – HaskellElephant Jul 13 '11 at 10:09