1

我正在关注一个教程,该教程解释了如何在 OCaml 中使用lwtCohttp.

我有一个_tags包含以下内容的文件:

true: package(lwt), package(cohttp), package(cohttp.lwt)

还有一个webserver.ml

open Lwt
open Cohttp
open Cohttp_lwt_unix

let make_server () =
  let callback conn_id req body =
    let uri = Request.uri req in
    match Uri.path uri with
    | "/" -> Server.respond_string ~status:`OK ~body:"hello!\n" ()
    | _ -> Server.respond_string ~status:`Not_found ~body:"Route not found" ()
  in
  let conn_closed conn_id () = () in
  Server.create { Server.callback; Server.conn_closed }

let _ =
  Lwt_unix.run (make_server ())

然后,ocamlbuild -use-ocamlfind webserver.native触发以下错误:

Error: Unbound record field callback
Command exited with code 2.

如果我更改为:Server.create { callback; conn_closed }它也会触发:

Error: Unbound record field callback
Command exited with code 2.

我不知道如何解决这个问题,所以提前感谢您对此进行调查。

4

1 回答 1

3

可能,您正在使用一个非常过时的教程,该教程是为旧cohttp界面编写的。您可以尝试查看上游存储库中的最新教程。

在您的情况下,至少应进行以下更改以编译程序:

  1. 您应该使用函数Server.make来创建服务器的实例;
  2. callbackconn_closed值应该作为函数参数传递,而不是作为记录传递,例如,

    Server.make ~callback ~conn_closed ()
    
  3. 您应该使用函数Server.create并传递一个从函数返回的值Server.make来创建服务器实例。

所以,可能,以下应该工作:

open Lwt
open Cohttp
open Cohttp_lwt_unix

let make_server () =
  let callback conn_id req body =
    let uri = Request.uri req in
    match Uri.path uri with
    | "/" -> Server.respond_string ~status:`OK ~body:"hello!\n" ()
    | _ -> Server.respond_string ~status:`Not_found ~body:"Route not found" ()
  in
  Server.create (Server.make ~callback ())

let _ =
  Lwt_unix.run (make_server ())
于 2016-11-07T19:01:14.693 回答