How to write a web server (service) in OCaml?

I wish to write a web service server (using http protocol) in OCaml.

For a simple example, I want to write a http server . User can access it and provide parameter like http://myservice.com?i=1&j=2 . Then my server get the request and parameters and calculate i+j and return the result.


Of course, my service will be more complicated for the calculation part. Instead of this simple example of calculation, what I really need to do is to

  • access the database (MongoDB) to get some data
  • access another 3rd party web service to get more data
  • calculate all data to get a result and return to the user.
  • So, I also need to consider parallelism / multi-threading , although I want to start with simple case first.


    My questions are:

  • Which library should I use to first set up such a http server ? I have looked into Ocamlnet 3 and think it might be a good candidate, but it lacks good tutorial and I still don't know how to use nethttpd or netplex etc.
  • How should I design the architecture of my web application? I know OCaml is not good at parallelism , then how can I make each service instance not blocking?

  • Matias Giovannini has a good tutorial how to do this in ocamlnet:

    http://alaska-kamtchatka.blogspot.ca/2012/10/how-to-write-simple-web-application.html

    Personally I can't stand ocamlnet so I'd look into cohttp. Ocamlnet is bigger, more mature, and has more docs however.


    我想你应该给Ocsigen一个尝试

    链接地址: http://www.djcxy.com/p/79340.html

    上一篇: 如何让OCaml顶级使用电池?

    下一篇: 如何在OCaml中编写Web服务器(服务)?