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.
§Arguments
addr- Address to listen on (e.g., “127.0.0.1:8080” or “0.0.0.0:8080”)
§Example
proxy.start("127.0.0.1:8080").await?;To run in background:
let handle = tokio::spawn(async move {
if let Err(e) = proxy.start("127.0.0.1:8080").await {
eprintln!("Proxy error: {}", e);
}
});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
This is a convenience method if you already have a parsed SocketAddr.
§Arguments
addr- Parsed SocketAddr to listen on
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)
This atomically replaces the configuration. New connections will use the updated configuration immediately. Existing connections will continue to use their original configuration snapshot.
§Arguments
config- New configuration to use