Илья Глазунов 881028c1e6 feat: Add reverse proxy functionality with enhanced routing capabilities
- Introduced IgnoreRequestPath option in proxy configuration to allow exact match routing.
- Implemented proxy_pass directive in routing extension to handle backend requests.
- Enhanced error handling for backend unavailability and timeouts.
- Added integration tests for reverse proxy, including basic requests, exact match routes, regex routes, header forwarding, and query string preservation.
- Created helper functions for setting up test servers and backends, along with assertion utilities for response validation.
- Updated server initialization to support extension management and middleware chaining.
- Improved logging for debugging purposes during request handling.
2025-12-12 00:38:30 +03:00

112 lines
3.0 KiB
Go

package extension
import (
"context"
"net/http"
"github.com/konduktor/konduktor/internal/logging"
)
// Extension is the interface that all extensions must implement
type Extension interface {
// Name returns the unique name of the extension
Name() string
// Initialize is called when the extension is loaded
Initialize() error
// ProcessRequest processes an incoming request before routing.
// Returns:
// - response: if non-nil, the request is handled and no further processing occurs
// - handled: if true, the request was handled by this extension
// - err: any error that occurred
ProcessRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) (handled bool, err error)
// ProcessResponse is called after the response is generated but before it's sent.
// Extensions can modify the response here.
ProcessResponse(ctx context.Context, w http.ResponseWriter, r *http.Request)
// Cleanup is called when the extension is being unloaded
Cleanup() error
// Enabled returns whether the extension is currently enabled
Enabled() bool
// SetEnabled enables or disables the extension
SetEnabled(enabled bool)
// Priority returns the extension's priority (lower = earlier execution)
Priority() int
}
// BaseExtension provides a default implementation for common Extension methods
type BaseExtension struct {
name string
enabled bool
priority int
logger *logging.Logger
}
// NewBaseExtension creates a new BaseExtension
func NewBaseExtension(name string, priority int, logger *logging.Logger) BaseExtension {
return BaseExtension{
name: name,
enabled: true,
priority: priority,
logger: logger,
}
}
// Name returns the extension name
func (b *BaseExtension) Name() string {
return b.name
}
// Enabled returns whether the extension is enabled
func (b *BaseExtension) Enabled() bool {
return b.enabled
}
// SetEnabled sets the enabled state
func (b *BaseExtension) SetEnabled(enabled bool) {
b.enabled = enabled
}
// Priority returns the extension priority
func (b *BaseExtension) Priority() int {
return b.priority
}
// Initialize default implementation (no-op)
func (b *BaseExtension) Initialize() error {
return nil
}
// Cleanup default implementation (no-op)
func (b *BaseExtension) Cleanup() error {
return nil
}
// ProcessRequest default implementation (pass-through)
func (b *BaseExtension) ProcessRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) (bool, error) {
return false, nil
}
// ProcessResponse default implementation (no-op)
func (b *BaseExtension) ProcessResponse(ctx context.Context, w http.ResponseWriter, r *http.Request) {
}
// Logger returns the extension's logger
func (b *BaseExtension) Logger() *logging.Logger {
return b.logger
}
// ExtensionConfig holds configuration for creating extensions
type ExtensionConfig struct {
Type string
Config map[string]interface{}
}
// ExtensionFactory is a function that creates an extension from config
type ExtensionFactory func(config map[string]interface{}, logger *logging.Logger) (Extension, error)