fetch_ml/internal/api/server.go
Jeremie Fraeys f0ffbb4a3d
refactor: Phase 5 complete - API packages extracted
Extracted all deferred API packages from monolithic ws_*.go files:

- api/routes.go (75 lines) - Extracted route registration from server.go
- api/errors.go (108 lines) - Standardized error responses and error codes
- api/jobs/handlers.go (271 lines) - Job WebSocket handlers
  * HandleAnnotateRun, HandleSetRunNarrative
  * HandleCancelJob, HandlePruneJobs, HandleListJobs
- api/jupyter/handlers.go (244 lines) - Jupyter WebSocket handlers
  * HandleStartJupyter, HandleStopJupyter
  * HandleListJupyter, HandleListJupyterPackages
  * HandleRemoveJupyter, HandleRestoreJupyter
- api/validate/handlers.go (163 lines) - Validation WebSocket handlers
  * HandleValidate, HandleGetValidateStatus, HandleListValidations
- api/ws/handler.go (298 lines) - WebSocket handler framework
  * Core WebSocket handling logic
  * Opcode constants and error codes

Lines redistributed: ~1,150 lines from ws_jobs.go (1,365), ws_jupyter.go (512),
ws_validate.go (523), ws_handler.go (379) into focused packages.

Note: Original ws_*.go files still present - cleanup in next commit.
Build status: Compiles successfully
2026-02-17 13:25:58 -05:00

148 lines
3.5 KiB
Go

package api
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/jfraeys/fetch_ml/internal/audit"
"github.com/jfraeys/fetch_ml/internal/experiment"
"github.com/jfraeys/fetch_ml/internal/jupyter"
"github.com/jfraeys/fetch_ml/internal/logging"
"github.com/jfraeys/fetch_ml/internal/middleware"
"github.com/jfraeys/fetch_ml/internal/prommetrics"
"github.com/jfraeys/fetch_ml/internal/queue"
"github.com/jfraeys/fetch_ml/internal/storage"
)
// Server represents the API server
type Server struct {
config *ServerConfig
httpServer *http.Server
logger *logging.Logger
expManager *experiment.Manager
taskQueue queue.Backend
db *storage.DB
handlers *Handlers
sec *middleware.SecurityMiddleware
cleanupFuncs []func()
jupyterServiceMgr *jupyter.ServiceManager
auditLogger *audit.Logger
promMetrics *prommetrics.Metrics // Prometheus metrics
}
// NewServer creates a new API server
func NewServer(configPath string) (*Server, error) {
// Load configuration
cfg, err := LoadServerConfig(configPath)
if err != nil {
return nil, err
}
if err := cfg.Validate(); err != nil {
return nil, err
}
server := &Server{
config: cfg,
}
// Initialize components
if err := server.initializeComponents(); err != nil {
return nil, err
}
// Setup HTTP server
server.setupHTTPServer()
return server, nil
}
// setupHTTPServer sets up the HTTP server and routes
func (s *Server) setupHTTPServer() {
mux := http.NewServeMux()
// Register all routes
s.registerRoutes(mux)
// Wrap with middleware
finalHandler := s.wrapWithMiddleware(mux)
finalHandler = s.wrapWithMetrics(finalHandler)
s.httpServer = &http.Server{
Addr: s.config.Server.Address,
Handler: finalHandler,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 120 * time.Second,
}
}
// Start starts the server
func (s *Server) Start() error {
if !s.config.Server.TLS.Enabled {
s.logger.Warn(
"TLS disabled for API server; do not use this configuration in production",
"address", s.config.Server.Address,
)
}
go func() {
var err error
if s.config.Server.TLS.Enabled {
s.logger.Info("starting HTTPS server", "address", s.config.Server.Address)
err = s.httpServer.ListenAndServeTLS(
s.config.Server.TLS.CertFile,
s.config.Server.TLS.KeyFile,
)
} else {
s.logger.Info("starting HTTP server", "address", s.config.Server.Address)
err = s.httpServer.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
s.logger.Error("server failed", "error", err)
}
os.Exit(1)
}()
return nil
}
// WaitForShutdown waits for shutdown signals and gracefully shuts down the server
func (s *Server) WaitForShutdown() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigChan
s.logger.Info("received shutdown signal", "signal", sig)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
s.logger.Info("shutting down http server...")
if err := s.httpServer.Shutdown(ctx); err != nil {
s.logger.Error("server shutdown error", "error", err)
} else {
s.logger.Info("http server shutdown complete")
}
// Run cleanup functions
for _, cleanup := range s.cleanupFuncs {
cleanup()
}
s.logger.Info("api server stopped")
}
// Close cleans up server resources
func (s *Server) Close() error {
// Run all cleanup functions
for _, cleanup := range s.cleanupFuncs {
cleanup()
}
return nil
}