pub struct CreateProxyConfig {
pub proxy_url: String,
pub include_paths: Vec<String>,
pub exclude_paths: Vec<String>,
pub enable_websocket: bool,
pub forward_get_only: bool,
pub cache_key_fn: Arc<dyn Fn(&RequestInfo<'_>) -> String + Send + Sync>,
pub cache_404_capacity: usize,
pub use_404_meta: bool,
}Expand description
Configuration for creating a proxy
Fields§
§proxy_url: StringThe backend URL to proxy requests to
include_paths: Vec<String>Paths to include in caching (empty means include all) Supports wildcards and method prefixes: “/api/”, “POST /api/”, “GET /*/users”, etc.
exclude_paths: Vec<String>Paths to exclude from caching (empty means exclude none) Supports wildcards and method prefixes: “/admin/*”, “POST ”, “PUT /api/”, etc. Exclude overrides include
enable_websocket: boolEnable WebSocket and protocol upgrade support (default: true) When enabled, requests with Connection: Upgrade headers will bypass the cache and establish a direct bidirectional TCP tunnel
forward_get_only: boolOnly allow GET requests, reject all others (default: false) When true, only GET requests are processed; POST, PUT, DELETE, etc. return 405 Method Not Allowed Useful for static site prerendering where mutations shouldn’t be allowed
cache_key_fn: Arc<dyn Fn(&RequestInfo<'_>) -> String + Send + Sync>Custom cache key generator Takes request info and returns a cache key Default: method + path + query string
cache_404_capacity: usizeCapacity for special 404 cache. When 0, 404 caching is disabled.
use_404_meta: boolWhen true, treat a response containing the meta tag <meta name="phantom-404" content="true"> as a 404
This is an optional performance-affecting fallback to detect framework-generated 404 pages.
Implementations§
Source§impl CreateProxyConfig
impl CreateProxyConfig
Sourcepub fn new(proxy_url: String) -> Self
pub fn new(proxy_url: String) -> Self
Create a new config with default settings
Examples found in repository?
5async fn main() {
6 // Initialize tracing (optional but recommended)
7 // tracing_subscriber::fmt::init();
8
9 // Create proxy configuration
10 // You can specify method prefixes to filter by HTTP method
11 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
12 .with_include_paths(vec![
13 "/api/*".to_string(),
14 "/public/*".to_string(),
15 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
16 ])
17 .with_exclude_paths(vec![
18 "/api/admin/*".to_string(),
19 "POST *".to_string(), // Don't cache any POST requests
20 "PUT *".to_string(), // Don't cache any PUT requests
21 "DELETE *".to_string(), // Don't cache any DELETE requests
22 ])
23 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
24
25 // Create proxy - proxy_url is the backend server to proxy requests to
26 let (proxy_app, refresh_trigger): (Router, RefreshTrigger) = create_proxy(proxy_config);
27
28 // You can clone and use the refresh_trigger in your code
29 let trigger_clone = refresh_trigger.clone();
30
31 // Example: Trigger cache refresh from another part of your application
32 tokio::spawn(async move {
33 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
34
35 // Clear all cache entries
36 trigger_clone.trigger();
37 println!("All cache cleared!");
38
39 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
40
41 // Clear only cache entries matching a pattern (supports wildcards)
42 trigger_clone.trigger_by_key_match("GET:/api/*");
43 println!("Cache cleared for GET:/api/* pattern!");
44 });
45
46 // Start the proxy server
47 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
48
49 println!("Proxy server listening on http://0.0.0.0:3000");
50 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
51 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
52 println!("Note: Only GET requests will be cached (POST/PUT/DELETE are excluded)");
53 println!("WebSocket support: enabled");
54
55 axum::serve(listener, proxy_app).await.unwrap();
56}Sourcepub fn with_include_paths(self, paths: Vec<String>) -> Self
pub fn with_include_paths(self, paths: Vec<String>) -> Self
Set include paths
Examples found in repository?
5async fn main() {
6 // Initialize tracing (optional but recommended)
7 // tracing_subscriber::fmt::init();
8
9 // Create proxy configuration
10 // You can specify method prefixes to filter by HTTP method
11 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
12 .with_include_paths(vec![
13 "/api/*".to_string(),
14 "/public/*".to_string(),
15 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
16 ])
17 .with_exclude_paths(vec![
18 "/api/admin/*".to_string(),
19 "POST *".to_string(), // Don't cache any POST requests
20 "PUT *".to_string(), // Don't cache any PUT requests
21 "DELETE *".to_string(), // Don't cache any DELETE requests
22 ])
23 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
24
25 // Create proxy - proxy_url is the backend server to proxy requests to
26 let (proxy_app, refresh_trigger): (Router, RefreshTrigger) = create_proxy(proxy_config);
27
28 // You can clone and use the refresh_trigger in your code
29 let trigger_clone = refresh_trigger.clone();
30
31 // Example: Trigger cache refresh from another part of your application
32 tokio::spawn(async move {
33 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
34
35 // Clear all cache entries
36 trigger_clone.trigger();
37 println!("All cache cleared!");
38
39 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
40
41 // Clear only cache entries matching a pattern (supports wildcards)
42 trigger_clone.trigger_by_key_match("GET:/api/*");
43 println!("Cache cleared for GET:/api/* pattern!");
44 });
45
46 // Start the proxy server
47 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
48
49 println!("Proxy server listening on http://0.0.0.0:3000");
50 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
51 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
52 println!("Note: Only GET requests will be cached (POST/PUT/DELETE are excluded)");
53 println!("WebSocket support: enabled");
54
55 axum::serve(listener, proxy_app).await.unwrap();
56}Sourcepub fn with_exclude_paths(self, paths: Vec<String>) -> Self
pub fn with_exclude_paths(self, paths: Vec<String>) -> Self
Set exclude paths
Examples found in repository?
5async fn main() {
6 // Initialize tracing (optional but recommended)
7 // tracing_subscriber::fmt::init();
8
9 // Create proxy configuration
10 // You can specify method prefixes to filter by HTTP method
11 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
12 .with_include_paths(vec![
13 "/api/*".to_string(),
14 "/public/*".to_string(),
15 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
16 ])
17 .with_exclude_paths(vec![
18 "/api/admin/*".to_string(),
19 "POST *".to_string(), // Don't cache any POST requests
20 "PUT *".to_string(), // Don't cache any PUT requests
21 "DELETE *".to_string(), // Don't cache any DELETE requests
22 ])
23 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
24
25 // Create proxy - proxy_url is the backend server to proxy requests to
26 let (proxy_app, refresh_trigger): (Router, RefreshTrigger) = create_proxy(proxy_config);
27
28 // You can clone and use the refresh_trigger in your code
29 let trigger_clone = refresh_trigger.clone();
30
31 // Example: Trigger cache refresh from another part of your application
32 tokio::spawn(async move {
33 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
34
35 // Clear all cache entries
36 trigger_clone.trigger();
37 println!("All cache cleared!");
38
39 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
40
41 // Clear only cache entries matching a pattern (supports wildcards)
42 trigger_clone.trigger_by_key_match("GET:/api/*");
43 println!("Cache cleared for GET:/api/* pattern!");
44 });
45
46 // Start the proxy server
47 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
48
49 println!("Proxy server listening on http://0.0.0.0:3000");
50 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
51 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
52 println!("Note: Only GET requests will be cached (POST/PUT/DELETE are excluded)");
53 println!("WebSocket support: enabled");
54
55 axum::serve(listener, proxy_app).await.unwrap();
56}Sourcepub fn with_websocket_enabled(self, enabled: bool) -> Self
pub fn with_websocket_enabled(self, enabled: bool) -> Self
Enable or disable WebSocket and protocol upgrade support
Examples found in repository?
5async fn main() {
6 // Initialize tracing (optional but recommended)
7 // tracing_subscriber::fmt::init();
8
9 // Create proxy configuration
10 // You can specify method prefixes to filter by HTTP method
11 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
12 .with_include_paths(vec![
13 "/api/*".to_string(),
14 "/public/*".to_string(),
15 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
16 ])
17 .with_exclude_paths(vec![
18 "/api/admin/*".to_string(),
19 "POST *".to_string(), // Don't cache any POST requests
20 "PUT *".to_string(), // Don't cache any PUT requests
21 "DELETE *".to_string(), // Don't cache any DELETE requests
22 ])
23 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
24
25 // Create proxy - proxy_url is the backend server to proxy requests to
26 let (proxy_app, refresh_trigger): (Router, RefreshTrigger) = create_proxy(proxy_config);
27
28 // You can clone and use the refresh_trigger in your code
29 let trigger_clone = refresh_trigger.clone();
30
31 // Example: Trigger cache refresh from another part of your application
32 tokio::spawn(async move {
33 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
34
35 // Clear all cache entries
36 trigger_clone.trigger();
37 println!("All cache cleared!");
38
39 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
40
41 // Clear only cache entries matching a pattern (supports wildcards)
42 trigger_clone.trigger_by_key_match("GET:/api/*");
43 println!("Cache cleared for GET:/api/* pattern!");
44 });
45
46 // Start the proxy server
47 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
48
49 println!("Proxy server listening on http://0.0.0.0:3000");
50 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
51 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
52 println!("Note: Only GET requests will be cached (POST/PUT/DELETE are excluded)");
53 println!("WebSocket support: enabled");
54
55 axum::serve(listener, proxy_app).await.unwrap();
56}Sourcepub fn with_forward_get_only(self, enabled: bool) -> Self
pub fn with_forward_get_only(self, enabled: bool) -> Self
Only allow GET requests, reject all others
Sourcepub fn with_cache_key_fn<F>(self, f: F) -> Self
pub fn with_cache_key_fn<F>(self, f: F) -> Self
Set custom cache key function
Sourcepub fn with_cache_404_capacity(self, capacity: usize) -> Self
pub fn with_cache_404_capacity(self, capacity: usize) -> Self
Set 404 cache capacity. When 0, 404 caching is disabled.
Sourcepub fn with_use_404_meta(self, enabled: bool) -> Self
pub fn with_use_404_meta(self, enabled: bool) -> Self
Treat pages that include the special meta tag as 404 pages
Trait Implementations§
Source§impl Clone for CreateProxyConfig
impl Clone for CreateProxyConfig
Source§fn clone(&self) -> CreateProxyConfig
fn clone(&self) -> CreateProxyConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more