This commit is contained in:
Simon Cruanes 2020-05-28 21:22:21 -04:00
parent 3ac5510e2d
commit 13dca59afd
2 changed files with 11 additions and 10 deletions

View file

@ -24,7 +24,7 @@ let () =
(* echo request *) (* echo request *)
S.add_route_handler server S.add_route_handler server
S.Route.(exact "echo" @/ return) S.Route.(exact "echo" @/ return)
"/echo" (fun req -> S.Response.make_ok (Format.asprintf "echo:@ %a@." S.Request.pp req)); (fun req -> S.Response.make_ok (Format.asprintf "echo:@ %a@." S.Request.pp req));
Printf.printf "listening on http://%s:%d\n%!" (S.addr server) (S.port server); Printf.printf "listening on http://%s:%d\n%!" (S.addr server) (S.port server);
match S.run server with match S.run server with
| Ok () -> () | Ok () -> ()

View file

@ -16,15 +16,16 @@ module S = Tiny_httpd
let () = let () =
let server = S.create () in let server = S.create () in
(* say hello *) (* say hello *)
S.add_path_handler ~meth:`GET server S.add_route_handler ~meth:`GET server
"/hello/%s@/" (fun name _req -> S.Route.(exact "hello" @/ string @/ return)
S.Response.make_string (Ok ("hello " ^name ^"!\n"))); (fun name _req -> S.Response.make_ok ("hello " ^name ^"!\n"));
(* echo request *) (* echo request *)
S.add_path_handler server S.add_route_handler server
"/echo" (fun req -> S.Response.make_string S.Route.(exact "echo" @/ return)
(Ok (Format.asprintf "echo:@ %a@." S.Request.pp req))); (fun req -> S.Response.make_ok (Format.asprintf "echo:@ %a@." S.Request.pp req));
S.add_path_handler ~meth:`PUT server S.add_route_handler ~meth:`PUT server
"/upload/%s" (fun path req -> S.Route.(exact "upload" @/ string_urlencoded @/ return)
(fun path req ->
try try
let oc = open_out @@ "/tmp/" ^ path in let oc = open_out @@ "/tmp/" ^ path in
output_string oc req.S.Request.body; output_string oc req.S.Request.body;
@ -51,7 +52,7 @@ $ curl -X GET http://localhost:8080/hello/quadrarotaphile
hello quadrarotaphile! hello quadrarotaphile!
# the path "echo" just prints the request. # the path "echo" just prints the request.
$ curl -X GET http://localhost:8080/echo --data "howdy y'all" $ curl -X GET http://localhost:8080/echo --data "howdy y'all"
echo: echo:
{meth=GET; {meth=GET;
headers=Host: localhost:8080 headers=Host: localhost:8080