开发者

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜