rustapi_extras/
lib.rs

1//! # rustapi-extras
2//!
3//! Optional security and utility features for the RustAPI framework.
4//!
5//! This crate provides production-ready middleware and utilities that are
6//! opt-in via Cargo feature flags to minimize binary size when not needed.
7//!
8//! ## Features
9//!
10//! - `jwt` - JWT authentication middleware and `AuthUser<T>` extractor
11//! - `cors` - CORS middleware with builder pattern configuration
12//! - `rate-limit` - IP-based rate limiting middleware
13//! - `config` - Configuration management with `.env` file support
14//! - `cookies` - Cookie parsing extractor
15//! - `sqlx` - SQLx database error conversion to ApiError
16//! - `insight` - Traffic insight middleware for analytics and debugging
17//! - `extras` - Meta feature enabling jwt, cors, and rate-limit
18//! - `full` - All features enabled
19//!
20//! ## Example
21//!
22//! ```toml
23//! [dependencies]
24//! rustapi-extras = { version = "0.1", features = ["jwt", "cors", "insight"] }
25//! ```
26
27#![warn(missing_docs)]
28#![warn(rustdoc::missing_crate_level_docs)]
29
30// JWT authentication module
31#[cfg(feature = "jwt")]
32pub mod jwt;
33
34// CORS middleware module
35#[cfg(feature = "cors")]
36pub mod cors;
37
38// Rate limiting module
39#[cfg(feature = "rate-limit")]
40pub mod rate_limit;
41
42// Configuration management module
43#[cfg(feature = "config")]
44pub mod config;
45
46// SQLx database integration module
47#[cfg(feature = "sqlx")]
48pub mod sqlx;
49
50// Diesel database integration module
51#[cfg(feature = "diesel")]
52pub mod diesel;
53
54// Traffic insight module
55#[cfg(feature = "insight")]
56pub mod insight;
57
58// Request timeout middleware
59#[cfg(feature = "timeout")]
60pub mod timeout;
61
62// Request guards (authorization)
63#[cfg(feature = "guard")]
64pub mod guard;
65
66// Request/Response logging middleware
67#[cfg(feature = "logging")]
68pub mod logging;
69
70// Circuit breaker middleware
71#[cfg(feature = "circuit-breaker")]
72pub mod circuit_breaker;
73
74// Retry middleware
75#[cfg(feature = "retry")]
76pub mod retry;
77
78// Request deduplication
79#[cfg(feature = "dedup")]
80pub mod dedup;
81
82// Input sanitization
83#[cfg(feature = "sanitization")]
84pub mod sanitization;
85
86// Security headers middleware
87#[cfg(feature = "security-headers")]
88pub mod security_headers;
89
90// API Key authentication
91#[cfg(feature = "api-key")]
92pub mod api_key;
93
94// Response caching
95#[cfg(feature = "cache")]
96pub mod cache;
97
98// OpenTelemetry integration
99#[cfg(feature = "otel")]
100pub mod otel;
101
102// Structured logging
103#[cfg(feature = "structured-logging")]
104pub mod structured_logging;
105
106// Re-exports for convenience
107#[cfg(feature = "jwt")]
108pub use jwt::{create_token, AuthUser, JwtError, JwtLayer, JwtValidation, ValidatedClaims};
109
110#[cfg(feature = "cors")]
111pub use cors::{AllowedOrigins, CorsLayer};
112
113#[cfg(feature = "rate-limit")]
114pub use rate_limit::RateLimitLayer;
115
116#[cfg(feature = "config")]
117pub use config::{
118    env_or, env_parse, load_dotenv, load_dotenv_from, require_env, try_require_env, Config,
119    ConfigError, Environment,
120};
121
122#[cfg(feature = "sqlx")]
123pub use sqlx::{convert_sqlx_error, PoolError, SqlxErrorExt, SqlxPoolBuilder, SqlxPoolConfig};
124
125#[cfg(feature = "diesel")]
126pub use diesel::{DieselPoolBuilder, DieselPoolConfig, DieselPoolError};
127
128#[cfg(feature = "insight")]
129pub use insight::{
130    InMemoryInsightStore, InsightConfig, InsightData, InsightLayer, InsightStats, InsightStore,
131};
132
133// Phase 11 re-exports
134#[cfg(feature = "timeout")]
135pub use timeout::TimeoutLayer;
136
137#[cfg(feature = "guard")]
138pub use guard::{PermissionGuard, RoleGuard};
139
140#[cfg(feature = "logging")]
141pub use logging::{LogFormat, LoggingConfig, LoggingLayer};
142
143#[cfg(feature = "circuit-breaker")]
144pub use circuit_breaker::{CircuitBreakerLayer, CircuitBreakerStats, CircuitState};
145
146#[cfg(feature = "retry")]
147pub use retry::{RetryLayer, RetryStrategy};
148
149#[cfg(feature = "security-headers")]
150pub use security_headers::{HstsConfig, ReferrerPolicy, SecurityHeadersLayer, XFrameOptions};
151
152#[cfg(feature = "api-key")]
153pub use api_key::ApiKeyLayer;
154
155#[cfg(feature = "cache")]
156pub use cache::{CacheConfig, CacheLayer};
157
158#[cfg(feature = "dedup")]
159pub use dedup::{DedupConfig, DedupLayer};
160
161#[cfg(feature = "sanitization")]
162pub use sanitization::{sanitize_html, sanitize_json, strip_tags};
163
164// Phase 5: Observability re-exports
165#[cfg(feature = "otel")]
166pub use otel::{
167    extract_trace_context, inject_trace_context, propagate_trace_context, OtelConfig,
168    OtelConfigBuilder, OtelExporter, OtelLayer, TraceContext, TraceSampler,
169};
170
171#[cfg(feature = "structured-logging")]
172pub use structured_logging::{
173    DatadogFormatter, JsonFormatter, LogFormatter, LogOutputFormat, LogfmtFormatter,
174    SplunkFormatter, StructuredLoggingConfig, StructuredLoggingConfigBuilder,
175    StructuredLoggingLayer,
176};
177
178// Phase 6: Security features
179#[cfg(feature = "csrf")]
180pub mod csrf;
181
182#[cfg(feature = "csrf")]
183pub use csrf::{CsrfConfig, CsrfLayer, CsrfToken};
184
185#[cfg(feature = "oauth2-client")]
186pub mod oauth2;
187
188#[cfg(feature = "oauth2-client")]
189pub use oauth2::{
190    AuthorizationRequest, CsrfState, OAuth2Client, OAuth2Config, PkceVerifier, Provider,
191    TokenError, TokenResponse,
192};
193
194#[cfg(feature = "audit")]
195pub mod audit;
196
197#[cfg(feature = "audit")]
198pub use audit::{
199    AuditAction, AuditEvent, AuditQuery, AuditQueryBuilder, AuditSeverity, AuditStore,
200    ComplianceInfo, FileAuditStore, InMemoryAuditStore,
201};