package api import ( "context" "database/sql" "encoding/json" "net/http" "time" "reanimator/internal/ingest" "reanimator/internal/repository/registry" "reanimator/internal/repository/tickets" "reanimator/internal/repository/timeline" ) type Server struct { httpServer *http.Server } func NewServer(addr string, readTimeout, writeTimeout time.Duration, db *sql.DB) *Server { mux := http.NewServeMux() mux.HandleFunc("/health", healthHandler) if db != nil { ticketRepo := tickets.NewTicketRepository(db) assetRepo := registry.NewAssetRepository(db) componentRepo := registry.NewComponentRepository(db) installationRepo := registry.NewInstallationRepository(db) timelineRepo := timeline.NewEventRepository(db) RegisterRegistryRoutes(mux, RegistryDependencies{ Customers: registry.NewCustomerRepository(db), Projects: registry.NewProjectRepository(db), Assets: assetRepo, Components: componentRepo, }) RegisterIngestRoutes(mux, IngestDependencies{ Service: ingest.NewService(db), }) RegisterAssetComponentRoutes(mux, AssetComponentDependencies{ Assets: assetRepo, Components: componentRepo, Installations: installationRepo, Tickets: ticketRepo, Timeline: timelineRepo, }) RegisterTicketRoutes(mux, TicketDependencies{ Tickets: ticketRepo, Assets: assetRepo, }) RegisterUIRoutes(mux, UIDependencies{ Assets: assetRepo, Installations: installationRepo, Timeline: timelineRepo, Tickets: ticketRepo, }) } return &Server{ httpServer: &http.Server{ Addr: addr, Handler: mux, ReadTimeout: readTimeout, WriteTimeout: writeTimeout, }, } } func (s *Server) Start() error { return s.httpServer.ListenAndServe() } func (s *Server) Shutdown(ctx context.Context) error { return s.httpServer.Shutdown(ctx) } func healthHandler(w http.ResponseWriter, _ *http.Request) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusOK) _ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) }