Skip to content

Commit

Permalink
update dependencies, use digestif for md5 and sha1, update deprecatio…
Browse files Browse the repository at this point in the history
…ns of Term and remove dependency to mirage-crypto
  • Loading branch information
mabiede committed Jan 23, 2025
1 parent 04d6fee commit e87f913
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 50 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ $ dune build example/simple_middleware/main.ml
```

Here we also use the ability of Opium to generate a cmdliner term to run your
app. Run your executable with `--help` to see the options that are available to you.
app. Run your executable with `-h` to see the options that are available to you.
For example:

```
# run in debug mode on port 9000
$ dune exec example/simple_middleware/main.exe -- -p 9000 -d
$ dune exec dune build example/simple_middleware/main.exe -- -p 9000 -d
```
12 changes: 6 additions & 6 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
(package
(name rock)
(synopsis
"Minimalist framework to build extensible HTTP servers and clients")
"Minimalist framework to build extensible HTTP servers and clients")
(description
"Rock is a Unix indpendent API to build extensible HTTP servers and clients. It provides building blocks such as middlewares and handlers (a.k.a controllers).")
"Rock is a Unix indpendent API to build extensible HTTP servers and clients. It provides building blocks such as middlewares and handlers (a.k.a controllers).")
(depends
(ocaml
(>= 4.08))
Expand All @@ -37,7 +37,7 @@
(name opium)
(synopsis "OCaml web framework")
(description
"Opium is a web framework for OCaml that provides everything you need to build safe, fast and extensible web applications.")
"Opium is a web framework for OCaml that provides everything you need to build safe, fast and extensible web applications.")
(depends
(ocaml
(>= 4.08))
Expand All @@ -54,7 +54,7 @@
magic-mime
yojson
tyxml
mirage-crypto
digestif
(base64
(>= 3.0.0))
astring
Expand All @@ -71,7 +71,7 @@
(name opium-testing)
(synopsis "Testing library for Opium")
(description
"A library that provides helpers to easily test your Opium applications.")
"A library that provides helpers to easily test your Opium applications.")
(depends
(ocaml
(>= 4.08))
Expand All @@ -85,7 +85,7 @@
(name opium-graphql)
(synopsis "Run GraphQL servers with Opium")
(description
"This package allows you to execute Opium requests against GraphQL schemas built with `graphql`.")
"This package allows you to execute Opium requests against GraphQL schemas built with `graphql`.")
(depends
(ocaml
(>= 4.08))
Expand Down
7 changes: 5 additions & 2 deletions example/graphql/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ module Schema = struct

let user : (context, user option) Graphql_lwt.Schema.typ =
Schema.(
obj "user" ~doc:"A user in the system" ~fields:(fun _ ->
obj
"user"
~doc:"A user in the system"
~fields:
[ field
"id"
~doc:"Unique user identifier"
Expand All @@ -46,7 +49,7 @@ module Schema = struct
~typ:(non_null role)
~args:Arg.[]
~resolve:(fun _info p -> p.role)
]))
])
;;

let schema =
Expand Down
5 changes: 2 additions & 3 deletions opium-graphql/src/opium_graphql.ml
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,8 @@ let make_handler
let graphiql_etag =
Asset.read "graphiql.html"
|> Option.get
|> Cstruct.of_string
|> Mirage_crypto.Hash.digest `MD5
|> Cstruct.to_string
|> Digestif.MD5.digest_string
|> Digestif.MD5.to_raw_string
|> Base64.encode_exn
;;

Expand Down
2 changes: 1 addition & 1 deletion opium.opam
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ depends: [
"magic-mime"
"yojson"
"tyxml"
"mirage-crypto"
"digestif"
"base64" {>= "3.0.0"}
"astring"
"re"
Expand Down
35 changes: 17 additions & 18 deletions opium/src/app.ml
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ module Cmds = struct
let term =
let open Cmdliner.Term in
fun app ->
pure setup_app
$ pure app
const setup_app
$ const app
$ port app.port
$ jobs app.jobs
$ host app.host
Expand All @@ -354,31 +354,30 @@ module Cmds = struct

let info name =
let doc = Printf.sprintf "%s (Opium App)" name in
let man = [] in
Term.info name ~doc ~man
Cmd.info name ~doc ~man:[]
;;
end

let run_command' app =
let open Cmdliner in
let cmd = Cmds.term app in
match Term.eval (cmd, Cmds.info app.name) with
| `Ok a ->
let cmd = Cmd.v (Cmds.info app.name) (Cmds.term app) in
match Cmd.eval_value cmd with
| Ok (`Ok a) ->
Lwt.async (fun () ->
let* _server = start a in
Lwt.return_unit);
let* _server = start a in
Lwt.return_unit);
let forever, _ = Lwt.wait () in
`Ok forever
| `Error _ -> `Error
| _ -> `Not_running
| Error _ -> `Error
| Ok (`Version | `Help) -> `Not_running
;;

let run_command app =
match app |> run_command' with
| `Ok a ->
Lwt.async (fun () ->
let* _server = a in
Lwt.return_unit);
let* _server = a in
Lwt.return_unit);
let forever, _ = Lwt.wait () in
Lwt_main.run forever
| `Error -> exit 1
Expand All @@ -387,9 +386,9 @@ let run_command app =

let run_multicore app =
let open Cmdliner in
let cmd = Cmds.term app in
match Term.eval (cmd, Cmds.info app.name) with
| `Ok a -> start_multicore a
| `Error _ -> exit 1
| _ -> exit 0
let cmd = Cmd.v (Cmds.info app.name) (Cmds.term app) in
match Cmd.eval_value cmd with
| Ok (`Ok a) -> start_multicore a
| Error _ -> exit 1
| Ok (`Version | `Help) -> exit 0
;;
13 changes: 3 additions & 10 deletions opium/src/cookie.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,11 @@ module Signer = struct
else constant_time_compare' a b 0
;;

let derive_key t =
Mirage_crypto.Hash.mac
`SHA1
~key:(Cstruct.of_string t.secret)
(Cstruct.of_string t.salt)
;;
let derive_key t = Digestif.SHA1.(hmac_string ~key:t.secret t.salt |> to_hex)

let get_signature t value =
value
|> Cstruct.of_string
|> Mirage_crypto.Hash.mac `SHA1 ~key:(derive_key t)
|> Cstruct.to_string
Digestif.SHA1.hmac_string ~key:(derive_key t) value
|> Digestif.SHA1.to_raw_string
|> Base64.encode_exn
;;

Expand Down
7 changes: 2 additions & 5 deletions opium/src/middlewares/middleware_etag.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ open Import

let etag_of_body body =
let encode s =
s
|> Cstruct.of_string
|> Mirage_crypto.Hash.digest `MD5
|> Cstruct.to_string
|> Base64.encode_exn
let open Digestif.MD5 in
s |> digest_string |> to_raw_string |> Base64.encode_exn
in
match body.Body.content with
| `String s -> Some (encode s)
Expand Down
5 changes: 2 additions & 3 deletions opium/src/middlewares/middleware_static_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ let default_etag ~local_path fname =
let* stat = Lwt_unix.stat fpath in
let hash =
Marshal.to_string stat.st_mtime []
|> Cstruct.of_string
|> Mirage_crypto.Hash.digest `MD5
|> Cstruct.to_string
|> Digestif.MD5.digest_string
|> Digestif.MD5.to_raw_string
|> Base64.encode_exn
in
Lwt.return_some hash
Expand Down

0 comments on commit e87f913

Please sign in to comment.