list.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package infra
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/api/server/handlers"
  5. "github.com/porter-dev/porter/api/server/shared"
  6. "github.com/porter-dev/porter/api/server/shared/apierrors"
  7. "github.com/porter-dev/porter/api/server/shared/config"
  8. "github.com/porter-dev/porter/api/types"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. type InfraListHandler struct {
  12. handlers.PorterHandlerWriter
  13. }
  14. func NewInfraListHandler(
  15. config *config.Config,
  16. writer shared.ResultWriter,
  17. ) *InfraListHandler {
  18. return &InfraListHandler{
  19. PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
  20. }
  21. }
  22. func (p *InfraListHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  23. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  24. infras, err := p.Repo().Infra().ListInfrasByProjectID(proj.ID)
  25. if err != nil {
  26. p.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  27. }
  28. infraList := make([]*types.Infra, 0)
  29. for _, infra := range infras {
  30. infraList = append(infraList, infra.ToInfraType())
  31. }
  32. var res types.ListProjectInfraResponse = infraList
  33. p.WriteResult(w, r, res)
  34. }