forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
48 lines (41 loc) · 1.18 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package resourceserver
import (
"encoding/json"
"net/http"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/api/accessor"
"github.com/concourse/atc/api/present"
"github.com/concourse/atc/db"
)
func (s *Server) GetResource(pipeline db.Pipeline) http.Handler {
logger := s.logger.Session("get-resource")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resourceName := r.FormValue(":resource_name")
teamName := r.FormValue(":team_name")
dbResource, found, err := pipeline.Resource(resourceName)
if err != nil {
logger.Error("failed-to-get-resource", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !found {
logger.Debug("resource-not-found", lager.Data{"resource": resourceName})
w.WriteHeader(http.StatusNotFound)
return
}
acc := accessor.GetAccessor(r)
resource := present.Resource(
dbResource,
pipeline.Groups(),
acc.IsAuthenticated(),
teamName,
)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(resource)
if err != nil {
logger.Error("failed-to-encode-resource", err)
w.WriteHeader(http.StatusInternalServerError)
}
})
}