【问题标题】:Error: Unbound record field Server.callback - Ocaml错误:未绑定记录字段 Server.callback - Ocaml
【发布时间】:2016-11-07 17:58:56
【问题描述】:

我正在学习一个教程,该教程解释了如何使用 lwtCohttp 在 OCaml 中制作一个简单的 Web 服务器。

我有一个 _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.

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

【问题讨论】:

    标签: webserver ocaml ocaml-lwt


    【解决方案1】:

    很可能,您正在使用一个非常过时的教程,该教程是为旧的cohttp 接口编写的。您可以尝试查看the upstream repository 中的最新教程。

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

    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 ())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-21
      • 1970-01-01
      • 1970-01-01
      • 2013-09-19
      相关资源
      最近更新 更多