https://github.com/jofas/actix_proxy
Glue code between awc and actix-web
https://github.com/jofas/actix_proxy
actix-web rust rust-crate
Last synced: 11 months ago
JSON representation
Glue code between awc and actix-web
- Host: GitHub
- URL: https://github.com/jofas/actix_proxy
- Owner: jofas
- License: other
- Created: 2021-02-23T13:35:44.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-09T13:12:51.000Z (over 2 years ago)
- Last Synced: 2025-04-09T23:00:03.430Z (over 1 year ago)
- Topics: actix-web, rust, rust-crate
- Language: Rust
- Homepage:
- Size: 38.1 KB
- Stars: 16
- Watchers: 1
- Forks: 3
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# actix-proxy
[](https://github.com/jofas/actix_proxy/actions/workflows/build.yml)
[](https://codecov.io/gh/jofas/actix_proxy)
[](https://crates.io/crates/actix-proxy)
[](https://crates.io/crates/actix-proxy)
[](https://docs.rs/actix-proxy/latest/actix_proxy/)
[](https://opensource.org/licenses/MIT)
A rust library for the [`actix-web`](https://actix.rs/) framework.
Glues together the [`actix-web`] and [`awc`] crates.
This library provides the `IntoHttpResponse` trait which transforms
a [`awc::ClientResponse`] into a [`actix_web::HttpResponse`] and
the `SendRequestError` type bridging the gap between `awc`'s
[`SendRequestError`] and `actix-web`, by implementing
[`actix_web::ResponseError`].
Sometimes you want to implement a gateway or proxy, which makes a
request to some remote service and forwards the response to the
client that made the request.
`actix-web` integrates with the [`awc::Client`] HTTP client.
Unfortunately, [`awc::ClientResponse`], the response type of the
client request, does not implement the [`Responder`] trait.
Because of that, you can't return [`awc::ClientResponse`] from an
endpoint of your `actix-web` server.
This makes it hard to forward the response from the remote location
through an endpoint of the proxy, requiring you to transform the
response into a type that implements [`Responder`].
With the `IntoHttpResponse` trait offered by `actix-proxy`, all you
need to do is call the `into_http_response` method on your
[`awc::ClientResponse`] to forward the response from the remote
service through the proxy to the original caller.
## Example
In this example we create a basic proxy for the [duckduckgo] search
engine, simply forwarding the called url's path, query and fragment
parts to duckduckgo:
```rust
use awc::Client;
use actix_web::{get, web, HttpResponse};
use actix_proxy::{IntoHttpResponse, SendRequestError};
#[get("/{url:.*}")]
async fn proxy(
path: web::Path<(String,)>,
client: web::Data,
) -> Result {
let (url,) = path.into_inner();
let url = format!("https://duckduckgo.com/{url}");
// here we use `IntoHttpResponse` to return the request to
// duckduckgo back to the client that called this endpoint
Ok(client.get(&url).send().await?.into_http_response())
}
```
Alternatively, you can use the `into_wrapped_http_response` method
to avoid having to wrap your result in an `Ok(..)` by hand:
```rust
use awc::Client;
use actix_web::{get, web, HttpResponse};
use actix_proxy::{IntoHttpResponse, SendRequestError};
#[get("/{url:.*}")]
async fn proxy(
path: web::Path<(String,)>,
client: web::Data,
) -> Result {
let (url,) = path.into_inner();
let url = format!("https://duckduckgo.com/{url}");
// here we use `IntoHttpResponse` to return the request to
// duckduckgo back to the client that called this endpoint
client.get(&url).send().await?.into_wrapped_http_response()
}
```
[`actix-web`]: https://docs.rs/actix-web/latest/actix_web/index.html
[`actix_web::HttpResponse`]: https://docs.rs/actix-web/latest/actix_web/struct.HttpResponse.html
[`actix_web::ResponseError`]: https://docs.rs/actix-web/latest/actix_web/trait.ResponseError.html
[`awc`]: https://docs.rs/awc/latest/awc/
[`awc::Client`]: https://docs.rs/awc/latest/awc/struct.Client.html
[`awc::ClientResponse`]: https://docs.rs/awc/latest/awc/struct.ClientResponse.html
[`SendRequestError`]: https://docs.rs/awc/latest/awc/error/enum.SendRequestError.html
[`Responder`]: https://docs.rs/actix-web/latest/actix_web/trait.Responder.html
[duckduckgo]: https://duckduckgo.com/