-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathhome_handler.go
87 lines (78 loc) · 2.37 KB
/
home_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
85
86
87
package main
import (
"html/template"
"net/http"
"os"
"sort"
"github.com/coreos/go-etcd/etcd"
"github.com/gengo/goship/lib/acl"
"github.com/gengo/goship/lib/auth"
"github.com/gengo/goship/lib/config"
helpers "github.com/gengo/goship/lib/view-helpers"
"github.com/gengo/goship/plugins/plugin"
"github.com/golang/glog"
)
// HomeHandler is the main home screen
type HomeHandler struct {
ac acl.AccessControl
ecl *etcd.Client
assets helpers.Assets
}
func (h HomeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c, err := config.Load(h.ecl)
if err != nil {
glog.Errorf("Failed to Parse to ETCD data %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
u, err := auth.CurrentUser(r)
if err != nil {
glog.Errorf("Failed to get current user: %v", err)
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
t, err := template.New("index.html").ParseFiles("templates/index.html", "templates/base.html")
if err != nil {
glog.Errorf("Failed to parse template: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
projs := acl.ReadableProjects(h.ac, c.Projects, u)
sort.Sort(ByName(c.Projects))
// columns maps a plugin name to a list of columns
columns := make(map[string][]plugin.Column)
for _, pl := range plugin.Plugins {
for _, p := range c.Projects {
cols, err := pl.Apply(p)
if err != nil {
glog.Errorf("Failed to apply plugin: %s", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
columns[p.Name] = append(columns[p.Name], cols...)
}
}
js, css := h.assets.Templates()
gt := os.Getenv(gitHubAPITokenEnvVar)
var pt string
if c.Pivotal != nil {
pt = c.Pivotal.Token
}
params := map[string]interface{}{
"Javascript": js,
"Stylesheet": css,
"Projects": projs,
"PluginColumns": columns,
"User": u,
"Page": "home",
"ConfirmDeployFlag": *confirmDeployFlag,
"GithubToken": gt,
"PivotalToken": pt,
}
helpers.RespondWithTemplate(w, "text/html", t, "base", params)
}
// ByName is the interface for sorting projects
type ByName []config.Project
func (slice ByName) Len() int { return len(slice) }
func (slice ByName) Less(i, j int) bool { return slice[i].Name < slice[j].Name }
func (slice ByName) Swap(i, j int) { slice[i], slice[j] = slice[j], slice[i] }