39 lines
796 B
OCaml
39 lines
796 B
OCaml
open Lwt.Infix
|
|
open Lwt.Syntax
|
|
|
|
let successful = ref 0
|
|
let failed = ref 0
|
|
|
|
(* middleware *)
|
|
let count_requests inner_handler request =
|
|
try%lwt
|
|
(inner_handler request) >>= (fun response ->
|
|
successful := !successful + 1;
|
|
Lwt.return response)
|
|
|
|
with exn ->
|
|
failed := !failed + 1;
|
|
raise exn
|
|
|
|
let () =
|
|
Dream.run
|
|
@@ Dream.logger
|
|
@@ count_requests
|
|
@@ Dream.router [
|
|
Dream.get "/"
|
|
(fun _ ->
|
|
Dream.html "Hello, world!");
|
|
|
|
Dream.get "/error"
|
|
(fun _ ->
|
|
failwith "failed on purpose");
|
|
|
|
Dream.get "/count"
|
|
(fun _ ->
|
|
Dream.html (Printf.sprintf "%3i successful requests<br>%3i failed requests" !successful !failed));
|
|
|
|
Dream.get "/echo/:word"
|
|
(fun request ->
|
|
Dream.html (Dream.param request "word"));
|
|
]
|