forked from vmware-archive/atc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
84 lines (64 loc) · 1.59 KB
/
handler.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
package getbuilds
import (
"html/template"
"net/http"
"strconv"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/web"
"github.com/concourse/go-concourse/concourse"
)
type Handler struct {
logger lager.Logger
clientFactory web.ClientFactory
template *template.Template
}
func NewHandler(logger lager.Logger, clientFactory web.ClientFactory, template *template.Template) *Handler {
return &Handler{
logger: logger,
clientFactory: clientFactory,
template: template,
}
}
type TemplateData struct {
Builds []PresentedBuild
Pagination concourse.Pagination
}
func FetchTemplateData(client concourse.Client, page concourse.Page) (TemplateData, error) {
builds, pagination, err := client.Builds(page)
if err != nil {
return TemplateData{}, err
}
return TemplateData{
Builds: PresentBuilds(builds),
Pagination: pagination,
}, nil
}
func (handler *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) error {
client := handler.clientFactory.Build(r)
log := handler.logger.Session("builds")
since, parseErr := strconv.Atoi(r.FormValue("since"))
if parseErr != nil {
since = 0
}
until, parseErr := strconv.Atoi(r.FormValue("until"))
if parseErr != nil {
until = 0
}
page := concourse.Page{
Since: since,
Until: until,
Limit: atc.PaginationWebLimit,
}
templateData, err := FetchTemplateData(client, page)
if err != nil {
log.Error("failed-to-build-template-data", err)
return err
}
err = handler.template.Execute(w, templateData)
if err != nil {
log.Fatal("failed-to-build-template", err)
return err
}
return nil
}