105 lines
2.9 KiB
Go
105 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"reanimator/internal/ingest"
|
|
"reanimator/internal/repository/analytics"
|
|
"reanimator/internal/repository/failures"
|
|
"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)
|
|
failureRepo := failures.NewFailureRepository(db)
|
|
analyticsRepo := analytics.NewRepository(db)
|
|
assetRepo := registry.NewAssetRepository(db)
|
|
componentRepo := registry.NewComponentRepository(db)
|
|
lotRepo := registry.NewLotRepository(db)
|
|
lotMappingRepo := registry.NewLotModelMappingRepository(db)
|
|
installationRepo := registry.NewInstallationRepository(db)
|
|
timelineRepo := timeline.NewEventRepository(db)
|
|
|
|
RegisterRegistryRoutes(mux, RegistryDependencies{
|
|
Customers: registry.NewCustomerRepository(db),
|
|
Projects: registry.NewProjectRepository(db),
|
|
Assets: assetRepo,
|
|
Components: componentRepo,
|
|
Lots: lotRepo,
|
|
LotMappings: lotMappingRepo,
|
|
})
|
|
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,
|
|
})
|
|
RegisterFailureRoutes(mux, FailureDependencies{
|
|
Failures: failureRepo,
|
|
Components: componentRepo,
|
|
Assets: assetRepo,
|
|
})
|
|
RegisterAnalyticsRoutes(mux, AnalyticsDependencies{
|
|
Analytics: analyticsRepo,
|
|
})
|
|
RegisterUIRoutes(mux, UIDependencies{
|
|
Customers: registry.NewCustomerRepository(db),
|
|
Projects: registry.NewProjectRepository(db),
|
|
Assets: assetRepo,
|
|
Components: componentRepo,
|
|
Lots: lotRepo,
|
|
LotMappings: lotMappingRepo,
|
|
Installations: installationRepo,
|
|
Timeline: timelineRepo,
|
|
Tickets: ticketRepo,
|
|
Failures: failureRepo,
|
|
Analytics: analyticsRepo,
|
|
})
|
|
}
|
|
|
|
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"})
|
|
}
|