phantom-frame
A high-performance prerendering proxy engine written in Rust. Cache and serve prerendered content with ease.
Features
- 🚀 Fast caching proxy - Cache prerendered content and serve it instantly
- 🔧 Dual mode operation - Run as standalone HTTP server or integrate as a library
- 🔄 Dynamic cache refresh - Trigger cache invalidation via control endpoint or programmatically
- 🔐 Optional authentication - Secure control endpoints with bearer token auth
- ⚡ Async/await - Built on Tokio and Axum for high performance
- 📦 Easy integration - Simple API for library usage
Usage
Mode 1: Standalone HTTP Server
Run as a standalone server with a TOML configuration file:
Configuration File (config.toml)
[]
# Control port for cache management endpoints (default: 17809)
= 17809
# Proxy port for serving prerendered content (default: 3000)
= 3000
# The backend URL to proxy requests to (default: http://localhost:8080)
= "http://localhost:8080"
# Optional: Bearer token for control endpoint authentication
# If set, requests to /refresh-cache must include: Authorization: Bearer <token>
= "your-secret-token-here"
Control Endpoints
POST /refresh-cache - Trigger cache invalidation
# Without authentication
# With authentication (if control_auth is set)
Mode 2: Library Integration
Add to your Cargo.toml:
[]
= { = "../phantom-frame" }
= { = "1.40", = ["full"] }
= "0.7"
Use in your code:
use ;
use Router;
async
Building
# Build the project
# Run in development
# Run the library example
How It Works
- Request Flow: When a request comes in, phantom-frame first checks if the content is cached
- Cache Miss: If not cached, it fetches from the backend, caches the response, and returns it
- Cache Hit: If cached, it serves the cached content immediately
- Cache Refresh: The cache can be invalidated via the control endpoint or programmatically
API Reference
Library API
create_proxy(proxy_url: String) -> (Router, RefreshTrigger)
Creates a proxy router and refresh trigger.
- Parameters:
proxy_url- The backend server URL to proxy requests to - Returns: Tuple of
(Router, RefreshTrigger)
RefreshTrigger
A clonable trigger for cache invalidation.
trigger()- Trigger a cache refreshsubscribe()- Subscribe to refresh events (returns a broadcast receiver)
Control Endpoints
POST /refresh-cache
Triggers cache invalidation. Requires Authorization: Bearer <token> header if control_auth is configured.
License
See LICENSE file for details