An OCaml webserver, but the allocating version (vs httpz which doesnt)
at main 60 lines 1.6 kB view raw
1(** Httpz static file server with Eio. 2 3 A static file server supporting Range requests, ETag, If-None-Match 4 conditional requests, and keep-alive connections. 5 6 {2 Quick Start} 7 8 {[ 9 Eio_main.run @@ fun env -> 10 Eio.Switch.run @@ fun sw -> 11 let config = Httpz.Server.{ 12 port = 8080; 13 root = Eio.Stdenv.cwd env; 14 limits = Httpz.default_limits; 15 } in 16 Httpz.Server.run ~net:(Eio.Stdenv.net env) ~sw config 17 ]} *) 18 19(** {1 Configuration} *) 20 21type config = { 22 port : int; 23 (** TCP port to listen on. *) 24 25 root : Eio.Fs.dir_ty Eio.Path.t; 26 (** Document root directory. *) 27 28 limits : Buf_read.limits; 29 (** HTTP parsing limits. *) 30} 31(** Server configuration. *) 32 33val default_config : fs:Eio.Fs.dir_ty Eio.Path.t -> config 34(** [default_config ~fs] creates a default configuration serving from [fs] 35 on port 8080 with default limits. *) 36 37(** {1 MIME Types} *) 38 39val mime_type_of_path : string -> string 40(** [mime_type_of_path path] returns the MIME type for a file path. 41 Uses magic-mime for detection with sensible defaults. *) 42 43(** {1 Running the Server} *) 44 45val run : 46 net:_ Eio.Net.t -> 47 sw:Eio.Switch.t -> 48 config -> 49 unit 50(** [run ~net ~sw config] starts the HTTP server. 51 52 The server runs until the switch is cancelled. It handles 53 multiple concurrent connections using Eio fibers. 54 55 Supports: 56 - GET and HEAD requests 57 - Range requests (single ranges) 58 - ETag and If-None-Match conditional requests 59 - Keep-alive connections 60 - Directory index (index.html) *)