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//! #### Security
32//!
33//! | Name | Description |
34//! |---------------|----------------------------------|
35//! | `Jwt` | A wrapper for JWT generation and parsing |
36//!
37//! #### Layers
38//!
39//! | Name | Description |
40//! | ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
41//! | `BasicAuthLayer` | Provides HTTP Basic Authentication middleware for protecting routes with username and password |
42//! | `CorsLayer` | Adds Cross-Origin Resource Sharing (CORS) headers to responses, allowing or restricting resource sharing between different origins |
43//! | `HttpErrorsLayer` | Middleware for intercepting and customizing HTTP error responses, enabling standardized error handling across your API |
44//! | `LoggerLayer` | Logs incoming requests and outgoing responses, useful for debugging and monitoring API activity |
45//! | `RequestId` | Middleware that generates and attaches a unique request identifier (UUID) to each incoming request for traceability |
46//! | `TimeLimiterLayer` | Middleware that restricts API usage to specific time slots. Outside of these allowed periods, it returns a 503 Service Unavailable error |
47//! | `PrometheusLayer` | Middleware that collects and exposes Prometheus-compatible metrics for monitoring API performance and usage |
48//! | `SecurityHeadersLayer` | Middleware add security headers like (CSP, etc.) |
49//!
50//! ##### Utility functions
51//!
52//! | Name | Description |
53//! | --------------------- | ------------------------------------------------------------------------ |
54//! | `body_from_parts` | Construct a response body from `Parts`, status code, message and headers |
55//! | `header_value_to_str` | Convert `HeaderValue` to `&str` |
56//!
57//! #### Extractors
58//!
59//! | Name | Description |
60//! | ------------------ | ---------------------------------------------------------------------- |
61//! | `ExtractRequestId` | Extracts the unique request identifier (UUID) from the request headers |
62//! | `Path` | Extracts and deserializes path parameters from the request URL |
63//! | `Query` | Extracts and deserializes query string parameters from the request URL |
64//!
65//! #### Response helpers
66//!
67//! | Name | Description |
68//! | ------------------ | ----------------------------------------------------------------------------------------------------------- |
69//! | `ApiSuccess` | Represents a successful API response (Status code and data in JSON). It implements the `IntoResponse` trait |
70//! | `ApiError` | Represents a list of HTTP errors |
71//! | `ApiErrorResponse` | Encapsulates the details of an API error response, including the status code and the error message |
72//!
73//! #### Handlers
74//!
75//! | Name | Description |
76//! | ------------------- | ------------------------------------------------------------------------------------------------- |
77//! | `PrometheusHandler` | Handler that exposes Prometheus metrics endpoint, allowing metrics scraping by Prometheus servers |
78
79#[allow(unused_imports)]
80#[macro_use]
81extern crate tracing;
82
83pub mod server;
84pub mod value_objects;