How does a client-server app source code look like?
Eliom client-server applications
Eliom client-server applications are running in your browser for a certain lifetime and consist of one or several pages/URLs. An application has its associated js file, which must have the same name (generated automatically by the default makefile and added automatically by Eliom in the page).
For example, we define an application called example:
module Example =
Eliom_registration.App
(struct
let application_name = "example"
end)
Eliom Services
Pages are generated by services. Here is how to define a service:
let main =
Eliom_service.create
~path:(Eliom_service.Path [])
~meth:(Eliom_service.Get unit)
()
The path is a list of string corresponding to the url. Here, the list is empty because it's the main page.
If, for example, the list is ["hello";"world"], the page will be accessed using the address:
http://website.com/hello/world/
More informations about parameters: How to use GET parameters (parameters in the URL)?
Now that our service has been declared, we associate to it an OCaml function that will generate the page. We call this service registration. If the service belongs to the application, we use the registrations functions from module Example defined above.
let _ =
Example.register
~service:main
(fun () () ->
Lwt.return ...)
The third parameter is the function that will be called to generate the page. This function takes two parameters, corresponding respectively to GET and POST parameters. Here both are () because the service does not take any parameter. The function returns an element of type html (using Lwt).
Page Content
The content of Eliom application pages is made using functions. The html function takes two parameters: head and body, which are also functions that takes parameters coresponding to their content.
(html
(head (title (txt "Page Title")) [])
(body [p [txt "Hello World!"]])))
Most of the elements functions take a list of other elements as a parameter.
Validity of HTML is checked at compile time, which means that a program that may generate a page that does not respect the recommendations of the W3C will be rejected at compile time.
Download full code
Warning: This third party code may be outdated. Please notify the author is something is broken, or do a pull request on github.