TargetResolver

Trait TargetResolver 

Source
pub trait TargetResolver:
    Clone
    + Send
    + Sync
    + 'static {
    // Required method
    fn resolve(
        &self,
        req: &Request<Body>,
        params: &[(String, String)],
    ) -> String;
}
Expand description

A trait for resolving the target URL for a proxy request.

Implement this trait to provide custom target URL resolution logic. The resolver receives the full request and path parameters, allowing routing decisions based on headers, method, query parameters, etc.

§Built-in Implementations

  • String and &'static str: Static target URLs (request/parameters are ignored)
  • TemplateTarget: Template-based URL with {param} substitution

§Example

use axum::body::Body;
use axum::http::Request;
use axum_reverse_proxy::TargetResolver;

#[derive(Clone)]
struct HeaderBasedResolver {
    default_url: String,
    premium_url: String,
}

impl TargetResolver for HeaderBasedResolver {
    fn resolve(&self, req: &Request<Body>, _params: &[(String, String)]) -> String {
        // Route premium users to a different backend
        if req.headers().get("x-premium-user").is_some() {
            self.premium_url.clone()
        } else {
            self.default_url.clone()
        }
    }
}

Required Methods§

Source

fn resolve(&self, req: &Request<Body>, params: &[(String, String)]) -> String

Resolve the target URL based on the request and path parameters.

§Arguments
  • req - The incoming HTTP request (headers, method, URI, etc.)
  • params - Path parameters extracted from the request URL as key-value pairs
§Returns

The target URL as a string. This should be a valid URL including scheme and host.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl TargetResolver for &'static str

Source§

fn resolve(&self, _req: &Request<Body>, _params: &[(String, String)]) -> String

Source§

impl TargetResolver for String

Source§

fn resolve(&self, _req: &Request<Body>, _params: &[(String, String)]) -> String

Implementors§