ocaml http/1, http/2 and websocket client and server library
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

release: bump to 0.4.1 for endpoint pipelines

+158 -1
+1
.beads/last-touched
··· 1 + hcs-xht
+45
docs/guides/plug-system.md
··· 48 48 |> Endpoint.router routes 49 49 ``` 50 50 51 + You can also use the endpoint operators for direct composition: 52 + 53 + ```ocaml 54 + let endpoint = 55 + Endpoint.create config 56 + @> Plug.Logger.create ~clock log_fn 57 + @> Plug.Compress.create () 58 + @> Plug.Session.create ~store () 59 + |> Endpoint.router routes 60 + ``` 61 + 62 + Pipe-friendly helpers are also available: 63 + 64 + ```ocaml 65 + let endpoint = 66 + Endpoint.create config 67 + |> Endpoint.with_plug (Plug.Logger.create ~clock log_fn) 68 + |> Endpoint.with_plug (Plug.Compress.create ()) 69 + |> Endpoint.with_plug (Plug.Session.create ~store ()) 70 + |> Endpoint.router routes 71 + ``` 72 + 73 + Attach a plug pipeline in one step (or use the endpoint pipeline operator): 74 + 75 + ```ocaml 76 + let endpoint_pipeline = 77 + Pipeline.create 78 + [ 79 + Plug.Session.create ~store (); 80 + Plug.Csrf.create (); 81 + Plug.Compress.create (); 82 + Plug.Logger.create ~clock log_fn; 83 + ] 84 + 85 + let endpoint = 86 + Endpoint.create config 87 + |> Endpoint.with_pipeline endpoint_pipeline 88 + |> Endpoint.router routes 89 + 90 + let endpoint = 91 + Endpoint.create config 92 + @>> endpoint_pipeline 93 + |> Endpoint.router routes 94 + ``` 95 + 51 96 ## Pipeline Module 52 97 53 98 For Phoenix-style middleware composition, use `Pipeline` to create reusable plug collections:
+16
docs/plugs/overview.md
··· 47 47 let wrapped = Plug.apply pipeline handler 48 48 ``` 49 49 50 + You can also attach a pipeline to an endpoint: 51 + 52 + ```ocaml 53 + let endpoint_pipeline = 54 + Pipeline.create 55 + [ 56 + Plug.Session.create ~store (); 57 + Plug.Csrf.create (); 58 + ] 59 + 60 + let endpoint = 61 + Endpoint.create Endpoint.default_config 62 + |> Endpoint.with_pipeline endpoint_pipeline 63 + |> Endpoint.router routes 64 + ``` 65 + 50 66 Or use `Plug.run` with a list: 51 67 52 68 ```ocaml
+1 -1
dune-project
··· 2 2 3 3 (name hcs) 4 4 5 - (version 0.4.0) 5 + (version 0.4.1) 6 6 7 7 (generate_opam_files true) 8 8
+8
lib/endpoint.ml
··· 15 15 { config; plugs = Pipeline.empty; router = None; ws_handler = None } 16 16 17 17 let plug t p = { t with plugs = Pipeline.plug t.plugs p } 18 + let with_plug p t = plug t p 19 + 20 + let plug_pipeline t pipeline = 21 + { t with plugs = Pipeline.compose t.plugs pipeline } 22 + 23 + let with_pipeline pipeline t = plug_pipeline t pipeline 24 + let ( @> ) t p = plug t p 25 + let ( @>> ) t pipeline = plug_pipeline t pipeline 18 26 let router t r = { t with router = Some r } 19 27 let websocket t handler = { t with ws_handler = Some handler } 20 28 let ws_handler t = t.ws_handler
+87
test/test_plug.ml
··· 1443 1443 ] 1444 1444 end 1445 1445 1446 + module Test_endpoint_pipeline = struct 1447 + let make_request target : Hcs.Server.request = 1448 + { 1449 + meth = `GET; 1450 + target; 1451 + version = Hcs.Server.HTTP_1_1; 1452 + headers = []; 1453 + body = ""; 1454 + } 1455 + 1456 + let request_id_plug = Hcs.Plug.Request_id.create () 1457 + 1458 + let test_with_plug_pipe () = 1459 + let endpoint = 1460 + Hcs.Endpoint.create Hcs.Endpoint.default_config |> fun endpoint -> 1461 + Hcs.Endpoint.with_plug request_id_plug endpoint 1462 + in 1463 + let handler = Hcs.Endpoint.to_handler endpoint in 1464 + let resp = handler (make_request "/") in 1465 + let has_request_id = 1466 + List.exists 1467 + (fun (name, _value) -> String.lowercase_ascii name = "x-request-id") 1468 + resp.Hcs.Response.headers 1469 + in 1470 + Alcotest.check Alcotest.bool "request id header" true has_request_id 1471 + 1472 + let test_endpoint_operator_plug () = 1473 + let endpoint = 1474 + Hcs.Endpoint.create Hcs.Endpoint.default_config |> fun endpoint -> 1475 + Hcs.Endpoint.(endpoint @> request_id_plug) 1476 + in 1477 + 1478 + let handler = Hcs.Endpoint.to_handler endpoint in 1479 + let resp = handler (make_request "/") in 1480 + let has_request_id = 1481 + List.exists 1482 + (fun (name, _value) -> String.lowercase_ascii name = "x-request-id") 1483 + resp.Hcs.Response.headers 1484 + in 1485 + Alcotest.check Alcotest.bool "request id header" true has_request_id 1486 + 1487 + let test_with_pipeline_pipe () = 1488 + let endpoint_pipeline = 1489 + Hcs.Pipeline.create [ Hcs.Plug.Compress.create (); request_id_plug ] 1490 + in 1491 + let endpoint = 1492 + Hcs.Endpoint.create Hcs.Endpoint.default_config |> fun endpoint -> 1493 + Hcs.Endpoint.with_pipeline endpoint_pipeline endpoint 1494 + in 1495 + let handler = Hcs.Endpoint.to_handler endpoint in 1496 + let resp = handler (make_request "/") in 1497 + let has_request_id = 1498 + List.exists 1499 + (fun (name, _value) -> String.lowercase_ascii name = "x-request-id") 1500 + resp.Hcs.Response.headers 1501 + in 1502 + Alcotest.check Alcotest.bool "request id header" true has_request_id 1503 + 1504 + let test_endpoint_operator_pipeline () = 1505 + let endpoint_pipeline = Hcs.Pipeline.create [ request_id_plug ] in 1506 + let endpoint = 1507 + Hcs.Endpoint.create Hcs.Endpoint.default_config |> fun endpoint -> 1508 + Hcs.Endpoint.(endpoint @>> endpoint_pipeline) 1509 + in 1510 + 1511 + let handler = Hcs.Endpoint.to_handler endpoint in 1512 + let resp = handler (make_request "/") in 1513 + let has_request_id = 1514 + List.exists 1515 + (fun (name, _value) -> String.lowercase_ascii name = "x-request-id") 1516 + resp.Hcs.Response.headers 1517 + in 1518 + Alcotest.check Alcotest.bool "request id header" true has_request_id 1519 + 1520 + let tests = 1521 + [ 1522 + Alcotest.test_case "with_plug uses pipeline" `Quick test_with_plug_pipe; 1523 + Alcotest.test_case "endpoint @> adds plug" `Quick 1524 + test_endpoint_operator_plug; 1525 + Alcotest.test_case "with_pipeline attaches pipeline" `Quick 1526 + test_with_pipeline_pipe; 1527 + Alcotest.test_case "endpoint @>> adds pipeline" `Quick 1528 + test_endpoint_operator_pipeline; 1529 + ] 1530 + end 1531 + 1446 1532 module Test_static = struct 1447 1533 open Hcs.Plug.Static 1448 1534 ··· 1552 1638 ("Csrf", Test_csrf.tests); 1553 1639 ("Basic_auth", Test_basic_auth.tests); 1554 1640 ("Retry", Test_retry.tests env); 1641 + ("Endpoint_pipeline", Test_endpoint_pipeline.tests); 1555 1642 ("Static", Test_static.tests); 1556 1643 ]