Expand description
Provide HTTP proxy capabilities for the Salvo web framework.
This crate allows you to easily forward requests to upstream servers, supporting both HTTP and HTTPS protocols. It’s useful for creating API gateways, load balancers, and reverse proxies.
§Example
In this example, requests to different hosts are proxied to different upstream servers:
- Requests to http://127.0.0.1:8698/ are proxied to https://www.rust-lang.org
- Requests to http://localhost:8698/ are proxied to https://crates.io
use salvo_core::prelude::*;
use salvo_proxy::Proxy;
#[tokio::main]
async fn main() {
let router = Router::new()
.push(
Router::new()
.host("127.0.0.1")
.path("{**rest}")
.goal(Proxy::use_hyper_client("https://www.rust-lang.org")),
)
.push(
Router::new()
.host("localhost")
.path("{**rest}")
.goal(Proxy::use_hyper_client("https://crates.io")),
);
let acceptor = TcpListener::new("0.0.0.0:8698").bind().await;
Server::new(acceptor).serve(router).await;
}Structs§
- Hyper
Client hyper-client - A
Clientimplementation based onhyper_util::client::legacy::Client. - Proxy
- Handler that can proxy request to other server.
- Reqwest
Client reqwest-client - A
Clientimplementation based onreqwest::Client. - Unix
Sock Client unix-sock-client - A client that creates a direct bidirectional channel (TCP tunnel) to a Unix socket.
Traits§
- Client
- Client trait for implementing different HTTP clients for proxying.
- Upstreams
- Upstreams trait for selecting target servers.
Functions§
- default_
host_ header_ getter - Default host header getter. This getter will get the host header from request uri
- default_
url_ path_ getter - Default url path getter.
- default_
url_ query_ getter - Default url query getter. This getter just return the query string from request uri.
- preserve_
original_ host_ header_ getter - Preserve original host header getter. Propagates the original request host header to the proxied request.
- rfc2616_
host_ header_ getter - RFC2616 complieant host header getter. This getter will get the host header from request uri, and add port if it’s not default port. Falls back to default upon any forward URI parse error.
Type Aliases§
- Host
Header Getter - Host header getter. You can use this to get the host header for the proxied request.
- UrlPart
Getter - Url part getter. You can use this to get the proxied url path or query.