Expand description
reverse-proxy-service
is tower Service
s that performs “reverse
proxy” with various rewriting rules.
Internally these services use hyper::Client
to send an incoming request to the another
server. The connector
for a client can be
HttpConnector
, HttpsConnector
,
or any ones whichever you want.
§Examples
There are two types of services, OneshotService
and ReusedService
. The
OneshotService
owns the Client
, while the ReusedService
shares the Client
via Arc
.
§General usage
use reverse_proxy_service::ReusedServiceBuilder;
use reverse_proxy_service::{ReplaceAll, ReplaceN};
use hyper::body::Body;
use http::Request;
use tower_service::Service as _;
let svc_builder = reverse_proxy_service::builder_http("example.com:1234").unwrap();
let req1 = Request::builder()
.method("GET")
.uri("https://myserver.com/foo/bar/foo")
.body(Body::empty())
.unwrap();
// Clones Arc<Client>
let mut svc1 = svc_builder.build(ReplaceAll("foo", "baz"));
// http://example.com:1234/baz/bar/baz
let _res = svc1.call(req1).await.unwrap();
let req2 = Request::builder()
.method("POST")
.uri("https://myserver.com/foo/bar/foo")
.header("Content-Type", "application/x-www-form-urlencoded")
.body(Body::from("key=value"))
.unwrap();
let mut svc2 = svc_builder.build(ReplaceN("foo", "baz", 1));
// http://example.com:1234/baz/bar/foo
let _res = svc2.call(req2).await.unwrap();
In this example, the svc1
and svc2
shares the same Client
, holding the Arc<Client>
s
inside them.
For more information of rewriting rules (ReplaceAll
, ReplaceN
etc.), see the
documentations of rewrite
.
§With axum
use reverse_proxy_service::ReusedServiceBuilder;
use reverse_proxy_service::{TrimPrefix, AppendSuffix, Static};
use axum::Router;
#[tokio::main]
async fn main() {
let host1 = reverse_proxy_service::builder_http("example.com").unwrap();
let host2 = reverse_proxy_service::builder_http("example.net:1234").unwrap();
let app = Router::new()
.route_service("/healthcheck", host1.build(Static("/")))
.route_service("/users/*path", host1.build(TrimPrefix("/users")))
.route_service("/posts", host2.build(AppendSuffix("/")));
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
§Return Types
The return type (Future::Output
) of ReusedService
and
OneshotService
is Result<Result<Response, Error>, Infallible>
. This is because axum’s
Router
accepts only such Service
s.
The Error
type implements IntoResponse
if you enable the
axum
feature.
It returns an empty body, with the status code INTERNAL_SERVER_ERROR
. The description of this
error will be logged out at error level in the
into_response()
method.
§Features
By default only http1
is enabled.
http1
: useshyper/http1
http2
: useshyper/http2
https
: alias tonativetls
nativetls
: uses thehyper-tls
craterustls
: alias torustls-webpki-roots
rustls-webpki-roots
: uses thehyper-rustls
crate, with the featurewebpki-roots
rustls-native-roots
: uses thehyper-rustls
crate, with the featurerustls-native-certs
rustls-http2
:http2
plusrustls
, andrustls/http2
is enabledaxum
: implementsIntoResponse
forError
You must turn on either http1
or http2
. You cannot use the services if, for example, only
the https
feature is on.
Through this document, we use rustls
to mean any of rustls*
features unless otherwise
specified.
Re-exports§
pub use rewrite::*;
Modules§
- client
http1
orhttp2
- Includes helper functions to build
Client
s, and some re-exports fromhyper::client
orhyper_tls
. - rewrite
- A
PathRewriter
instance defines a rule to rewrite the request path.
Structs§
- Oneshot
Service http1
orhttp2
- A
Service<Request<B>>
that sends a request and returns the response, owning aClient
. - Reused
Service http1
orhttp2
- A
Service<Request<B>>
that sends a request and returns the response, sharing aClient
. - Reused
Service Builder http1
orhttp2
- The return type of
builder()
,builder_http()
andbuilder_https()
. - RevProxy
Future
Enums§
Functions§
- builder
http1
orhttp2
- Builder of
ReusedService
. - builder_
http http1
orhttp2
- Builder of
ReusedService
, withclient::http_default()
. - builder_
https ( http1
orhttp2
) and (https
ornativetls
) - Builder of
ReusedService
, withclient::https_default()
. - builder_
nativetls ( http1
orhttp2
) andnativetls
- Builder of
ReusedService
, withclient::nativetls_default()
. - builder_
rustls ( http1
orhttp2
) andrustls
- Builder of
ReusedService
, withclient::rustls_default()
.