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.
§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(())
}Implementations§
Source§impl Proxy
impl Proxy
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
Sourcepub fn config(&self) -> &Config
pub fn config(&self) -> &Config
Get a reference to current configuration
This allows inspection of current proxy configuration without modifying it.
§Returns
Reference to current 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 fn update_config(&mut self, config: Config)
pub fn update_config(&mut self, config: Config)
Update the configuration
This allows hot-reload of configuration without restarting the proxy. New connections will use the updated configuration immediately.
§Arguments
config- New configuration to use
§Note
This operation is atomic for new connections. Existing connections will continue to use their original configuration.