Skip to content

Instantly share code, notes, and snippets.

@hannesm
Last active June 6, 2018 10:22
Show Gist options
  • Save hannesm/2d2bd883d929bb15ba1f5f82a230b9f0 to your computer and use it in GitHub Desktop.
Save hannesm/2d2bd883d929bb15ba1f5f82a230b9f0 to your computer and use it in GitHub Desktop.
https fetch
open Mirage
let client =
let packages = [ package "cohttp-mirage"; package "duration" ; package ~sublibs:["mirage"] "tls" ] in
foreign
~packages
"Unikernel.Client" @@ time @-> resolver @-> conduit @-> job
let () =
let stack = generic_stackv4 default_network in
let res_dns = resolver_dns stack in
let conduit = conduit_direct stack in
let job = [ client $ default_time $ res_dns $ conduit ] in
register "https-fetch" job
open Lwt.Infix
open Mirage_types_lwt
let uri = Uri.of_string "https://nqsb.io"
module Client (T: TIME) (RES: Resolver_lwt.S) (CON: Conduit_mirage.S) = struct
let https_fetch resolver ctx =
Logs.info (fun m -> m "fetching %a" Uri.pp_hum uri) ;
let ctx = Cohttp_mirage.Client.ctx resolver ctx in
Cohttp_mirage.Client.get ~ctx uri >>= fun (response, body) ->
Cohttp_lwt.Body.to_string body >>= fun body ->
Logs.info (fun m -> m "%s" (Sexplib.Sexp.to_string_hum (Cohttp.Response.sexp_of_t response))) ;
Logs.info (fun m -> m "Received body length: %d" (String.length body)) ;
Lwt.return_unit
let start _time res ctx =
Conduit_mirage.with_tls ctx >>= fun ctx ->
https_fetch res ctx
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment