Eio HTTP server with static file serving and route handlers
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

respond: rewrite README — fix route type to get/post combinators, typed requests

+28 -15
+28 -15
README.md
··· 2 2 3 3 Eio HTTP server with static file serving and route handlers. 4 4 5 - Serves files from a document root with MIME detection, ETag conditional requests, and directory index. Supports custom route handlers for API endpoints. Reuses HTTP types from the `requests` library. 5 + Serves files from a document root with MIME detection, ETag conditional 6 + requests, and directory index. Supports custom route handlers for API 7 + endpoints. Reuses HTTP types from the `requests` library. 6 8 7 9 ## Installation 8 10 ··· 15 17 ```ocaml 16 18 Eio_main.run @@ fun env -> 17 19 let routes = 18 - [ 19 - ("/api/health", fun _params -> Respond.Response.json {|{"ok":true}|}); 20 - ("/api/echo", fun params -> 21 - match List.assoc_opt "msg" params with 22 - | Some msg -> Respond.Response.text msg 23 - | None -> Respond.Response.bad_request "missing msg"); 20 + Respond.[ 21 + get "/api/health" (fun _req -> Response.json {|{"ok":true}|}); 22 + post "/api/echo" (fun req -> 23 + Response.text (Respond.post_request_body req)); 24 24 ] 25 25 in 26 26 Respond.run ~net:(Eio.Stdenv.net env) ~port:8080 27 - ~root:(Eio.Stdenv.cwd env) ~routes 27 + ~root:(Eio.Stdenv.cwd env) routes 28 28 ``` 29 29 30 30 ## API Overview 31 31 32 - - **`type route`** -- `string * (params -> Response.t)` -- path pattern + handler 33 - - **`type params`** -- `(string * string) list` -- query/route parameters 34 - - **`Response.json`**, **`Response.text`**, **`Response.html`** -- Response constructors 35 - - **`Response.not_found`**, **`Response.bad_request`**, **`Response.redirect`** -- Error responses 36 - - **`parse_url`** -- Extract path and query parameters 37 - - **`match_route`** -- Find matching route for a request path 38 - - **`run`** -- Start serving on a port with static files + route handlers 32 + ### Routes 33 + 34 + Routes are created using `get` and `post` combinators: 35 + 36 + - **`Respond.get path handler`** -- Create a GET route. Handler receives a `get_request`. 37 + - **`Respond.post path handler`** -- Create a POST route. Handler receives a `post_request`. 38 + 39 + Handlers receive typed request records (with `params`, `headers`, and for 40 + POST also `body`), not bare parameter lists. 41 + 42 + ### Response Constructors 43 + 44 + - **`Response.json`**, **`Response.text`**, **`Response.html`** -- Content responses 45 + - **`Response.not_found`**, **`Response.bad_request`**, **`Response.redirect`** -- Error/redirect responses 46 + - **`Response.method_not_allowed`**, **`Response.internal_server_error`** -- HTTP error responses 47 + 48 + ### Server 49 + 50 + - **`Respond.run`** -- Start serving on a port with static files + route handlers. 51 + Routes are passed as the last positional argument. 39 52 40 53 ## License 41 54