pub struct CreateProxyConfig {Show 14 fields
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,
pub cache_strategy: CacheStrategy,
pub compress_strategy: CompressStrategy,
pub cache_storage_mode: CacheStorageMode,
pub cache_directory: Option<PathBuf>,
pub proxy_mode: ProxyMode,
pub webhooks: Vec<WebhookConfig>,
}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.
cache_strategy: CacheStrategyControls which responses should be cached after the backend responds.
compress_strategy: CompressStrategyControls how cached bodies are stored in memory.
cache_storage_mode: CacheStorageModeControls where cached response bodies are stored.
cache_directory: Option<PathBuf>Optional override for filesystem-backed cache bodies.
proxy_mode: ProxyModeControls the operating mode of the proxy (Dynamic vs PreGenerate/SSG).
webhooks: Vec<WebhookConfig>Webhooks called for every request before cache reads. Blocking webhooks gate access; notify webhooks are fire-and-forget.
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?
8async fn main() {
9 // Initialize tracing (optional but recommended)
10 // tracing_subscriber::fmt::init();
11
12 // Create proxy configuration
13 // You can specify method prefixes to filter by HTTP method
14 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
15 .with_include_paths(vec![
16 "/api/*".to_string(),
17 "/public/*".to_string(),
18 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
19 ])
20 .with_exclude_paths(vec![
21 "/api/admin/*".to_string(),
22 "POST *".to_string(), // Don't cache any POST requests
23 "PUT *".to_string(), // Don't cache any PUT requests
24 "DELETE *".to_string(), // Don't cache any DELETE requests
25 ])
26 .caching_strategy(CacheStrategy::None)
27 .compression_strategy(CompressStrategy::Brotli)
28 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
29 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
30 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
31
32 // Create proxy - proxy_url is the backend server to proxy requests to
33 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
34
35 // You can clone and use the handle in your code
36 let handle_clone = handle.clone();
37
38 // Example: Trigger cache invalidation from another part of your application
39 tokio::spawn(async move {
40 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
41
42 // Invalidate all cache entries
43 handle_clone.invalidate_all();
44 println!("All cache invalidated!");
45
46 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
47
48 // Invalidate only cache entries matching a pattern (supports wildcards)
49 handle_clone.invalidate("GET:/api/*");
50 println!("Cache invalidated for GET:/api/* pattern!");
51 });
52
53 // Example: PreGenerate (SSG) mode with snapshot management
54 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
55 // .with_proxy_mode(ProxyMode::PreGenerate {
56 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
57 // fallthrough: false, // return 404 on cache miss (default)
58 // });
59 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
60 // // At runtime, manage snapshots:
61 // ssg_handle.add_snapshot("/book/2").await.unwrap();
62 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
63 // ssg_handle.remove_snapshot("/about").await.unwrap();
64 // ssg_handle.refresh_all_snapshots().await.unwrap();
65 let _ = ProxyMode::Dynamic; // suppress unused import warning
66
67 // Start the proxy server
68 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
69
70 println!("Proxy server listening on http://0.0.0.0:3000");
71 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
72 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
73 println!("Cache strategy: none (proxy-only mode)");
74 println!("Compression strategy: brotli (applies only to cached responses)");
75 println!("Cache storage mode: filesystem (custom cache directory)");
76 println!("Note: Cache reads and writes are disabled in this example");
77 println!("WebSocket support: enabled");
78
79 axum::serve(listener, proxy_app).await.unwrap();
80}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?
8async fn main() {
9 // Initialize tracing (optional but recommended)
10 // tracing_subscriber::fmt::init();
11
12 // Create proxy configuration
13 // You can specify method prefixes to filter by HTTP method
14 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
15 .with_include_paths(vec![
16 "/api/*".to_string(),
17 "/public/*".to_string(),
18 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
19 ])
20 .with_exclude_paths(vec![
21 "/api/admin/*".to_string(),
22 "POST *".to_string(), // Don't cache any POST requests
23 "PUT *".to_string(), // Don't cache any PUT requests
24 "DELETE *".to_string(), // Don't cache any DELETE requests
25 ])
26 .caching_strategy(CacheStrategy::None)
27 .compression_strategy(CompressStrategy::Brotli)
28 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
29 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
30 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
31
32 // Create proxy - proxy_url is the backend server to proxy requests to
33 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
34
35 // You can clone and use the handle in your code
36 let handle_clone = handle.clone();
37
38 // Example: Trigger cache invalidation from another part of your application
39 tokio::spawn(async move {
40 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
41
42 // Invalidate all cache entries
43 handle_clone.invalidate_all();
44 println!("All cache invalidated!");
45
46 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
47
48 // Invalidate only cache entries matching a pattern (supports wildcards)
49 handle_clone.invalidate("GET:/api/*");
50 println!("Cache invalidated for GET:/api/* pattern!");
51 });
52
53 // Example: PreGenerate (SSG) mode with snapshot management
54 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
55 // .with_proxy_mode(ProxyMode::PreGenerate {
56 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
57 // fallthrough: false, // return 404 on cache miss (default)
58 // });
59 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
60 // // At runtime, manage snapshots:
61 // ssg_handle.add_snapshot("/book/2").await.unwrap();
62 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
63 // ssg_handle.remove_snapshot("/about").await.unwrap();
64 // ssg_handle.refresh_all_snapshots().await.unwrap();
65 let _ = ProxyMode::Dynamic; // suppress unused import warning
66
67 // Start the proxy server
68 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
69
70 println!("Proxy server listening on http://0.0.0.0:3000");
71 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
72 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
73 println!("Cache strategy: none (proxy-only mode)");
74 println!("Compression strategy: brotli (applies only to cached responses)");
75 println!("Cache storage mode: filesystem (custom cache directory)");
76 println!("Note: Cache reads and writes are disabled in this example");
77 println!("WebSocket support: enabled");
78
79 axum::serve(listener, proxy_app).await.unwrap();
80}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?
8async fn main() {
9 // Initialize tracing (optional but recommended)
10 // tracing_subscriber::fmt::init();
11
12 // Create proxy configuration
13 // You can specify method prefixes to filter by HTTP method
14 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
15 .with_include_paths(vec![
16 "/api/*".to_string(),
17 "/public/*".to_string(),
18 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
19 ])
20 .with_exclude_paths(vec![
21 "/api/admin/*".to_string(),
22 "POST *".to_string(), // Don't cache any POST requests
23 "PUT *".to_string(), // Don't cache any PUT requests
24 "DELETE *".to_string(), // Don't cache any DELETE requests
25 ])
26 .caching_strategy(CacheStrategy::None)
27 .compression_strategy(CompressStrategy::Brotli)
28 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
29 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
30 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
31
32 // Create proxy - proxy_url is the backend server to proxy requests to
33 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
34
35 // You can clone and use the handle in your code
36 let handle_clone = handle.clone();
37
38 // Example: Trigger cache invalidation from another part of your application
39 tokio::spawn(async move {
40 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
41
42 // Invalidate all cache entries
43 handle_clone.invalidate_all();
44 println!("All cache invalidated!");
45
46 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
47
48 // Invalidate only cache entries matching a pattern (supports wildcards)
49 handle_clone.invalidate("GET:/api/*");
50 println!("Cache invalidated for GET:/api/* pattern!");
51 });
52
53 // Example: PreGenerate (SSG) mode with snapshot management
54 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
55 // .with_proxy_mode(ProxyMode::PreGenerate {
56 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
57 // fallthrough: false, // return 404 on cache miss (default)
58 // });
59 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
60 // // At runtime, manage snapshots:
61 // ssg_handle.add_snapshot("/book/2").await.unwrap();
62 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
63 // ssg_handle.remove_snapshot("/about").await.unwrap();
64 // ssg_handle.refresh_all_snapshots().await.unwrap();
65 let _ = ProxyMode::Dynamic; // suppress unused import warning
66
67 // Start the proxy server
68 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
69
70 println!("Proxy server listening on http://0.0.0.0:3000");
71 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
72 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
73 println!("Cache strategy: none (proxy-only mode)");
74 println!("Compression strategy: brotli (applies only to cached responses)");
75 println!("Cache storage mode: filesystem (custom cache directory)");
76 println!("Note: Cache reads and writes are disabled in this example");
77 println!("WebSocket support: enabled");
78
79 axum::serve(listener, proxy_app).await.unwrap();
80}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?
8async fn main() {
9 // Initialize tracing (optional but recommended)
10 // tracing_subscriber::fmt::init();
11
12 // Create proxy configuration
13 // You can specify method prefixes to filter by HTTP method
14 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
15 .with_include_paths(vec![
16 "/api/*".to_string(),
17 "/public/*".to_string(),
18 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
19 ])
20 .with_exclude_paths(vec![
21 "/api/admin/*".to_string(),
22 "POST *".to_string(), // Don't cache any POST requests
23 "PUT *".to_string(), // Don't cache any PUT requests
24 "DELETE *".to_string(), // Don't cache any DELETE requests
25 ])
26 .caching_strategy(CacheStrategy::None)
27 .compression_strategy(CompressStrategy::Brotli)
28 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
29 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
30 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
31
32 // Create proxy - proxy_url is the backend server to proxy requests to
33 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
34
35 // You can clone and use the handle in your code
36 let handle_clone = handle.clone();
37
38 // Example: Trigger cache invalidation from another part of your application
39 tokio::spawn(async move {
40 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
41
42 // Invalidate all cache entries
43 handle_clone.invalidate_all();
44 println!("All cache invalidated!");
45
46 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
47
48 // Invalidate only cache entries matching a pattern (supports wildcards)
49 handle_clone.invalidate("GET:/api/*");
50 println!("Cache invalidated for GET:/api/* pattern!");
51 });
52
53 // Example: PreGenerate (SSG) mode with snapshot management
54 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
55 // .with_proxy_mode(ProxyMode::PreGenerate {
56 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
57 // fallthrough: false, // return 404 on cache miss (default)
58 // });
59 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
60 // // At runtime, manage snapshots:
61 // ssg_handle.add_snapshot("/book/2").await.unwrap();
62 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
63 // ssg_handle.remove_snapshot("/about").await.unwrap();
64 // ssg_handle.refresh_all_snapshots().await.unwrap();
65 let _ = ProxyMode::Dynamic; // suppress unused import warning
66
67 // Start the proxy server
68 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
69
70 println!("Proxy server listening on http://0.0.0.0:3000");
71 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
72 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
73 println!("Cache strategy: none (proxy-only mode)");
74 println!("Compression strategy: brotli (applies only to cached responses)");
75 println!("Cache storage mode: filesystem (custom cache directory)");
76 println!("Note: Cache reads and writes are disabled in this example");
77 println!("WebSocket support: enabled");
78
79 axum::serve(listener, proxy_app).await.unwrap();
80}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
Sourcepub fn with_cache_strategy(self, strategy: CacheStrategy) -> Self
pub fn with_cache_strategy(self, strategy: CacheStrategy) -> Self
Set the cache strategy used to decide which response types are stored.
Sourcepub fn caching_strategy(self, strategy: CacheStrategy) -> Self
pub fn caching_strategy(self, strategy: CacheStrategy) -> Self
Alias for callers that prefer a more fluent builder name.
Examples found in repository?
8async fn main() {
9 // Initialize tracing (optional but recommended)
10 // tracing_subscriber::fmt::init();
11
12 // Create proxy configuration
13 // You can specify method prefixes to filter by HTTP method
14 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
15 .with_include_paths(vec![
16 "/api/*".to_string(),
17 "/public/*".to_string(),
18 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
19 ])
20 .with_exclude_paths(vec![
21 "/api/admin/*".to_string(),
22 "POST *".to_string(), // Don't cache any POST requests
23 "PUT *".to_string(), // Don't cache any PUT requests
24 "DELETE *".to_string(), // Don't cache any DELETE requests
25 ])
26 .caching_strategy(CacheStrategy::None)
27 .compression_strategy(CompressStrategy::Brotli)
28 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
29 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
30 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
31
32 // Create proxy - proxy_url is the backend server to proxy requests to
33 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
34
35 // You can clone and use the handle in your code
36 let handle_clone = handle.clone();
37
38 // Example: Trigger cache invalidation from another part of your application
39 tokio::spawn(async move {
40 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
41
42 // Invalidate all cache entries
43 handle_clone.invalidate_all();
44 println!("All cache invalidated!");
45
46 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
47
48 // Invalidate only cache entries matching a pattern (supports wildcards)
49 handle_clone.invalidate("GET:/api/*");
50 println!("Cache invalidated for GET:/api/* pattern!");
51 });
52
53 // Example: PreGenerate (SSG) mode with snapshot management
54 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
55 // .with_proxy_mode(ProxyMode::PreGenerate {
56 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
57 // fallthrough: false, // return 404 on cache miss (default)
58 // });
59 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
60 // // At runtime, manage snapshots:
61 // ssg_handle.add_snapshot("/book/2").await.unwrap();
62 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
63 // ssg_handle.remove_snapshot("/about").await.unwrap();
64 // ssg_handle.refresh_all_snapshots().await.unwrap();
65 let _ = ProxyMode::Dynamic; // suppress unused import warning
66
67 // Start the proxy server
68 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
69
70 println!("Proxy server listening on http://0.0.0.0:3000");
71 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
72 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
73 println!("Cache strategy: none (proxy-only mode)");
74 println!("Compression strategy: brotli (applies only to cached responses)");
75 println!("Cache storage mode: filesystem (custom cache directory)");
76 println!("Note: Cache reads and writes are disabled in this example");
77 println!("WebSocket support: enabled");
78
79 axum::serve(listener, proxy_app).await.unwrap();
80}Sourcepub fn with_compress_strategy(self, strategy: CompressStrategy) -> Self
pub fn with_compress_strategy(self, strategy: CompressStrategy) -> Self
Set the compression strategy used for stored cache entries.
Sourcepub fn compression_strategy(self, strategy: CompressStrategy) -> Self
pub fn compression_strategy(self, strategy: CompressStrategy) -> Self
Alias for callers that prefer a more fluent builder name.
Examples found in repository?
8async fn main() {
9 // Initialize tracing (optional but recommended)
10 // tracing_subscriber::fmt::init();
11
12 // Create proxy configuration
13 // You can specify method prefixes to filter by HTTP method
14 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
15 .with_include_paths(vec![
16 "/api/*".to_string(),
17 "/public/*".to_string(),
18 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
19 ])
20 .with_exclude_paths(vec![
21 "/api/admin/*".to_string(),
22 "POST *".to_string(), // Don't cache any POST requests
23 "PUT *".to_string(), // Don't cache any PUT requests
24 "DELETE *".to_string(), // Don't cache any DELETE requests
25 ])
26 .caching_strategy(CacheStrategy::None)
27 .compression_strategy(CompressStrategy::Brotli)
28 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
29 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
30 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
31
32 // Create proxy - proxy_url is the backend server to proxy requests to
33 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
34
35 // You can clone and use the handle in your code
36 let handle_clone = handle.clone();
37
38 // Example: Trigger cache invalidation from another part of your application
39 tokio::spawn(async move {
40 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
41
42 // Invalidate all cache entries
43 handle_clone.invalidate_all();
44 println!("All cache invalidated!");
45
46 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
47
48 // Invalidate only cache entries matching a pattern (supports wildcards)
49 handle_clone.invalidate("GET:/api/*");
50 println!("Cache invalidated for GET:/api/* pattern!");
51 });
52
53 // Example: PreGenerate (SSG) mode with snapshot management
54 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
55 // .with_proxy_mode(ProxyMode::PreGenerate {
56 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
57 // fallthrough: false, // return 404 on cache miss (default)
58 // });
59 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
60 // // At runtime, manage snapshots:
61 // ssg_handle.add_snapshot("/book/2").await.unwrap();
62 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
63 // ssg_handle.remove_snapshot("/about").await.unwrap();
64 // ssg_handle.refresh_all_snapshots().await.unwrap();
65 let _ = ProxyMode::Dynamic; // suppress unused import warning
66
67 // Start the proxy server
68 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
69
70 println!("Proxy server listening on http://0.0.0.0:3000");
71 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
72 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
73 println!("Cache strategy: none (proxy-only mode)");
74 println!("Compression strategy: brotli (applies only to cached responses)");
75 println!("Cache storage mode: filesystem (custom cache directory)");
76 println!("Note: Cache reads and writes are disabled in this example");
77 println!("WebSocket support: enabled");
78
79 axum::serve(listener, proxy_app).await.unwrap();
80}Sourcepub fn with_cache_storage_mode(self, mode: CacheStorageMode) -> Self
pub fn with_cache_storage_mode(self, mode: CacheStorageMode) -> Self
Set the backing store for cached response bodies.
Examples found in repository?
8async fn main() {
9 // Initialize tracing (optional but recommended)
10 // tracing_subscriber::fmt::init();
11
12 // Create proxy configuration
13 // You can specify method prefixes to filter by HTTP method
14 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
15 .with_include_paths(vec![
16 "/api/*".to_string(),
17 "/public/*".to_string(),
18 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
19 ])
20 .with_exclude_paths(vec![
21 "/api/admin/*".to_string(),
22 "POST *".to_string(), // Don't cache any POST requests
23 "PUT *".to_string(), // Don't cache any PUT requests
24 "DELETE *".to_string(), // Don't cache any DELETE requests
25 ])
26 .caching_strategy(CacheStrategy::None)
27 .compression_strategy(CompressStrategy::Brotli)
28 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
29 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
30 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
31
32 // Create proxy - proxy_url is the backend server to proxy requests to
33 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
34
35 // You can clone and use the handle in your code
36 let handle_clone = handle.clone();
37
38 // Example: Trigger cache invalidation from another part of your application
39 tokio::spawn(async move {
40 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
41
42 // Invalidate all cache entries
43 handle_clone.invalidate_all();
44 println!("All cache invalidated!");
45
46 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
47
48 // Invalidate only cache entries matching a pattern (supports wildcards)
49 handle_clone.invalidate("GET:/api/*");
50 println!("Cache invalidated for GET:/api/* pattern!");
51 });
52
53 // Example: PreGenerate (SSG) mode with snapshot management
54 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
55 // .with_proxy_mode(ProxyMode::PreGenerate {
56 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
57 // fallthrough: false, // return 404 on cache miss (default)
58 // });
59 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
60 // // At runtime, manage snapshots:
61 // ssg_handle.add_snapshot("/book/2").await.unwrap();
62 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
63 // ssg_handle.remove_snapshot("/about").await.unwrap();
64 // ssg_handle.refresh_all_snapshots().await.unwrap();
65 let _ = ProxyMode::Dynamic; // suppress unused import warning
66
67 // Start the proxy server
68 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
69
70 println!("Proxy server listening on http://0.0.0.0:3000");
71 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
72 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
73 println!("Cache strategy: none (proxy-only mode)");
74 println!("Compression strategy: brotli (applies only to cached responses)");
75 println!("Cache storage mode: filesystem (custom cache directory)");
76 println!("Note: Cache reads and writes are disabled in this example");
77 println!("WebSocket support: enabled");
78
79 axum::serve(listener, proxy_app).await.unwrap();
80}Sourcepub fn with_cache_directory(self, directory: impl Into<PathBuf>) -> Self
pub fn with_cache_directory(self, directory: impl Into<PathBuf>) -> Self
Set the filesystem directory used for disk-backed cache bodies.
Examples found in repository?
8async fn main() {
9 // Initialize tracing (optional but recommended)
10 // tracing_subscriber::fmt::init();
11
12 // Create proxy configuration
13 // You can specify method prefixes to filter by HTTP method
14 let proxy_config = CreateProxyConfig::new("http://localhost:8080".to_string())
15 .with_include_paths(vec![
16 "/api/*".to_string(),
17 "/public/*".to_string(),
18 "GET /admin/stats".to_string(), // Only cache GET requests to this endpoint
19 ])
20 .with_exclude_paths(vec![
21 "/api/admin/*".to_string(),
22 "POST *".to_string(), // Don't cache any POST requests
23 "PUT *".to_string(), // Don't cache any PUT requests
24 "DELETE *".to_string(), // Don't cache any DELETE requests
25 ])
26 .caching_strategy(CacheStrategy::None)
27 .compression_strategy(CompressStrategy::Brotli)
28 .with_cache_storage_mode(phantom_frame::CacheStorageMode::Filesystem)
29 .with_cache_directory(PathBuf::from("./.phantom-frame-cache"))
30 .with_websocket_enabled(true); // Enable WebSocket support (default: true)
31
32 // Create proxy - proxy_url is the backend server to proxy requests to
33 let (proxy_app, handle): (Router, CacheHandle) = create_proxy(proxy_config);
34
35 // You can clone and use the handle in your code
36 let handle_clone = handle.clone();
37
38 // Example: Trigger cache invalidation from another part of your application
39 tokio::spawn(async move {
40 tokio::time::sleep(tokio::time::Duration::from_secs(60)).await;
41
42 // Invalidate all cache entries
43 handle_clone.invalidate_all();
44 println!("All cache invalidated!");
45
46 tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
47
48 // Invalidate only cache entries matching a pattern (supports wildcards)
49 handle_clone.invalidate("GET:/api/*");
50 println!("Cache invalidated for GET:/api/* pattern!");
51 });
52
53 // Example: PreGenerate (SSG) mode with snapshot management
54 // let ssg_config = CreateProxyConfig::new("http://localhost:8080".to_string())
55 // .with_proxy_mode(ProxyMode::PreGenerate {
56 // paths: vec!["/".to_string(), "/about".to_string(), "/book/1".to_string()],
57 // fallthrough: false, // return 404 on cache miss (default)
58 // });
59 // let (ssg_app, ssg_handle) = create_proxy(ssg_config);
60 // // At runtime, manage snapshots:
61 // ssg_handle.add_snapshot("/book/2").await.unwrap();
62 // ssg_handle.refresh_snapshot("/book/1").await.unwrap();
63 // ssg_handle.remove_snapshot("/about").await.unwrap();
64 // ssg_handle.refresh_all_snapshots().await.unwrap();
65 let _ = ProxyMode::Dynamic; // suppress unused import warning
66
67 // Start the proxy server
68 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
69
70 println!("Proxy server listening on http://0.0.0.0:3000");
71 println!("Caching paths: /api/*, /public/*, GET /admin/stats");
72 println!("Excluding: /api/admin/*, POST *, PUT *, DELETE *");
73 println!("Cache strategy: none (proxy-only mode)");
74 println!("Compression strategy: brotli (applies only to cached responses)");
75 println!("Cache storage mode: filesystem (custom cache directory)");
76 println!("Note: Cache reads and writes are disabled in this example");
77 println!("WebSocket support: enabled");
78
79 axum::serve(listener, proxy_app).await.unwrap();
80}Sourcepub fn with_proxy_mode(self, mode: ProxyMode) -> Self
pub fn with_proxy_mode(self, mode: ProxyMode) -> Self
Set the proxy operating mode.
Use ProxyMode::PreGenerate { paths, fallthrough } to enable SSG mode.
Sourcepub fn with_webhooks(self, webhooks: Vec<WebhookConfig>) -> Self
pub fn with_webhooks(self, webhooks: Vec<WebhookConfig>) -> Self
Set the webhooks for this server. Blocking webhooks gate access; notify webhooks are fire-and-forget.
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