Lightweight database using Ocsipersist

For maintaining the list of user and password, we do not need the full power of a SQL database, a key/value table is sufficient. Ocsigen has such a simple table mechanism directly integrated: Ocsipersist.

We first create a table holding ocaml values:

let user_table = Ocsipersist.open_table "user_table"

Then we can easily handle the user management code:

let check_pwd name pwd =
  try_lwt
    lwt saved_password = Ocsipersist.find user_table name in
    Lwt.return (pwd = saved_password)
  with
    Not_found -> Lwt.return false

let () = Eliom_registration.Action.register
  ~service:create_account_service
  (fun () (name, pwd) -> Ocsipersist.add user_table name pwd)

let () = Eliom_registration.Action.register
  ~service:connection_service
  (fun () (name, password) ->
    match_lwt check_pwd name password with
      | true -> Eliom_reference.set username (Some name)
      | false -> Lwt.return ())

Ocsiperist and Eliom

Eliom has a higher level interface above Ocsiperist to store the server side state of an application, called Eliom references. For example persistent Eliom references of scope session group can be used to store information about a user (if the session group corresponds to the user).