pub struct Proxy { /* private fields */ }Expand description
HTTP Proxy server that can be embedded into other applications
This struct encapsulates the proxy state and allows programmatic control
over the proxy lifecycle. Configuration is stored in an Arc<RwLock<Config>>
so it can be hot-reloaded at runtime (e.g. via the API server).
§Example
use tiny_proxy::{Config, Proxy};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = Config::from_file("file.caddy")?;
let proxy = Proxy::new(config);
proxy.start("127.0.0.1:8080").await?;
Ok(())
}§Hot-reload Example
use tiny_proxy::{Config, Proxy};
use std::sync::Arc;
use tokio::sync::RwLock;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let config = Config::from_file("config.caddy")?;
let proxy = Proxy::new(config);
// Get a handle to the shared config for hot-reload
let config_handle = proxy.shared_config();
// Spawn proxy in background
let handle = tokio::spawn(async move {
if let Err(e) = proxy.start("127.0.0.1:8080").await {
eprintln!("Proxy error: {}", e);
}
});
// Later, update config at runtime
let new_config = Config::from_file("updated-config.caddy")?;
{
let mut guard = config_handle.write().await;
*guard = new_config;
}
handle.await?;
Ok(())
}Implementations§
Source§impl Proxy
impl Proxy
Sourcepub fn new(config: Config) -> Self
pub fn new(config: Config) -> Self
Create a new proxy instance with the given configuration
The configuration is internally wrapped in Arc<RwLock<Config>>
so it can be shared with an API server for hot-reload.
§Arguments
config- Configuration loaded from file or constructed programmatically
§Returns
A new Proxy instance ready to be started
Create a new proxy instance from an already shared configuration
Use this when you already have an Arc<RwLock<Config>> that is
shared with an API server or other component.
§Arguments
config- Shared configuration wrapped inArc<RwLock<Config>>
Sourcepub async fn start(&self, addr: &str) -> Result<()>
pub async fn start(&self, addr: &str) -> Result<()>
Start the proxy server on the specified address
This method blocks indefinitely, handling incoming connections. To run the proxy in the background, spawn it in a tokio task.
Starts a single listener on addr. If matching sites use TLS, an HTTPS
listener is started; otherwise plain HTTP. Does not start HTTP→HTTPS
redirect servers — use Self::start_all for auto-detect multi-listener mode.
§Arguments
addr- Address to listen on (e.g., “127.0.0.1:8080” or “0.0.0.0:8443”)
§Example
proxy.start("127.0.0.1:8080").await?;Sourcepub async fn start_with_addr(&self, addr: SocketAddr) -> Result<()>
pub async fn start_with_addr(&self, addr: SocketAddr) -> Result<()>
Start the proxy server with a parsed SocketAddr
Same as Self::start: one listener on addr, HTTPS or HTTP depending on
site TLS config. No automatic HTTP→HTTPS redirect — see Self::start_all.
§Arguments
addr- Parsed SocketAddr to listen on
Sourcepub async fn start_all(&self) -> Result<()>
pub async fn start_all(&self) -> Result<()>
Start all listeners defined in the configuration (auto-detect mode).
Scans the config for all unique listen addresses and starts a listener for each. TLS sites get HTTPS listeners with SNI; non-TLS sites get HTTP.
For each distinct TLS port, also starts an HTTP→HTTPS redirect listener:
redirect_port = tls_port - 443 + 80 (e.g. 443→80, 8443→8080).
Redirect bind is best-effort: if the redirect port is in use, HTTPS still works.
Unlike Self::start / Self::start_with_addr, this method spawns multiple
listeners and redirect servers. Use this when the config defines several site
addresses (CLI without --addr).
This method blocks until all listener tasks finish (typically forever).
§Example
proxy.start_all().await?;Get a reference to the shared configuration handle
This returns a clone of the Arc<RwLock<Config>>, allowing
external code (e.g. an API server) to read and update the
configuration at runtime.
§Returns
A cloned Arc<RwLock<Config>>
Sourcepub async fn config_snapshot(&self) -> Config
pub async fn config_snapshot(&self) -> Config
Get a snapshot of the current configuration
Reads the current configuration and returns an owned clone. This is useful for inspecting config without holding a lock.
§Returns
A cloned Config
Sourcepub fn max_concurrency(&self) -> usize
pub fn max_concurrency(&self) -> usize
Sourcepub fn set_max_concurrency(&mut self, max: usize)
pub fn set_max_concurrency(&mut self, max: usize)
Sourcepub async fn update_config(&self, config: Config)
pub async fn update_config(&self, config: Config)
Update the configuration at runtime (hot-reload)
Atomically replaces routing configuration. New connections use the updated config immediately; in-flight connections keep their original snapshot.
TLS certificates are loaded when a listener starts. This method updates
site routing and directives only — not cert/key files or TlsAcceptor.
Restart the proxy (or TLS listener) to pick up new certificates.
§Arguments
config- New configuration to use