-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.go
132 lines (120 loc) · 3.14 KB
/
web.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package listing
import (
"fmt"
"html/template"
"net/http"
"time"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
func (s *Lister) addRoutes(router *mux.Router) {
if *ShowListings {
log.Infof("Enabling listings")
s.getTemplates()
router.Path("/").HandlerFunc(s.ListingIndex)
router.Path("/css").HandlerFunc(s.CSS)
router.Path("/js").HandlerFunc(s.JS)
s.start()
} else if *ShowIndex {
log.Infof("Showing index only")
s.getTemplates()
router.Path("/").HandlerFunc(s.Index)
router.Path("/css").HandlerFunc(s.CSS)
} else {
log.Infof("Not showing index or listings")
router.Path("/").HandlerFunc(s.OK)
}
}
func (s *Lister) JS(writer http.ResponseWriter, _ *http.Request) {
writer.Header().Add("Content-Type", "application/javascript")
err := s.templates.ExecuteTemplate(writer, "main.js", nil)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
}
func (s *Lister) CSS(writer http.ResponseWriter, _ *http.Request) {
writer.Header().Add("Content-Type", "text/css")
err := s.templates.ExecuteTemplate(writer, "normalize.css", nil)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
err = s.templates.ExecuteTemplate(writer, "main.css", nil)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
}
func (s *Lister) OK(writer http.ResponseWriter, _ *http.Request) {
writer.WriteHeader(http.StatusOK)
}
func (s *Lister) Index(writer http.ResponseWriter, req *http.Request) {
err := s.templates.ExecuteTemplate(writer, "index.gohtml", Index{
Title: s.getHostname(req),
})
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
}
func (s *Lister) ListingIndex(writer http.ResponseWriter, req *http.Request) {
err := s.templates.ExecuteTemplate(writer, "listingIndex.gohtml", ListingIndex{
Title: s.getHostname(req),
Repositories: s.repositories,
LastPolled: s.lastPoll,
})
if err != nil {
log.Printf("Unable to output template: %s", err)
writer.WriteHeader(http.StatusInternalServerError)
return
}
}
func (s *Lister) getTemplates() {
s.templates = template.Must(template.New("").
Funcs(template.FuncMap{
"TagPrint": tagPrint,
"HumanSize": humanBytes,
"DisplayTime": func(format time.Time) string {
return format.Format("02-01 15:04")
},
}).
ParseFS(templates, "templates/*.gohtml", "templates/*.css", "templates/*.js"))
}
func tagPrint(input []Tag) string {
if len(input) == 0 {
return "No Tags"
}
output := ""
for index := range input {
if index > 4 {
output += ", ..."
break
}
if index != 0 {
output += ", "
}
output += input[index].Name + " (" + humanBytes(input[index].Size) + ")"
}
return output
}
func humanBytes(b int) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}
func (s *Lister) getHostname(req *http.Request) string {
if *PullHostname != "" {
return *PullHostname
} else if req != nil && req.Host != "" {
return req.Host
}
return "Docker Registry"
}