From 94428f9e3c69ebd23e843fa77259b0dd3dc53142 Mon Sep 17 00:00:00 2001 From: Alban Gruin Date: Sat, 12 Sep 2020 14:01:24 +0200 Subject: ucs: adding a basic webserver, only returning an ICS file on GET queries Signed-off-by: Alban Gruin --- src/ucs.ml | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/ucs.ml b/src/ucs.ml index b4e0952..16fce75 100644 --- a/src/ucs.ml +++ b/src/ucs.ml @@ -19,7 +19,7 @@ open Lwt open Cohttp open Cohttp_lwt_unix -let body = +let body () = let parameters = Uri.encoded_of_query ["start", ["2020-09-01"]; "end", ["2020-10-01"]; "resType", ["103"]; "calView", ["month"]; @@ -30,8 +30,19 @@ let body = >>= fun (_resp, body) -> Cohttp_lwt.Body.to_string body +let server = + let callback _conn req _body = + let meth = Request.meth req in + match meth with + | `GET -> + body () >>= fun body -> + let body = Ics.to_string @@ Course.decode body and + headers = Header.init_with "Content-Type" "text/calendar" in + Server.respond_string ~status:`OK ~body ~headers () + | _ -> + Server.respond_string + ~status:`Method_not_allowed ~body:"Method not allowed\n" () in + Server.create ~mode:(`TCP (`Port 8080)) (Server.make ~callback ()) + let () = - Lwt_main.run body - |> Course.decode - |> Ics.to_string - |> print_endline + ignore @@ Lwt_main.run server -- cgit v1.2.1