package httpserver import ( "context" "net" "net/http" "github.com/toki/oto/services/core/internal/cicdstate" "github.com/toki/oto/services/core/internal/runnerregistry" ) // Server wraps the HTTP server for the OTO Core service. type Server struct { httpServer *http.Server } // NewServer creates a new instance of Server. func NewServer(addr string) *Server { return NewServerWithRegistryAndStore(addr, runnerregistry.New(), cicdstate.NewStore()) } // NewServerWithRegistry creates a server using an injected runner registry. func NewServerWithRegistry(addr string, registry *runnerregistry.Registry) *Server { return NewServerWithRegistryAndStore(addr, registry, cicdstate.NewStore()) } // NewServerWithRegistryAndStore creates a server with injected runner registry and CICD store. func NewServerWithRegistryAndStore(addr string, registry *runnerregistry.Registry, store *cicdstate.Store) *Server { mux := http.NewServeMux() registerRoutes(mux, registry, store) return &Server{ httpServer: &http.Server{ Addr: addr, Handler: mux, }, } } // Start starts the HTTP server. func (s *Server) Start() error { return s.httpServer.ListenAndServe() } // StartListener starts the HTTP server using a custom net.Listener. // Useful for testing with dynamic ports. func (s *Server) StartListener(ln net.Listener) error { return s.httpServer.Serve(ln) } // Shutdown gracefully shuts down the server. func (s *Server) Shutdown(ctx context.Context) error { return s.httpServer.Shutdown(ctx) }