forked from vmware-archive/glider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builds.go
90 lines (69 loc) · 1.73 KB
/
builds.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package handler
import (
"encoding/json"
"errors"
"net/http"
"sort"
"sync"
"time"
"github.com/nu7hatch/gouuid"
"github.com/pivotal-golang/lager"
"github.com/concourse/glider/api/builds"
"github.com/concourse/logbuffer"
)
func (handler *Handler) CreateBuild(w http.ResponseWriter, r *http.Request) {
var build builds.Build
err := json.NewDecoder(r.Body).Decode(&build)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
err = handler.validateBuild(build)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
uuid, err := uuid.NewV4()
if err != nil {
panic(err)
}
build.Guid = uuid.String()
build.CreatedAt = time.Now()
log := handler.logger.Session("create", lager.Data{
"build": build,
})
log.Info("register")
handler.bitsMutex.Lock()
handler.bits[build.Guid] = BitsSession{
bits: make(chan *http.Request, 1),
servingBits: &sync.WaitGroup{},
}
handler.bitsMutex.Unlock()
handler.logsMutex.Lock()
handler.logs[build.Guid] = logbuffer.NewLogBuffer()
handler.logsMutex.Unlock()
handler.buildsMutex.Lock()
handler.builds[build.Guid] = &build
handler.buildsMutex.Unlock()
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(build)
}
func (handler *Handler) GetBuild(w http.ResponseWriter, r *http.Request) {
handler.buildsMutex.RLock()
builds := make([]builds.Build, len(handler.builds))
i := 0
for _, build := range handler.builds {
builds[i] = *build
i++
}
handler.buildsMutex.RUnlock()
sort.Sort(sort.Reverse(ByCreatedAt(builds)))
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(builds)
}
func (handler *Handler) validateBuild(build builds.Build) error {
if build.Config.Image == "" {
return errors.New("missing build image")
}
return nil
}