Documentation
¶
Overview ¶
Package server provides HTTP server implementation for Starport.
Index ¶
- Constants
- Variables
- func CORS(cfg CORSConfig) func(http.Handler) http.Handler
- func LoggingMiddleware(next http.Handler) http.Handler
- func SecurityHeaders(next http.Handler) http.Handler
- func SizeLimiter(maxSize int64) func(http.Handler) http.Handler
- func Timeout(timeout time.Duration) func(http.Handler) http.Handler
- type AuthMiddleware
- func (m *AuthMiddleware) RequireAPIKey(next http.Handler) http.Handler
- func (m *AuthMiddleware) RequireAdmin(next http.Handler) http.Handler
- func (m *AuthMiddleware) RequireAnyScope(scopes ...string) func(http.Handler) http.Handler
- func (m *AuthMiddleware) RequireKeyOwnership(next http.Handler) http.Handler
- type CORSConfig
- type Config
- type Dependencies
- type Server
Constants ¶
const ( ContextKeyAPIKey contextKey = requestctx.APIKey ContextKeyAPIKeyID contextKey = requestctx.APIKeyID ContextKeyAPIKeyModel contextKey = requestctx.APIKeyModel )
Context keys for middleware
Variables ¶
var ( // ErrModelRequired is returned when neither model nor models array is provided ErrModelRequired = errors.New("model or models array is required") // ErrMessagesRequired is returned when messages are not provided ErrMessagesRequired = errors.New("messages are required") // ErrInvalidTemperature is returned when temperature is out of valid range ErrInvalidTemperature = errors.New("temperature must be between 0 and 2") // ErrInvalidTopP is returned when top_p is out of valid range ErrInvalidTopP = errors.New("top_p must be between 0 and 1") // ErrInvalidMaxTokens is returned when max_tokens is less than 1 ErrInvalidMaxTokens = errors.New("max_tokens must be at least 1") // ErrInvalidN is returned when n is less than 1 ErrInvalidN = errors.New("n must be at least 1") // ErrInvalidPresencePenalty is returned when presence_penalty is out of valid range ErrInvalidPresencePenalty = errors.New("presence_penalty must be between -2 and 2") // ErrInvalidFrequencyPenalty is returned when frequency_penalty is out of valid range ErrInvalidFrequencyPenalty = errors.New("frequency_penalty must be between -2 and 2") // ErrInvalidMinP is returned when min_p is out of valid range ErrInvalidMinP = errors.New("min_p must be between 0 and 1") // ErrInvalidTopA is returned when top_a is out of valid range ErrInvalidTopA = errors.New("top_a must be between 0 and 1") // ErrInvalidRepetitionPenalty is returned when repetition_penalty is out of valid range ErrInvalidRepetitionPenalty = errors.New("repetition_penalty must be greater than 0") // Embeddings validation errors // ErrEmbeddingsModelRequired is returned when model is not provided for embeddings ErrEmbeddingsModelRequired = errors.New("model is required") // ErrInputRequired is returned when input is not provided ErrInputRequired = errors.New("input is required") // ErrInvalidEncodingFormat is returned when encoding format is invalid ErrInvalidEncodingFormat = errors.New("encoding_format must be 'float' or 'base64'") )
Request validation errors
var ( RequestID = middleware.RequestID ClientIP = middleware.ClientIPFromRemoteAddr Recoverer = middleware.Recoverer Compress = middleware.Compress )
Middleware aliases for chi middleware.
var ( // ErrConfigRequired reports an absent HTTP server configuration. ErrConfigRequired = errors.New("server config is required") // ErrServiceRequired reports an absent gateway use-case service. ErrServiceRequired = errors.New("gateway service is required") // ErrIdentitiesRequired reports an absent identity repository. ErrIdentitiesRequired = errors.New("identity repository is required") // ErrProviderKeysRequired reports an absent provider-key service. ErrProviderKeysRequired = errors.New("provider key service is required") // ErrRateLimitsRequired reports an absent rate-limit repository. ErrRateLimitsRequired = errors.New("rate-limit repository is required") // ErrProviderOperationsRequired reports an absent provider operations port. ErrProviderOperationsRequired = errors.New("provider operations are required") )
Functions ¶
func CORS ¶
func CORS(cfg CORSConfig) func(http.Handler) http.Handler
CORS returns a configured CORS handler
func LoggingMiddleware ¶
LoggingMiddleware creates a custom logging middleware using zerolog
func SecurityHeaders ¶
SecurityHeaders adds security headers to responses
func SizeLimiter ¶
SizeLimiter limits the size of request bodies
Types ¶
type AuthMiddleware ¶
type AuthMiddleware struct {
// contains filtered or unexported fields
}
AuthMiddleware provides authentication functionality
func NewAuthMiddleware ¶
func NewAuthMiddleware(identities identity.Repository) *AuthMiddleware
NewAuthMiddleware creates a new authentication middleware
func (*AuthMiddleware) RequireAPIKey ¶
func (m *AuthMiddleware) RequireAPIKey(next http.Handler) http.Handler
RequireAPIKey validates API key authentication
func (*AuthMiddleware) RequireAdmin ¶
func (m *AuthMiddleware) RequireAdmin(next http.Handler) http.Handler
RequireAdmin validates admin privileges
func (*AuthMiddleware) RequireAnyScope ¶
RequireAnyScope validates that the authenticated API key has at least one accepted scope. The wildcard "*" grants access to all scopes.
func (*AuthMiddleware) RequireKeyOwnership ¶
func (m *AuthMiddleware) RequireKeyOwnership(next http.Handler) http.Handler
RequireKeyOwnership validates that the user owns the API key they're trying to manage
type CORSConfig ¶
type CORSConfig struct {
// AllowedOrigins is a list of origins a cross-domain request can be executed from
AllowedOrigins []string `env:"CORS_ALLOWED_ORIGINS,default=*"`
// AllowedMethods is a list of methods the client is allowed to use with cross-domain requests
AllowedMethods []string `env:"CORS_ALLOWED_METHODS,default=GET,POST,PUT,DELETE,OPTIONS"`
// AllowedHeaders is list of non simple headers the client is allowed to use with cross-domain requests
AllowedHeaders []string `env:"CORS_ALLOWED_HEADERS,default=Accept,Authorization,Content-Type,X-CSRF-Token"`
// ExposedHeaders indicates which headers are safe to expose to the API of a CORS API specification
ExposedHeaders []string `env:"CORS_EXPOSED_HEADERS,default="`
// AllowCredentials indicates whether the request can include user credentials
AllowCredentials bool `env:"CORS_ALLOW_CREDENTIALS,default=true"`
// MaxAge indicates how long (in seconds) the results of a preflight request can be cached
MaxAge int `env:"CORS_MAX_AGE,default=300"`
}
CORSConfig holds CORS configuration
type Config ¶
type Config struct {
// Port to listen on
Port int `env:"PORT,default=8080"`
// Host to bind to
Host string `env:"HOST,default=0.0.0.0"`
// Read and write timeouts
ReadTimeout time.Duration `env:"READ_TIMEOUT,default=10s"`
WriteTimeout time.Duration `env:"WRITE_TIMEOUT,default=10s"`
IdleTimeout time.Duration `env:"IDLE_TIMEOUT,default=120s"`
// Request timeout for middleware
RequestTimeout time.Duration `env:"REQUEST_TIMEOUT,default=60s"`
// Shutdown timeout
ShutdownTimeout time.Duration `env:"SHUTDOWN_TIMEOUT,default=30s"`
// Maximum request body size (default: 10MB)
MaxRequestSize int64 `env:"MAX_REQUEST_SIZE,default=10485760"`
// Maximum aggregate size of HTTP request headers.
MaxHeaderBytes int `env:"MAX_HEADER_BYTES,default=1048576"`
// Rate limiting configuration. Enforcement happens after API key
// authentication and uses the authenticated API key ID, not the raw secret.
EnableRateLimiting bool `env:"ENABLE_RATE_LIMITING,default=false"`
RateLimitRequestsPerWindow int64 `env:"RATE_LIMIT_REQUESTS_PER_WINDOW,default=0"`
RateLimitWindow time.Duration `env:"RATE_LIMIT_WINDOW,default=1m"`
// CORS configuration
CORS CORSConfig
}
Config holds server configuration
type Dependencies ¶
type Dependencies struct {
Service proxy.Proxy
Identities identity.Repository
ProviderKeys byok.ProviderKeys
RateLimits ratelimit.Repository
ProviderOperations controllers.ProviderOperations
ChatUI *chatui.Handler
}
Dependencies contains ready application ports for the HTTP adapter.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents the HTTP server with new handler organization
func New ¶
func New(config *Config, dependencies Dependencies) (*Server, error)
New creates an HTTP adapter from ready application dependencies.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package controllers contains HTTP handlers for the Starport API.
|
Package controllers contains HTTP handlers for the Starport API. |
|
Package dto owns shared administrative HTTP response values.
|
Package dto owns shared administrative HTTP response values. |
|
Package requestctx defines typed request context values shared by the server middleware and HTTP controllers.
|
Package requestctx defines typed request context values shared by the server middleware and HTTP controllers. |