package sihl-web

  1. Overview
  2. Docs
Contains HTTP server implementations as Sihl service

Install

Dune Dependency

Authors

Maintainers

Sources

sihl-0.2.2.tbz
sha256=cfdbfc4b5b122d5be73480dc6050ea19f970c918d19ecfde1fe1f3b152154d28
sha512=2bcb39b1e7e98892d65ded542b143259156671106e7a30ad5211c22ec54b87047320c2cf6c115db1931a563ff8204e9f420cf3edfc1559d4c9ce53b0ef40e529

doc/src/sihl-web/middleware_id.ml.html

Source file middleware_id.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Core = Sihl_core

let key : string Opium.Context.key =
  Opium.Context.Key.create ("id", Sexplib.Std.sexp_of_string)
;;

exception Id_not_found

let find req =
  try Opium.Context.find_exn key req.Opium.Request.env with
  | _ ->
    Logs.err (fun m -> m "No id found");
    Logs.info (fun m -> m "Have you applied the ID middleware for this route?");
    raise @@ Id_not_found
;;

let find_opt req =
  try Some (find req) with
  | _ -> None
;;

let set id req =
  let env = req.Opium.Request.env in
  let env = Opium.Context.add key id env in
  { req with env }
;;

let m () =
  let filter handler req =
    let id = Core.Random.bytes ~nr:32 |> List.to_seq |> String.of_seq in
    let req = set id req in
    handler req
  in
  Rock.Middleware.create ~name:"id" ~filter
;;
OCaml

Innovation. Community. Security.