Fix main.ml

This commit is contained in:
Calascibetta Romain 2025-01-19 17:54:47 +01:00
parent 2714a6d67d
commit cbc0ad960d

25
main.ml
View file

@ -1,18 +1,16 @@
#require "miou.unix" #require "miou.unix" ;;
#require "mirage-crypto-rng-miou-unix" ;;
#require "mirage-crypto-rng-miou-unix" #require "vif" ;;
#require "digestif.c" ;;
#require "vif" #require "base64" ;;
#require "digestif.c"
#require "base64"
let index server _req () = let index server _req () =
Vif.Response.with_string server `OK "Hello from an OCaml script!" Vif.Response.with_string server `OK "Hello from an OCaml script!"
;;
let test arg server _req () = let test arg server _req () =
Vif.Response.with_string server `OK (Fmt.str "%02x\n%!" arg) Vif.Response.with_string server `OK (Fmt.str "%02x\n%!" arg)
;;
let digest server req () = let digest server req () =
let ic = Vif.Request.to_stream req in let ic = Vif.Request.to_stream req in
@ -24,6 +22,7 @@ let digest server req () =
let hash = go Digestif.SHA1.empty in let hash = go Digestif.SHA1.empty in
let hash = Digestif.SHA1.to_hex hash in let hash = Digestif.SHA1.to_hex hash in
Vif.Response.with_string server `OK hash Vif.Response.with_string server `OK hash
;;
let random len server req () = let random len server req () =
let buf = Bytes.create 0x7ff in let buf = Bytes.create 0x7ff in
@ -40,24 +39,30 @@ let random len server req () =
end end
in in
go len go len
;;
let routes = let routes =
let open Vif.U in let open Vif.U in
let open Vif.R in let open Vif.R in
[ [
(rel /?? nil) --> index; (rel / "random" /% Tyre.int /?? nil) --> random (rel /?? nil) --> index
; (rel / "random" /% Tyre.int /?? nil) --> random
; (rel / "test" /% Tyre.int /?? nil) --> test ; (rel / "test" /% Tyre.int /?? nil) --> test
; (rel / "digest" /?? nil) --> digest ; (rel / "digest" /?? nil) --> digest
] ]
;;
let default target server req () = let default target server req () =
Vif.Response.with_string server `Not_found (Fmt.str "%S not found\n%!" target) Vif.Response.with_string server `Not_found (Fmt.str "%S not found\n%!" target)
;;
let my_device_as_arg, my_device = let my_device_as_arg, my_device =
Vif.D.device ~name:"my-device" ~finally:ignore [] () Vif.D.device ~name:"my-device" ~finally:ignore [] ()
;;
let () = let () =
Miou_unix.run @@ fun () -> Miou_unix.run @@ fun () ->
let sockaddr = Unix.(ADDR_INET (inet_addr_loopback, 8080)) in let sockaddr = Unix.(ADDR_INET (inet_addr_loopback, 8080)) in
let cfg = Vif.config sockaddr in let cfg = Vif.config sockaddr in
Vif.run ~cfg ~default ~devices:Vif.[ D.rng; my_device_as_arg ] routes () Vif.run ~cfg ~default ~devices:Vif.[ D.rng; my_device_as_arg ] routes ()
;;