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

I want to write a web services server (using the http protocol) in OCaml.

For a simple example, I want to write http server . The user can access it and provide a parameter of the type http://myservice.com?i=1&j=2 . Then my server will receive 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 calculation example, what I really need to do is

  • database access (MongoDB) to retrieve data
  • Access another third-party web service for more data.
  • calculate all the data to get the result and return to the user.

So, I also need to consider parallelism / multi-threading , although first I want to start with a simple case.


My questions:

  • What library should I use to set up such an http server ? I reviewed Ocamlnet 3 and think it might be a good candidate, but it lacks a good tutorial, and I still don't know how to use nethttpd or netplex , etc.
  • How do I build my web application architecture ? I know that OCaml is not suitable for parallelism , then how can I make each service instance not block?
+4
source share
2 answers

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

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

Personally, I cannot stand ocamlnet, so I would look at cohttp. Ocamlnet is bigger, more mature and has more documents.

+6
source

I think you should give Ocsigen a try.

+3
source

Source: https://habr.com/ru/post/1485839/


All Articles