Skip to main content

tiny_proxy/
lib.rs

1//! Tiny Proxy Server - Embeddable HTTP Reverse Proxy
2//!
3//! This library provides a lightweight, configurable HTTP reverse proxy
4//! that can be embedded into Rust applications or run as a standalone CLI tool.
5//!
6//! ## Features
7//!
8//! - Configuration via Caddy-like syntax
9//! - Path-based routing with pattern matching
10//! - Header manipulation
11//! - URI rewriting
12//! - HTTP/HTTPS backend support
13//!
14//! ## Example (Library Mode)
15//!
16//! ```no_run
17//! use tiny_proxy::{Config, Proxy};
18//!
19//! #[tokio::main]
20//! async fn main() -> anyhow::Result<()> {
21//!     // Load configuration from file
22//!     let config = Config::from_file("config.caddy")?;
23//!
24//!     // Create and start proxy
25//!     let proxy = Proxy::new(config);
26//!     proxy.start("127.0.0.1:8080").await?;
27//!
28//!     Ok(())
29//! }
30//! ```
31//!
32//! ## Example (Background Execution)
33//!
34//! To run the proxy in the background while doing other work:
35//!
36//! ```no_run
37//! use tiny_proxy::{Config, Proxy};
38//!
39//! #[tokio::main]
40//! async fn main() -> anyhow::Result<()> {
41//!     let config = Config::from_file("config.caddy")?;
42//!     let proxy = Proxy::new(config);
43//!
44//!     // Spawn proxy in background
45//!     let handle = tokio::spawn(async move {
46//!         if let Err(e) = proxy.start("127.0.0.1:8080").await {
47//!             eprintln!("Proxy error: {}", e);
48//!         }
49//!     });
50//!
51//!     // Do other work here...
52//!
53//!     handle.await?;
54//!     Ok(())
55//! }
56//! ```
57//!
58//! ## Example (CLI Mode)
59//!
60//! When built as a binary, the proxy can be run from command line:
61//!
62//! ```bash
63//! tiny-proxy --config config.caddy --addr 127.0.0.1:8080
64//! ```
65//!
66//! ## Configuration Format
67//!
68//! The proxy uses a Caddy-like configuration format:
69//!
70//! ```text
71//! localhost:8080 {
72//!     reverse_proxy backend:3000
73//!     header X-Forwarded-For {remote_ip}
74//! }
75//! ```
76//!
77//! For more configuration options, see the [config] module documentation.
78
79#[cfg(feature = "cli")]
80pub mod cli;
81pub mod config;
82pub mod error;
83pub mod proxy;
84
85#[cfg(feature = "api")]
86pub mod api;
87
88// Re-export commonly used types for convenience
89pub use config::Config;
90pub use error::{ProxyError, Result};
91pub use proxy::{ActionResult, Proxy};
92
93#[cfg(feature = "api")]
94pub use api::start_api_server;