MTE unikernel with Mkernel+Mnet+Vifu
This commit is contained in:
parent
76662bf23c
commit
a23e59dfca
31 changed files with 535 additions and 376 deletions
50
src/mte.ml
50
src/mte.ml
|
|
@ -14,17 +14,22 @@
|
|||
along with this program. If not, see <https://www.gnu.org/licenses/>. *)
|
||||
|
||||
let hello req _server _env =
|
||||
let open Vif.Response in
|
||||
let open Vifu.Response in
|
||||
let open Syntax in
|
||||
let* () = with_string req "Hello~~\n" in
|
||||
let* () = add ~field:"content-type" "text/plain" in
|
||||
respond `OK
|
||||
|
||||
(* TODO nice routes typing
|
||||
to enforce json verification
|
||||
to specify handlers responses data and status codes
|
||||
|
||||
use Vif.Uri.conv *)
|
||||
let routes =
|
||||
let open Vif.Uri in
|
||||
let open Vif.Route in
|
||||
let open Vifu.Uri in
|
||||
let open Vifu.Route in
|
||||
let get path = get (path /?? any) in
|
||||
let post path jsont = post (Vif.Type.json_encoding jsont) (path /?? any) in
|
||||
let post path jsont = post (Vifu.Type.json_encoding jsont) (path /?? any) in
|
||||
let v s = rel / s in
|
||||
let tos =
|
||||
[
|
||||
|
|
@ -64,20 +69,35 @@ let routes =
|
|||
in
|
||||
tos @ status_info @ management
|
||||
|
||||
module RNG = Mirage_crypto_rng.Fortuna
|
||||
|
||||
let () =
|
||||
let ( let@ ) finally fn = Fun.protect ~finally fn in
|
||||
Util.Log_reporter.setup ();
|
||||
let cfg =
|
||||
let port = Config.Exchange.port in
|
||||
let sockaddr = Unix.(ADDR_INET (inet_addr_loopback, port)) in
|
||||
Vif.config ~reporter:Util.Log_reporter.reporter sockaddr
|
||||
let rng =
|
||||
let rng () = Mirage_crypto_rng_mkernel.initialize (module RNG) in
|
||||
Mkernel.map rng Mkernel.[]
|
||||
in
|
||||
Miou_unix.run @@ fun () ->
|
||||
Caqti_miou.Switch.run @@ fun caqti_switch ->
|
||||
let env : Devices.env =
|
||||
{ caqti_switch; db_uri= Config.Exchangedb_postgres.config }
|
||||
let storage = Mkernel.block "storage" in
|
||||
let service =
|
||||
let ipv4 = Ipaddr.V4.Prefix.of_string_exn "10.0.0.2/24" in
|
||||
Mnet.stack ~name:"service" ipv4
|
||||
in
|
||||
let devices = Vif.Devices.[ Devices.db_connection; Devices.keys ] in
|
||||
let middlewares = Vif.Middlewares.[] in
|
||||
Mkernel.(run [ rng; storage; service ])
|
||||
@@ fun rng storage (stack, tcp, udp) () ->
|
||||
let@ () = fun () -> Mirage_crypto_rng_mkernel.kill rng in
|
||||
let@ () = fun () -> Mnet.kill stack in
|
||||
let hed, he = Mnet_happy_eyeballs.create tcp in
|
||||
let@ () = fun () -> Mnet_happy_eyeballs.kill hed in
|
||||
let dns = Mnet_dns.create (udp, he) in
|
||||
let t = Mnet_dns.transport dns in
|
||||
let@ () = fun () -> Mnet_dns.Transport.kill t in
|
||||
Caqti_miou.Switch.run @@ fun sw ->
|
||||
(* -- *)
|
||||
let fs = Fat.create storage in
|
||||
let env = Env.{ sw; stack; tcp; dns; fs } in
|
||||
let devices = Vifu.Devices.[ Global.db_conn; Global.keys ] in
|
||||
let cfg = Vifu.Config.v Config.Exchange.port in
|
||||
Logs.info (fun m ->
|
||||
m ~tags:(Util.Log_reporter.detail "...") "Starting MTE server");
|
||||
Vif.run ~cfg ~devices ~middlewares routes env
|
||||
Vifu.run ~cfg ~devices tcp routes env
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue