ocsigen / ocsigenserver

Web server in OCaml.
http://ocsigen.org/ocsigenserver/
Other
100 stars 32 forks source link

Using Ocsigen Server without configuration file (and with static linking) #238

Closed balat closed 3 months ago

balat commented 7 months ago

Almost ready.

Todo:

And in other projects/branches/PRs:

Example: To add a Web server to your OCaml program, serving static files from directory "static":

let _ = Ocsigen_server.start [ Ocsigen_server.host [Staticmod.run ~dir:"static" ()]]

Install:

opam pin add -n ocsigenserver git@github.com:ocsigen/ocsigenserver.git#config
opam install ocsigenserver

Example of Dune file:

(executable
 (public_name Ololo)
 (name main)
 (libraries
  main
  ocsigenserver
  ocsigenserver.ext.staticmod))

Compile with:

dune build

A more complex example without Eliom:

let _ = Ocsigen_server.start [ Ocsigen_server.host ~regexp:".*"
        [ Accesscontrol.(
            if_
              (and_
                 [ ip "127.0.0.1"
                 ; header ~name:"user-agent" ~regexp:".*Safari.*"
                 ; method_ `POST ])
              [forbidden] [])
        ; Redirectmod.run
            ~redirection:
              (Redirectmod.create_redirection ~full_url:false ~regexp:"^p.*$"
                 "toto.html")
            ()
        ; Authbasic.run ~realm:"pouette"
            ~auth:(fun _u p -> Lwt.return (p = "toto"))
            ()
        ; Staticmod.run ~dir:"statico" ()
        ; Staticmod.run ~dir:"static" ()
        ; Cors.run ~credentials:false ()
        ; Deflatemod.run ~mode:(`All_but []) () ] ]
]]

Here is an example of a complex configuration using Server and Eliom without config file with the new interface:

let _ = Ocsigen_config.set_debug ()

let f s _ () =
  Lwt.return
    Eliom_content.Html.F.(html (head (title (txt "")) []) (body [h1 [txt s]]))

(* My first Eliom app has default name *)
let myservice =
  Eliom_service.create ~path:(Eliom_service.Path ["aa"])
    ~meth:(Eliom_service.Get Eliom_parameter.any) ()

let () = Eliom_registration.Html.register ~service:myservice (f "Default")

(* My second Eliom app is called "e" *)
let _ = Eliom.set_app_name "e"

let myservice =
  Eliom_service.create ~path:(Eliom_service.Path ["aa"])
    ~meth:(Eliom_service.Get Eliom_parameter.any) ()

let () = Eliom_registration.Html.register ~service:myservice (f "e")

(* Configuring Ocsigen Server:  *)
let _ =
  Ocsigen_server.start ~debugmode:true ~veryverbose:()
    [ Ocsigen_server.host ~re:"a" [Staticmod.run ~dir:"static" ()]
    ; Ocsigen_server.host ~re:".*"
        [ Redirectmod.run
            ~redirection:
              (Redirectmod.create_redirection ~full_url:`No ~regexp:"^p.*$"
                 "toto.html")
            ()
        ; Authbasic.run ~realm:"pouette"
            ~auth:(fun _u p -> Lwt.return (p = "b"))
            ()
        ; Staticmod.run ~dir:"statico" ()
        ; Staticmod.run ~dir:"static" ()
        ; (* Default Eliom app: *)
          Eliom.run ()
        ; (* Second Eliom app on site "ee" *) 
          Ocsigen_server.site ["ee"] [Eliom.run ~app:"e" ()]
        ; (* others *)
          Cors.run ~credentials:false ()
        ; Deflatemod.run ~mode:(`All_but []) () ] ]

To compile, create this dune file:

(executable
 (public_name stata)
 (name main)
 (libraries
  stata
  ocsigenserver
  ocsigenserver.ext.staticmod
  ocsigenserver.ext.authbasic
  ocsigenserver.ext.cors
  ocsigenserver.ext.deflatemod
  ocsigenserver.ext.redirectmod
  ocsipersist.dbm
  eliom.server))

and compile:

dune build

To test with Ocsigen Start:

opam pin add -n ocsigenserver git@github.com:ocsigen/ocsigenserver.git#config
opam pin add -n ocsipersist-lib git@github.com:ocsigen/ocsipersist.git#static
opam pin add -n ocsipersist git@github.com:ocsigen/ocsipersist.git#static
opam pin add -n ocsipersist-pgsql git@github.com:ocsigen/ocsipersist.git#static
opam pin add -n ocsipersist-pgsql-config git@github.com:ocsigen/ocsipersist.git#static
opam pin add -n eliom git@github.com:ocsigen/eliom.git#config
opam pin add -n ocsigen-toolkit git@github.com:ocsigen/ocsigen-toolkit.git#static
opam pin add -n ocsigen-start git@github.com:ocsigen/ocsigen-start.git#static
opam install ocsigen-start
git clone git@github.com:ocsigen/os_template.git
cd os_template
git checkout static
make test.static.byte

Thanks to @vasilisp for the inspiration!

balat commented 6 months ago

This should be ready to test. @vouillon @hhugo @Wonko7