funes/
lib.rs

1//! > Lo recuerdo (yo no tengo derecho a pronunciar ese verbo sagrado, sólo un
2//! > hombre en la tierra tuvo derecho y ese hombre ha muerto) con una oscura
3//! > pasionaria en la mano, viéndola como nadie la ha visto, aunque la mirara
4//! > desde el crepúsculo del día hasta el de la noche, toda una vida entera.
5//!
6//! *Jorge Luis Borges - Funes, el memorioso*
7//!
8//! ---
9//!
10//! # Funes
11//!
12//! ![pipeline workflow](https://github.com/rodmoioliveira/funes/actions/workflows/ci.yml/badge.svg)
13//! [![crates.io](https://img.shields.io/crates/v/funes.svg)](https://crates.io/crates/funes)
14//! [![docs.rs](https://docs.rs/funes/badge.svg)](https://docs.rs/funes)
15//! [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/rodmoioliveira/funes/blob/main/LICENSE)
16//! [![Changelog](https://camo.githubusercontent.com/4d89fc2186d69bdbb2c6ea6cb54ab16915be5e5e0b63a393e87a75741f1baa8c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6368616e67656c6f672d4348414e47454c4f472e6d642d253233453035373335)](https://github.com/rodmoioliveira/funes/blob/main/CHANGELOG.md)
17//! [![Code of Conduct](https://img.shields.io/badge/code-of%20conduct-blue.svg)](https://github.com/rodmoioliveira/funes/blob/main/CODE_OF_CONDUCT.md)
18//!
19//! `funes` is a server to mock HTTP responses. You might use it to:
20//!
21//! - test applications without hitting production resources;
22//! - create integrations tests for your applications;
23//!
24//! # Installation
25//!
26//! To install `funes`, you must have [rust and
27//! cargo](https://www.rust-lang.org/tools/install) installed. Then you can run:
28//!
29//! ```sh
30//! cargo install funes
31//! ```
32//!
33//! # Usage
34//!
35//! Now you can run your `funes` server in one terminal and make requests from
36//! another one:
37//!
38//! ```sh
39//! # terminal 1
40//! funes
41//!
42//! ENVS: Envs {
43//!   allow_externals: true,
44//!   h_server: "funes",
45//!   h_user_agent: "funes",
46//!   latency_collection: "",
47//!   latency_enable: false,
48//!   localhost: "0.0.0.0:8080",
49//!   log: "funes,actix_web=info",
50//!   mock_dir: "/Users/rodolfo.moi/.funes"
51//! },
52//! FUNES_LATENCY_COLLECTION: Collection {
53//!   regex: ".+",
54//!   latencies: {}
55//! }
56//! ```
57//!
58//! To mock the requests of an `{api}`, call the endpoint
59//! `http://localhost:8080/{api}`. The first request will hit the `{api}` and then
60//! store the response:
61//!
62//! ```sh
63//! # terminal 2
64//! curl http://localhost:8080/jsonplaceholder.typicode.com/todos/1
65//!
66//! # terminal 1
67//! 201 0.125973 GET /jsonplaceholder.typicode.com/todos/1 HTTP/1.1 curl/7.64.1 bytes:66
68//! ```
69//!
70//! The second request and all the subsequent ones will be served from the
71//! stored response:
72//!
73//! ```sh
74//! # terminal 2
75//! curl http://localhost:8080/jsonplaceholder.typicode.com/todos/1
76//!
77//! # terminal 1
78//! 200 0.000330 GET /jsonplaceholder.typicode.com/todos/1 HTTP/1.1 curl/7.64.1 bytes:66
79//! ```
80//!
81//! Posts are supported:
82//!
83//! ```sh
84//! # terminal 2
85//! curl \
86//!   -d '{ "userId": 1, "id": 1, "title": "title", "body": "body" }' \
87//!   -H "Content-Type: application/json" \
88//!   -X POST 0.0.0.0:8080/jsonplaceholder.typicode.com/posts
89//!
90//! # terminal 1
91//! 201 0.293888 POST /jsonplaceholder.typicode.com/posts HTTP/1.1 curl/7.64.1 bytes:51
92//! ```
93//!
94//! # Endpoints
95//!
96//! These are the endpoints of a `funes` app:
97//!
98//! - `/{api}` - Like `0.0.0.0:8080/pokeapi.co/api/v2/pokemon/1`
99//! - `/mocks` - List of all saved mocks
100//! - `/health`
101//! - `/resource-status`
102//!
103//! # Default Envs
104//!
105//! ```sh
106//! FUNES_ALLOW_EXTERNALS=true
107//! FUNES_APP=funes
108//! FUNES_HOST=0.0.0.0:8080
109//! FUNES_LOG=funes,actix_web=info
110//! FUNES_MOCK_DIR=$HOME/.funes
111//! ```
112//!
113//! # Examples
114//!
115//! All examples can be found in the
116//! [examples](https://github.com/rodmoioliveira/funes/tree/main/examples) folder:
117//!
118//! - [docker](https://github.com/rodmoioliveira/funes/tree/main/examples/docker)
119//! - [latency-collection](https://github.com/rodmoioliveira/funes/tree/main/examples/latency-collection)
120//!
121//! # Benchmarks
122//!
123//! Funes is built on top of [actix](https://actix.rs/) and is quite fast! Take a
124//! look:
125//!
126//! ```sh
127//! # https://github.com/giltene/wrk2
128//! wrk2 -t2 -c10 -d60s -R2000 --latency http://localhost:8080/pokeapi.co/api/v2/pokemon/1
129//!
130//! Running 1m test @ http://localhost:8080/pokeapi.co/api/v2/pokemon/1
131//!   2 threads and 10 connections
132//!   Thread calibration: mean lat.: 1.449ms, rate sampling interval: 10ms
133//!   Thread calibration: mean lat.: 1.469ms, rate sampling interval: 10ms
134//!   Thread Stats   Avg      Stdev     Max   +/- Stdev
135//!     Latency     1.43ms  588.92us   6.26ms   66.00%
136//!     Req/Sec     1.05k   146.60     1.67k    73.83%
137//!   Latency Distribution (HdrHistogram - Recorded Latency)
138//!  50.000%    1.39ms
139//!  75.000%    1.83ms
140//!  90.000%    2.20ms
141//!  99.000%    2.94ms
142//!  99.900%    3.58ms
143//!  99.990%    4.31ms
144//!  99.999%    6.23ms
145//! 100.000%    6.26ms
146//!
147//!   Detailed Percentile spectrum:
148//!        Value   Percentile   TotalCount 1/(1-Percentile)
149//!
150//!        0.209     0.000000            1         1.00
151//!        0.676     0.100000        10025         1.11
152//!        0.877     0.200000        20009         1.25
153//!        1.065     0.300000        30007         1.43
154//!        1.233     0.400000        39993         1.67
155//!        1.393     0.500000        50000         2.00
156//!        1.474     0.550000        54997         2.22
157//!        1.557     0.600000        59988         2.50
158//!        1.645     0.650000        64995         2.86
159//!        1.735     0.700000        69983         3.33
160//!        1.829     0.750000        74984         4.00
161//!        1.878     0.775000        77482         4.44
162//!        1.929     0.800000        80006         5.00
163//!        1.982     0.825000        82477         5.71
164//!        2.042     0.850000        84989         6.67
165//!        2.113     0.875000        87505         8.00
166//!        2.151     0.887500        88721         8.89
167//!        2.197     0.900000        89961        10.00
168//!        2.249     0.912500        91232        11.43
169//!        2.307     0.925000        92483        13.33
170//!        2.371     0.937500        93726        16.00
171//!        2.407     0.943750        94329        17.78
172//!        2.447     0.950000        94972        20.00
173//!        2.491     0.956250        95597        22.86
174//!        2.537     0.962500        96213        26.67
175//!        2.595     0.968750        96846        32.00
176//!        2.629     0.971875        97146        35.56
177//!        2.665     0.975000        97453        40.00
178//!        2.703     0.978125        97764        45.71
179//!        2.753     0.981250        98085        53.33
180//!        2.809     0.984375        98397        64.00
181//!        2.841     0.985938        98556        71.11
182//!        2.875     0.987500        98703        80.00
183//!        2.919     0.989062        98861        91.43
184//!        2.967     0.990625        99017       106.67
185//!        3.027     0.992188        99175       128.00
186//!        3.057     0.992969        99252       142.22
187//!        3.083     0.993750        99326       160.00
188//!        3.117     0.994531        99406       182.86
189//!        3.157     0.995313        99482       213.33
190//!        3.209     0.996094        99561       256.00
191//!        3.239     0.996484        99601       284.44
192//!        3.271     0.996875        99639       320.00
193//!        3.297     0.997266        99677       365.71
194//!        3.335     0.997656        99719       426.67
195//!        3.375     0.998047        99755       512.00
196//!        3.415     0.998242        99775       568.89
197//!        3.455     0.998437        99794       640.00
198//!        3.499     0.998633        99815       731.43
199//!        3.533     0.998828        99833       853.33
200//!        3.587     0.999023        99853      1024.00
201//!        3.617     0.999121        99863      1137.78
202//!        3.645     0.999219        99873      1280.00
203//!        3.711     0.999316        99882      1462.86
204//!        3.781     0.999414        99892      1706.67
205//!        3.835     0.999512        99902      2048.00
206//!        3.875     0.999561        99907      2275.56
207//!        3.915     0.999609        99911      2560.00
208//!        3.933     0.999658        99916      2925.71
209//!        3.963     0.999707        99921      3413.33
210//!        4.025     0.999756        99926      4096.00
211//!        4.107     0.999780        99929      4551.11
212//!        4.115     0.999805        99931      5120.00
213//!        4.143     0.999829        99933      5851.43
214//!        4.191     0.999854        99936      6826.67
215//!        4.247     0.999878        99938      8192.00
216//!        4.311     0.999890        99940      9102.22
217//!        5.243     0.999902        99941     10240.00
218//!        5.371     0.999915        99942     11702.86
219//!        5.499     0.999927        99943     13653.33
220//!        5.551     0.999939        99944     16384.00
221//!        5.639     0.999945        99945     18204.44
222//!        5.855     0.999951        99946     20480.00
223//!        5.855     0.999957        99946     23405.71
224//!        5.935     0.999963        99947     27306.67
225//!        5.935     0.999969        99947     32768.00
226//!        6.107     0.999973        99948     36408.89
227//!        6.107     0.999976        99948     40960.00
228//!        6.107     0.999979        99948     46811.43
229//!        6.227     0.999982        99949     54613.33
230//!        6.227     0.999985        99949     65536.00
231//!        6.227     0.999986        99949     72817.78
232//!        6.227     0.999988        99949     81920.00
233//!        6.227     0.999989        99949     93622.86
234//!        6.263     0.999991        99950    109226.67
235//!        6.263     1.000000        99950          inf
236//! #[Mean    =        1.428, StdDeviation   =        0.589]
237//! #[Max     =        6.260, Total count    =        99950]
238//! #[Buckets =           27, SubBuckets     =         2048]
239//! ----------------------------------------------------------
240//!   119982 requests in 1.00m, 22.30GB read
241//! Requests/sec:   1999.67
242//! Transfer/sec:    380.54MB
243//! ```
244
245mod config;
246mod error;
247mod fetch;
248mod format;
249mod handlers;
250mod io;
251mod latency;
252pub mod server;
253mod statics;
254mod utils;