Skip to content

Instantly share code, notes, and snippets.

@jlongster
Last active October 25, 2016 15:22
Show Gist options
  • Save jlongster/6137eb15a092f9e8f53f92f5f125b831 to your computer and use it in GitHub Desktop.
Save jlongster/6137eb15a092f9e8f53f92f5f125b831 to your computer and use it in GitHub Desktop.
For `(Router.callback router ())` in Index.re:
Error: The types don't match.
This is: Cohttp_lwt_unix.Request.t -> 'a -> 'b
Wanted: Cohttp_lwt_unix.Server.conn ->
Cohttp.Request.t ->
Cohttp_lwt_body.t -> (Cohttp.Response.t * Cohttp_lwt_body.t) Lwt.t
open Lwt;
open Cohttp;
open Cohttp_lwt_unix;
let module Util = Yojson.Basic.Util;
let router =
Router.add_route
(Router.create ())
"/"
(
fun req body =>
Cohttp_lwt_body.to_string body >>= (
fun body => {
let json = Yojson.Basic.from_string body;
let synced_from = json |> Util.member "synced_from" |> Util.to_string;
let response = Yojson.Basic.to_string (
`Assoc [
("id", `String (Uuidm.to_string (Uuidm.v `V4))),
("devices", `String synced_from)
]
);
Server.respond_string status::`OK body::response ()
}
)
);
let server =
Server.create mode::(`TCP (`Port 8000)) (Server.make callback::(Router.callback router ()) ());
ignore (Lwt_main.run server);
open Lwt;
open Cohttp;
open Cohttp_lwt_unix;
let create () => [];
let add_route router url handler => [(url, handler), ...router];
let callback router => {
let routes = List.rev router;
fun _conn req body => {
let uri = req |> Request.uri |> Uri.to_string;
let (url, handler) = List.find (fun (url, handler) => url == uri) routes;
handler req body
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment