SCGI library for OCaml and eio

Hey everyone!

To learn a bit of networking and eio, I wrote an scgi library with eio support. It aims to just implement the scgi protocol and a few helpers for writing HTTP responses. It’s still very new, and I am looking for feedback on the interface and implementation before I publish it to opam.

Here’s a simple ping/pong example to get started:

open Scgi_eio

let handler (request : Request.t) =
  match Request.path request with
  | ["ping"] ->
      Http_response.body_string `Ok "pong"
  | _ ->
      Http_response.body_status `Not_found

let () : unit =
  let port = 3000 in
  Eio_main.run
  @@ fun env ->
  let addr = `Tcp (Eio.Net.Ipaddr.V4.loopback, port) in
  let net = Eio.Stdenv.net env in
  Eio.Switch.run
  @@ fun sw ->
  let conn = Eio.Net.listen net ~sw ~reuse_addr:true ~backlog:5 addr in
  Eio.traceln "Listening to connections on port %s" (string_of_int port) ;
  Eio.Net.run_server conn
    (Scgi_eio.http_server ~settings:Scgi_eio.default_settings handler)
    ~on_error:(Eio.traceln "Error handling connection: %a" Fmt.exn)
1 Like