Skip to content

Commit

Permalink
httpaf-mirage (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed May 11, 2020
1 parent 9b1e3a5 commit 96b9c60
Show file tree
Hide file tree
Showing 18 changed files with 749 additions and 262 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ script: bash -ex ./.travis-docker.sh
env:
global:
- DISTRO="ubuntu"
- PINS="httpaf-async:. httpaf-lwt-unix:. httpaf:."
- PINS="httpaf-async:. httpaf-lwt:. httpaf:. httpaf-lwt-unix:. httpaf-mirage:."
- PACKAGE="httpaf"
- TESTS=true
- POST_INSTALL_HOOK="opam install --with-test httpaf-async httpaf-lwt-unix && opam exec -- make examples"
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/dune
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

(alias
(name benchmarks)
(deps (glob_files *.exe)))
(deps
(glob_files *.exe)))
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(lang dune 1.5)
(lang dune 1.0)

(name httpaf)
3 changes: 2 additions & 1 deletion examples/lwt/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

(alias
(name examples)
(deps (glob_files *.exe)))
(deps
(glob_files *.exe)))
18 changes: 18 additions & 0 deletions examples/mirage/config.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
open Mirage

(* Network configuration *)

let stack = generic_stackv4 default_network

(* Dependencies *)

let server =
foreign "Unikernel.Make"
(console @-> pclock @-> http @-> job)

let app =
httpaf_server @@ conduit_direct stack

let () =
register "httpaf_unikernel"
[ server $ default_console $ default_posix_clock $ app ]
52 changes: 52 additions & 0 deletions examples/mirage/unikernel.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
open Lwt.Infix
open Httpaf

module type HTTP = Httpaf_mirage.Server_intf

module Dispatch (C: Mirage_types_lwt.CONSOLE) (Http: HTTP) = struct

let log c fmt = Printf.ksprintf (C.log c) fmt

let get_content c path =
log c "Replying: %s" path >|= fun () ->
"Hello from the httpaf unikernel"

let dispatcher c reqd =
let {Request.target; _} = Reqd.request reqd in
Lwt.catch
(fun () ->
get_content c target >|= fun body ->
let response = Response.create
~headers:(Headers.of_list ["Content-Length", body
|> String.length
|> string_of_int])
`OK
in
Reqd.respond_with_string reqd response body)
(fun exn ->
let response = Response.create `Internal_server_error in
Lwt.return (Reqd.respond_with_string reqd response (Printexc.to_string exn)))
|> ignore

let serve c dispatch =
let error_handler ?request:_ _error mk_response =
let response_body = mk_response Headers.empty in
Body.write_string response_body "Error handled";
Body.flush response_body (fun () -> Body.close_writer response_body)
in
Http.create_connection_handler
?config:None
~request_handler:(dispatch c)
~error_handler
end

(** Server boilerplate *)
module Make (C : Mirage_types_lwt.CONSOLE) (Clock : Mirage_types_lwt.PCLOCK) (Http: HTTP) = struct

module D = Dispatch (C) (Http)

let log c fmt = Printf.ksprintf (C.log c) fmt
let start c _clock http =
log c "started unikernel listen on port 8001" >>= fun () ->
http (`TCP 8001) @@ D.serve c D.dispatcher
end
6 changes: 3 additions & 3 deletions httpaf-lwt-unix.opam
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ homepage: "https://github.com/inhabitedtype/httpaf"
bug-reports: "https://github.com/inhabitedtype/httpaf/issues"
dev-repo: "git+https://github.com/inhabitedtype/httpaf.git"
build: [
["dune" "subst"] {pinned}
["dune" "build" "-p" name "-j" jobs]
["dune" "build" "-p" name]
]
depends: [
"ocaml" {>= "4.03.0"}
"faraday-lwt-unix"
"httpaf" {>= "0.6.0"}
"dune" {>= "1.5.0"}
"httpaf-lwt"
"lwt"
]
synopsis: "Lwt support for http/af"
synopsis: "Lwt + Unix support for http/af"
21 changes: 21 additions & 0 deletions httpaf-lwt.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
opam-version: "2.0"
name: "httpaf-lwt"
maintainer: "Spiros Eliopoulos <[email protected]>"
authors: [
"Anton Bachin <[email protected]>"
"Spiros Eliopoulos <[email protected]>"
]
license: "BSD-3-clause"
homepage: "https://github.com/inhabitedtype/httpaf"
bug-reports: "https://github.com/inhabitedtype/httpaf/issues"
dev-repo: "git+https://github.com/inhabitedtype/httpaf.git"
build: [
["dune" "build" "-p" name "-j" jobs]
]
depends: [
"ocaml" {>= "4.03.0"}
"httpaf"
"dune" {build}
"lwt"
]
synopsis: "Lwt support for http/af"
27 changes: 27 additions & 0 deletions httpaf-mirage.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
opam-version: "2.0"
name: "httpaf-mirage"
maintainer: "Antonio Nuno Monteiro <[email protected]>"
authors: [ "Antonio Nuno Monteiro <[email protected]>"
"Spiros Eliopoulos <[email protected]>" ]
license: "BSD-3-clause"
homepage: "https://github.com/inhabitedtype/httpaf"
bug-reports: "https://github.com/inhabitedtype/httpaf/issues"
dev-repo: "git+https://github.com/inhabitedtype/httpaf.git"
build: [
["dune" "build" "-p" name]
]
depends: [
"ocaml" {>= "4.03.0"}
"faraday-lwt"
"httpaf"
"httpaf-lwt"
"mirage-conduit"
"cstruct"
"dune" {build}
"lwt"
]
pin-depends: [
[ "httpaf.dev" "git+https://github.com/anmonteiro/httpaf.git#fork" ]
[ "httpaf-lwt.dev" "git+https://github.com/anmonteiro/httpaf.git#fork" ]
]
synopsis: "Mirage support for http/af"
5 changes: 3 additions & 2 deletions lwt-unix/dune
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(library
(name httpaf_lwt_unix)
(public_name httpaf-lwt-unix)
(libraries faraday-lwt-unix httpaf lwt.unix)
(flags (:standard -safe-string)))
(libraries faraday-lwt-unix httpaf httpaf-lwt lwt.unix)
(flags
(:standard -safe-string)))
Loading

0 comments on commit 96b9c60

Please sign in to comment.