단일 server.go에 집중되어 있던 HTTP 라우팅 로직을 실행/작업/런너 핸들러 파일로 분리해 책임 경계를 더 선명하게 하며, milestone 기록 파일의 보관 위치도 정리한다.
54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
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)
|
|
}
|