1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Proxy module for handling LLM API requests
//!
//! This module implements a high-performance proxy service with dual-path architecture
//! for LLM API calls, designed to minimize latency while capturing comprehensive audit data.
//!
//! ## Architecture Overview
//!
//! The proxy uses a dual-path architecture to achieve both low latency and comprehensive auditing:
//!
//! ```text
//! ┌─────────────┐ ┌──────────────┐ ┌─────────────┐
//! │ Client │────▶│ Tower Stack │────▶│ Hot Path │
//! └─────────────┘ └──────────────┘ └──────┬──────┘
//! │ │
//! ▼ ▼
//! ┌──────────────┐ ┌──────────────┐
//! │ Middleware │ │ Ring Buffer │
//! └──────────────┘ └──────┬───────┘
//! │
//! ▼
//! ┌──────────────┐
//! │ Audit Path │
//! └──────────────┘
//! ```
//!
//! ### Hot Path (<5ms latency)
//! - Zero-copy streaming of requests/responses
//! - Minimal processing overhead
//! - Fire-and-forget writes to ring buffer
//!
//! ### Audit Path (async processing)
//! - Consumes events from ring buffer
//! - Persists to database
//! - Handles analytics and monitoring
//!
//! ## Module Organization
//!
//! - `types`: Core domain types with validation
//! - `http`: HTTP utilities and type-safe wrappers
//! - `middleware`: Tower middleware stack components
//! - `paths`: Hot path and audit path implementations
//! - `storage`: Ring buffer for event passing
//!
//! ## Example Usage
//!
//! ```rust,ignore
//! use union_square::proxy::{ProxyService, ProxyConfig, ProxyMiddlewareConfig};
//!
//! // Configure the proxy
//! let config = ProxyConfig::default();
//! let service = ProxyService::new(config);
//!
//! // Configure middleware
//! let middleware_config = ProxyMiddlewareConfig::default()
//! .disable_metrics() // Disable metrics endpoint
//! .enable_detailed_errors(); // Enable detailed errors for debugging
//!
//! // Create router with middleware
//! let auth_config = middleware_config.auth.clone();
//! let router = service.into_router(auth_config);
//!
//! // Run the server
//! let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
//! axum::serve(listener, router).await?;
//! ```
// Core types and configuration
// HTTP utilities and types
// Middleware components
pub use *;
pub use ;
// Path implementations
// Storage and persistence
// Internal modules (not part of public API)
// Test modules
// Main public exports
pub use ProxyService;
pub use ;