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 *)
S.add_route_handler server
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);
match S.run server with
| Ok () -> ()

View file

@ -16,15 +16,16 @@ module S = Tiny_httpd
let () =
let server = S.create () in
(* say hello *)
S.add_path_handler ~meth:`GET server
"/hello/%s@/" (fun name _req ->
S.Response.make_string (Ok ("hello " ^name ^"!\n")));
S.add_route_handler ~meth:`GET server
S.Route.(exact "hello" @/ string @/ return)
(fun name _req -> S.Response.make_ok ("hello " ^name ^"!\n"));
(* echo request *)
S.add_path_handler server
"/echo" (fun req -> S.Response.make_string
(Ok (Format.asprintf "echo:@ %a@." S.Request.pp req)));
S.add_path_handler ~meth:`PUT server
"/upload/%s" (fun path req ->
S.add_route_handler server
S.Route.(exact "echo" @/ return)
(fun req -> S.Response.make_ok (Format.asprintf "echo:@ %a@." S.Request.pp req));
S.add_route_handler ~meth:`PUT server
S.Route.(exact "upload" @/ string_urlencoded @/ return)
(fun path req ->
try
let oc = open_out @@ "/tmp/" ^ path in
output_string oc req.S.Request.body;