https://github.com/danielsanchezq/warp-reverse-proxy
Fully composable warp filter that can be used as a reverse proxy.
https://github.com/danielsanchezq/warp-reverse-proxy
reverse-proxy rust warp
Last synced: 2 months ago
JSON representation
Fully composable warp filter that can be used as a reverse proxy.
- Host: GitHub
- URL: https://github.com/danielsanchezq/warp-reverse-proxy
- Owner: danielSanchezQ
- License: mit
- Created: 2020-08-07T07:44:20.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2024-04-01T04:51:47.000Z (over 1 year ago)
- Last Synced: 2025-03-31T18:19:21.265Z (6 months ago)
- Topics: reverse-proxy, rust, warp
- Language: Rust
- Homepage: https://github.com/danielSanchezQ/warp-reverse-proxy
- Size: 85.9 KB
- Stars: 48
- Watchers: 3
- Forks: 19
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# warp-reverse-proxy
[](./LICENSE)
[](https://github.com/danielsanchezq/warp-reverse-proxy/actions?query=workflow%3ACI)
[](https://docs.rs/warp-reverse-proxy)Fully composable [warp](https://github.com/seanmonstar/warp) filter that can be used as a reverse proxy. It forwards the request to the
desired address and replies back the remote address response.### Add the library dependency
```toml
[dependencies]
warp = "0.3"
warp-reverse-proxy = "1"
```### Use it as simple as:
```rust
use warp::{hyper::Body, Filter, Rejection, Reply, http::Response};
use warp_reverse_proxy::reverse_proxy_filter;async fn log_response(response: Response) -> Result {
println!("{:?}", response);
Ok(response)
}#[tokio::main]
async fn main() {
let hello = warp::path!("hello" / String).map(|name| format!("Hello, {}!", name));
// // spawn base server
tokio::spawn(warp::serve(hello).run(([0, 0, 0, 0], 8080)));
// Forward request to localhost in other port
let app = warp::path!("hello" / ..).and(
reverse_proxy_filter("".to_string(), "http://127.0.0.1:8080/".to_string())
.and_then(log_response),
);
// spawn proxy server
warp::serve(app).run(([0, 0, 0, 0], 3030)).await;
}
```### For more control. You can compose inner library filters to help you compose your own reverse proxy:
```rust
#[tokio::main]
async fn main() {
let hello = warp::path!("hello" / String).map(|name| format!("Hello port, {}!", name));// // spawn base server
tokio::spawn(warp::serve(hello).run(([0, 0, 0, 0], 8080)));let request_filter = extract_request_data_filter();
let app = warp::path!("hello" / String)
// build proxy address and base path data from current filter
.map(|port| (format!("http://127.0.0.1:{}/", port), "".to_string()))
.untuple_one()
// build the request with data from previous filters
.and(request_filter)
.and_then(proxy_to_and_forward_response)
.and_then(log_response);// spawn proxy server
warp::serve(app).run(([0, 0, 0, 0], 3030)).await;
}
```### Requests client initialization
By default, a simple `reqwests::Client` is initialized and used.
In case some specific client configuration need to be used it can be overridden:```rust
use warp_reverse_proxy::{reverse_proxy_filter, CLIENT as PROXY_CLIENT};#[tokio::main]
async fn main() {
let client = reqwest::Client::builder()
.default_headers(headers)
.build().expect("client goes boom...");
PROXY_CLIENT.set(client).expect("client couldn't be set");
...
}
```