Documentation
¶
Overview ¶
Package middleware provides HTTP middleware for the CLI Proxy API server.
Package middleware provides HTTP middleware components for the CLI Proxy API server. This file contains the request logging middleware that captures comprehensive request and response data when enabled through configuration.
Package middleware provides Gin HTTP middleware for the CLI Proxy API server. It includes a sophisticated response writer wrapper designed to capture and log request and response data, including support for streaming responses, without impacting latency.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractModelMiddleware ¶
func ExtractModelMiddleware() gin.HandlerFunc
ExtractModelMiddleware extracts the model name from the request and sets it in the context. This is useful for handlers that need to access the model name without parsing the body again.
func LimitsMiddleware ¶
func LimitsMiddleware(enforcer *limits.Enforcer) gin.HandlerFunc
LimitsMiddleware returns a Gin middleware that enforces API key model restrictions and monthly quotas. It reads the model name from the request body and validates access before allowing the request to proceed.
func RequestLoggingMiddleware ¶
func RequestLoggingMiddleware(logger logging.RequestLogger) gin.HandlerFunc
RequestLoggingMiddleware creates a Gin middleware that logs HTTP requests and responses. It captures detailed information about the request and response, including headers and body, and uses the provided RequestLogger to record this data. When logging is disabled in the logger, it still captures data so that upstream errors can be persisted.
func StripModelPrefix ¶
func StripModelPrefix() gin.HandlerFunc
StripModelPrefix is a middleware that removes "{owner}/" prefix from model names in request payloads. This allows clients to send model names like "anthropic/claude-3-5-haiku" which will be automatically stripped to "claude-3-5-haiku" before routing.
Types ¶
type RequestInfo ¶
type RequestInfo struct {
URL string // URL is the request URL.
Method string // Method is the HTTP method (e.g., GET, POST).
Headers map[string][]string // Headers contains the request headers.
Body []byte // Body is the raw request body.
RequestID string // RequestID is the unique identifier for the request.
}
RequestInfo holds essential details of an incoming HTTP request for logging purposes.
type ResponseWriterWrapper ¶
type ResponseWriterWrapper struct {
gin.ResponseWriter
// contains filtered or unexported fields
}
ResponseWriterWrapper wraps the standard gin.ResponseWriter to intercept and log response data. It is designed to handle both standard and streaming responses, ensuring that logging operations do not block the client response.
func NewResponseWriterWrapper ¶
func NewResponseWriterWrapper(w gin.ResponseWriter, logger logging.RequestLogger, requestInfo *RequestInfo) *ResponseWriterWrapper
NewResponseWriterWrapper creates and initializes a new ResponseWriterWrapper. It takes the original gin.ResponseWriter, a logger instance, and request information.
Parameters:
- w: The original gin.ResponseWriter to wrap.
- logger: The logging service to use for recording requests.
- requestInfo: The pre-captured information about the incoming request.
Returns:
- A pointer to a new ResponseWriterWrapper.
func (*ResponseWriterWrapper) Finalize ¶
func (w *ResponseWriterWrapper) Finalize(c *gin.Context) error
Finalize completes the logging process for the request and response. For streaming responses, it closes the chunk channel and the stream writer. For non-streaming responses, it logs the complete request and response details, including any API-specific request/response data stored in the Gin context.
func (*ResponseWriterWrapper) Write ¶
func (w *ResponseWriterWrapper) Write(data []byte) (int, error)
Write wraps the underlying ResponseWriter's Write method to capture response data. For non-streaming responses, it writes to an internal buffer. For streaming responses, it sends data chunks to a non-blocking channel for asynchronous logging. CRITICAL: This method prioritizes writing to the client to ensure zero latency, handling logging operations subsequently.
func (*ResponseWriterWrapper) WriteHeader ¶
func (w *ResponseWriterWrapper) WriteHeader(statusCode int)
WriteHeader wraps the underlying ResponseWriter's WriteHeader method. It captures the status code, detects if the response is streaming based on the Content-Type header, and initializes the appropriate logging mechanism (standard or streaming).
func (*ResponseWriterWrapper) WriteString ¶
func (w *ResponseWriterWrapper) WriteString(data string) (int, error)
WriteString wraps the underlying ResponseWriter's WriteString method to capture response data. Some handlers (and fmt/io helpers) write via io.StringWriter; without this override, those writes bypass Write() and would be missing from request logs.