shawn-mcginty / naboris

Simple, fast, minimalist http server for OCaml/ReasonML
https://naboris.dev
MIT License
71 stars 4 forks source link

OCaml example from readme #31

Closed gtach2o closed 4 years ago

gtach2o commented 4 years ago

Hi! I was trying to execute OCaml example from readme

(* OCaml *)
let server_config: unit Naboris.ServerConfig.t = Naboris.ServerConfig.create ()
  |> Naboris.ServerConfig.setRequestHandler(fun route req res ->
    match (Naboris.Route.path route) with
      | ["hello"] ->
        res
          |> Naboris.Res.text req "Hello world!";
      | _ ->
        res
          |> Naboris.Res.status 404
          |> Naboris.Res.text req "Resource not found.";
  ) in

Naboris.listenAndWaitForever 3000 server_config
(* In a browser navigate to http://localhost:3000/hello *)

but I didn't get success.

Here are the steps:

opam switch create . ocaml-base-compiler.4.09.0
eval $(opam env)
opam install merlin ocp-indent dune utop naboris
touch main.ml #paste OCaml example
touch dune #paste dune config
(executable
    (name main)
    (libraries naboris)
)
dune build main.exe

No errors. Execute

dune exec ./main.exe 
# or
_build/default/main.exe 

nothing happens. I'm beginner in OCaml, so maybe I did something wrong. I would appreciate any advice. Thanks.

shawn-mcginty commented 4 years ago

@gtach2o thanks for checking out naboris!

I will definitely update the readme to an actual working example.. that's my fault.

Naboris.listenAndWaitForever actually returns an Lwt Promise (which never resolves)

At the highest level Lwt promises need to be executed within the context of Lwt_main . You can read their documentation here https://ocsigen.org/lwt/5.1.1/api/Lwt_main

To get your example up and running wrap the last call like this.

let _ = Lwt_main.run begin
  Naboris.listenAndWaitForever 3000 server_config
end
gtach2o commented 4 years ago

Thank you! It works.

shawn-mcginty commented 4 years ago

I'm going to leave this open until I actually fix the README

shawn-mcginty commented 4 years ago

Will be fixed in 0.1.1