Documentation
¶
Overview ¶
Package server provides the HTTP server implementation for the xw application.
This package implements the server-side of the xw API, handling incoming HTTP requests from CLI clients and other API consumers. It provides:
- RESTful API endpoints for all operations
- Request routing and middleware
- Integration with model registry and device manager
- Graceful shutdown support
- Request logging and error handling
The server is designed to run as a long-lived service process, either as a standalone daemon or as a systemd service. It maintains state including loaded models and device availability.
Example usage:
cfg := config.NewDefaultConfig()
srv := server.NewServer(cfg)
if err := srv.Start(); err != nil {
log.Fatalf("Server failed: %v", err)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitializeModels ¶
InitializeModels loads and registers models from configuration.
This function should be called during server startup to populate the model registry with models defined in the configuration file.
Parameters:
- configPath: Optional path to model configuration file (empty for default)
Returns:
- Error if model configuration loading fails (non-fatal, logs warning)
func InitializeRuntimeManager ¶
InitializeRuntimeManager creates and initializes the runtime manager with all available runtime implementations.
This function is responsible for:
- Creating the runtime manager
- Discovering and registering available runtimes
- Starting background tasks
Runtime registration failures are logged but don't cause the function to fail, allowing the system to operate with whatever runtimes are available.
Parameters:
- cfg: Configuration providing access to runtime parameters
Returns:
- Configured runtime manager
- Error only if manager creation fails
Types ¶
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the HTTP server that handles API requests from clients.
The Server manages the complete lifecycle of the xw service including:
- HTTP request handling and routing
- Model registry and device manager integration
- Graceful startup and shutdown
- Request logging and monitoring
The server is thread-safe and can handle multiple concurrent requests. It uses standard Go HTTP server with configurable timeouts and limits.
func NewServer ¶
NewServer creates and initializes a new server instance.
The server is created with the provided configuration and initializes all required subsystems including:
- Model registry with default models
- Device manager with hardware detection
- Version information for diagnostics
The server is ready to start after creation but is not yet listening for connections. Call Start() to begin accepting requests.
Parameters:
- cfg: The configuration for the server
- runtimeMgr: The runtime manager
- version: Server version string
Returns:
- A pointer to a fully initialized Server ready to start.
Example:
cfg := config.NewDefaultConfig()
srv := server.NewServer(cfg, runtimeMgr, "1.0.0")
if err := srv.Start(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server error: %v", err)
}
func (*Server) Start ¶
Start starts the HTTP server and begins accepting connections.
This method configures the HTTP server with all routes and middleware, then starts listening on the configured host and port. The method blocks until the server is shut down via Stop() or encounters a fatal error.
The server registers the following endpoints:
- GET /api/health - Health check
- GET /api/version - Version information
- POST /api/models/list - List available models
- POST /api/models/pull - Pull a model
- POST /api/run - Execute a model
All requests are logged through the logging middleware.
Returns:
- nil if the server shuts down gracefully
- http.ErrServerClosed after graceful shutdown
- error if the server fails to start or encounters a fatal error
Example:
go func() {
if err := srv.Start(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server failed: %v", err)
}
}()
// Wait for signal to shutdown...
srv.Stop(context.Background())
func (*Server) Stop ¶
Stop gracefully shuts down the server without interrupting active connections.
This method initiates a graceful shutdown of the HTTP server. It:
- Stops accepting new connections
- Waits for active requests to complete
- Respects the timeout in the provided context
- Cleans up resources
If the context expires before all connections close, the server is forcefully terminated.
Parameters:
- ctx: Context with timeout for graceful shutdown
Returns:
- nil if shutdown completes within the timeout
- error if shutdown fails or times out
Example:
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := srv.Stop(ctx); err != nil {
logger.Error("Shutdown error: %v", err)
}