postrust-proxy 0.5.1

Reverse proxy for Postrust with load balancing and automatic TLS certificates
Documentation
//! # Postrust Proxy
//!
//! High-performance reverse proxy module for Postrust with:
//! - HTTP/1.1 and HTTP/2 support
//! - Load balancing (round-robin, random, least-connections, weighted, sticky)
//! - Active health checking
//! - Rate limiting
//! - Automatic TLS via Let's Encrypt (ACME)
//! - Zero-downtime configuration updates
//!
//! ## Architecture
//!
//! The proxy is built on vendored code from [rust-rpxy](https://github.com/junkurihara/rust-rpxy),
//! with additional features for database-backed configuration, health checking, and rate limiting.
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                     Postrust Proxy                          │
//! ├─────────────────────────────────────────────────────────────┤
//! │  ┌─────────────┐  ┌─────────────┐  ┌─────────────────────┐  │
//! │  │   Config    │  │   Health    │  │     Rate Limit      │  │
//! │  │ TOML + DB   │  │   Checker   │  │    Token Bucket     │  │
//! │  └──────┬──────┘  └──────┬──────┘  └──────────┬──────────┘  │
//! │         │                │                    │              │
//! │         └────────────────┼────────────────────┘              │
//! │                          │                                   │
//! │              ┌───────────▼───────────┐                       │
//! │              │    Vendored Core      │                       │
//! │              │  (from rust-rpxy)     │                       │
//! │              │  - Proxy handler      │                       │
//! │              │  - Load balancer      │                       │
//! │              │  - HTTP forwarding    │                       │
//! │              └───────────────────────┘                       │
//! └─────────────────────────────────────────────────────────────┘
//! ```

#![warn(clippy::all)]
#![warn(missing_docs)]
// `result_large_err` and `large_enum_variant` are both rooted in the rich
// `ProxyError` enum, which is deliberate: an error that says which upstream and
// why is worth more than a small one. `type_complexity` covers a few internal
// signatures waiting to become type aliases.
#![allow(clippy::result_large_err)]
#![allow(clippy::large_enum_variant)]
#![allow(clippy::type_complexity)]

pub mod admin;
pub mod config;
pub mod health;
pub mod ratelimit;
pub mod saas;
pub mod tls;
pub mod vendored;

mod error;

pub use error::{ProxyError, ProxyResult};

// Re-export key types for convenience
pub use config::{Backend, ProxyConfig, Route, Upstream};
pub use health::HealthChecker;
pub use ratelimit::RateLimiter;

/// Proxy server state shared across handlers.
pub struct ProxyState {
    /// Database connection pool
    pub pool: sqlx::PgPool,
    /// Current proxy configuration
    pub config: std::sync::Arc<tokio::sync::RwLock<ProxyConfig>>,
    /// Health checker instance
    pub health_checker: std::sync::Arc<HealthChecker>,
    /// Rate limiter instance
    pub rate_limiter: std::sync::Arc<RateLimiter>,
}

impl ProxyState {
    /// Create a new proxy state.
    pub async fn new(pool: sqlx::PgPool, config: ProxyConfig) -> ProxyResult<Self> {
        let rate_limit_defaults = config.rate_limit.clone();
        let config = std::sync::Arc::new(tokio::sync::RwLock::new(config));
        let health_checker = std::sync::Arc::new(HealthChecker::new());
        let rate_limiter = std::sync::Arc::new(RateLimiter::new(rate_limit_defaults));

        Ok(Self {
            pool,
            config,
            health_checker,
            rate_limiter,
        })
    }
}