1package configserver
2
3import (
4	"encoding/json"
5	"fmt"
6	"net/http"
7
8	"code.cloudfoundry.org/lager"
9
10	"github.com/concourse/concourse/atc"
11	"github.com/tedsuo/rata"
12)
13
14func (s *Server) GetConfig(w http.ResponseWriter, r *http.Request) {
15	logger := s.logger.Session("get-config")
16	pipelineName := rata.Param(r, "pipeline_name")
17	teamName := rata.Param(r, "team_name")
18
19	team, found, err := s.teamFactory.FindTeam(teamName)
20	if err != nil {
21		logger.Error("failed-to-find-team", err)
22		w.WriteHeader(http.StatusInternalServerError)
23		return
24	}
25
26	if !found {
27		logger.Debug("team-not-found", lager.Data{"team": teamName})
28		w.WriteHeader(http.StatusNotFound)
29		return
30	}
31
32	pipeline, found, err := team.Pipeline(pipelineName)
33	if err != nil {
34		logger.Error("failed-to-find-pipeline", err)
35		w.WriteHeader(http.StatusInternalServerError)
36		return
37	}
38
39	if !found {
40		logger.Debug("pipeline-not-found", lager.Data{"pipeline": pipelineName})
41		w.WriteHeader(http.StatusNotFound)
42		return
43	}
44
45	if pipeline.Archived() {
46		logger.Debug("pipeline-is-archived", lager.Data{"pipeline": pipelineName})
47		w.WriteHeader(http.StatusNotFound)
48		return
49	}
50
51	config, err := pipeline.Config()
52	if err != nil {
53		logger.Error("failed-to-get-pipeline-config", err)
54		w.WriteHeader(http.StatusInternalServerError)
55		return
56	}
57
58	w.Header().Set(atc.ConfigVersionHeader, fmt.Sprintf("%d", pipeline.ConfigVersion()))
59	w.Header().Set("Content-Type", "application/json")
60
61	err = json.NewEncoder(w).Encode(atc.ConfigResponse{
62		Config: config,
63	})
64	if err != nil {
65		logger.Error("failed-to-encode-config", err)
66		w.WriteHeader(http.StatusInternalServerError)
67	}
68}
69