api_tools/
lib.rs

1//! # Api Tools - A toolkit for API in Rust
2//!
3//! Toolkit for API in Rust
4//!
5//! API Tools is a Rust library providing utilities for developing robust, consistent, and secure APIs.
6//! It offers ready-to-use layers, extractors, error handling, and helpers designed to simplify API development,
7//! especially with the Axum framework.
8//! The toolkit aims to standardize common API patterns and reduce boilerplate in your Rust projects.
9//!
10//! ## Features list
11//!
12//! | Name         | Description                       | Default |
13//! | ------------ | --------------------------------- | :-----: |
14//! | `axum`       | Enable Axum feature               |   ❌    |
15//! | `prometheus` | Enable Prometheus metrics feature |   ❌    |
16//! | `full`       | Enable all features               |   ❌    |
17//!
18//! ## Components
19//!
20//! ### Value objects
21//!
22//! | Name          | Description                                                                                |
23//! | ------------- | ------------------------------------------------------------------------------------------ |
24//! | `UtcDateTime` | A wrapper around `chrono::DateTime` to handle date and time values in UTC                  |
25//! | `Timezone`    | A wrapper around `chrono_tz::Tz` to handle time zones                                      |
26//! | `Pagination`  | A struct to handle pagination parameters, including page number, page size and total count |
27//! | `QuerySort`   | A struct to handle sorting query parameters, including field and direction                 |
28//!
29//! ### Axum
30//!
31//! #### Layers
32//!
33//! | Name               | Description                                                                                                                              |
34//! | ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------- |
35//! | `BasicAuthLayer`   | Provides HTTP Basic Authentication middleware for protecting routes with username and password                                           |
36//! | `CorsLayer`        | Adds Cross-Origin Resource Sharing (CORS) headers to responses, allowing or restricting resource sharing between different origins       |
37//! | `HttpErrorsLayer`  | Middleware for intercepting and customizing HTTP error responses, enabling standardized error handling across your API                   |
38//! | `LoggerLayer`      | Logs incoming requests and outgoing responses, useful for debugging and monitoring API activity                                          |
39//! | `RequestId`        | Middleware that generates and attaches a unique request identifier (UUID) to each incoming request for traceability                      |
40//! | `TimeLimiterLayer` | Middleware that restricts API usage to specific time slots. Outside of these allowed periods, it returns a 503 Service Unavailable error |
41//! | `PrometheusLayer`  | Middleware that collects and exposes Prometheus-compatible metrics for monitoring API performance and usage                              |
42//!
43//! ##### Utility functions
44//!
45//! | Name                  | Description                                                              |
46//! | --------------------- | ------------------------------------------------------------------------ |
47//! | `body_from_parts`     | Construct a response body from `Parts`, status code, message and headers |
48//! | `header_value_to_str` | Convert `HeaderValue` to `&str`                                          |
49//!
50//! #### Extractors
51//!
52//! | Name               | Description                                                            |
53//! | ------------------ | ---------------------------------------------------------------------- |
54//! | `ExtractRequestId` | Extracts the unique request identifier (UUID) from the request headers |
55//! | `Path`             | Extracts and deserializes path parameters from the request URL         |
56//! | `Query`            | Extracts and deserializes query string parameters from the request URL |
57//!
58//! #### Response helpers
59//!
60//! | Name               | Description                                                                                                 |
61//! | ------------------ | ----------------------------------------------------------------------------------------------------------- |
62//! | `ApiSuccess`       | Represents a successful API response (Status code and data in JSON). It implements the `IntoResponse` trait |
63//! | `ApiError`         | Represents a list of HTTP errors                                                                            |
64//! | `ApiErrorResponse` | Encapsulates the details of an API error response, including the status code and the error message          |
65//!
66//! #### Handlers
67//!
68//! | Name                | Description                                                                                       |
69//! | ------------------- | ------------------------------------------------------------------------------------------------- |
70//! | `PrometheusHandler` | Handler that exposes Prometheus metrics endpoint, allowing metrics scraping by Prometheus servers |
71
72#[macro_use]
73extern crate tracing;
74
75pub mod security;
76pub mod server;
77pub mod value_objects;