FastCGI implementation in OCaml
1(* Simple authorization server *)
2
3open Fastcgi
4
5let auth_logic auth_req =
6 (* Simple authorization: allow only GET requests to /public/* paths *)
7 match auth_req.Authorizer.method_, auth_req.uri with
8 | "GET", uri when String.starts_with ~prefix:"/public/" uri ->
9 Authorizer.Authorized [("USER_AUTHORIZED", "true"); ("ACCESS_LEVEL", "public")]
10 | "GET", "/login" ->
11 Authorizer.Authorized [("LOGIN_PAGE", "true")]
12 | _ ->
13 Authorizer.Unauthorized {
14 status = 403;
15 headers = [("Content-Type", "text/html")];
16 body = "<h1>403 Forbidden</h1><p>Access denied. Only GET requests to /public/* are allowed.</p>";
17 }
18
19let auth_handler request response =
20 let auth_req = Authorizer.request_of_fastcgi request in
21 let result = auth_logic auth_req in
22 Authorizer.response_of_result result response
23
24let () = Eio_main.run @@ fun env ->
25 let net = Eio.Stdenv.net env in
26 Eio.Switch.run @@ fun sw ->
27 Server.run_default ~sw ~net
28 ~handler:(Handler.Authorizer auth_handler)
29 ~listen_address:(`Tcp ("127.0.0.1", 9002))