-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.go
187 lines (162 loc) · 4.85 KB
/
server.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"bytes"
"html/template"
"log"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/distribution/reference"
"github.com/gorilla/mux"
"github.com/opencontainers/go-digest"
"github.com/utilitywarehouse/registry-browser/registry"
"github.com/utilitywarehouse/registry-browser/s3"
)
// templatePlus subtracts two ints within a template
func templatePlus(a, b int) int {
return a + b
}
// breadCrumb facilitates building breadcrumb style navigation menus of the
// form: a / b / c, where 'c' is a hyperlink to 'a/b/c', 'b' to 'a/b' and 'a' to
// 'a'
type breadCrumb struct {
Segment string
Path string
}
// templateBreadCrumbs creates breadcrumbs for a repository name within a
// template
func templateBreadCrumbs(name string) []breadCrumb {
bc := []breadCrumb{}
segments := strings.Split(name, "/")
for i := 0; i < len(segments); i++ {
e := breadCrumb{Segment: segments[i], Path: strings.Join(segments[0:i+1], "/")}
bc = append(bc, e)
}
return bc
}
// server implements http.Handler, it serves the browser
type server struct {
http.Handler
r *registry.Client
s3 *s3.Client
tmpl *template.Template
}
// newServer returns a new server
func newServer(r *registry.Client, s *s3.Client) (*server, error) {
tmpl, err := template.New("").Funcs(template.FuncMap{
"plus": templatePlus,
"breadCrumbs": templateBreadCrumbs,
}).ParseFiles("./templates/manifests.html", "./templates/list.html")
if err != nil {
return nil, err
}
srv := &server{
r: r,
s3: s,
tmpl: tmpl,
}
m := mux.NewRouter()
m.HandleFunc("/repository/{name:"+reference.NameRegexp.String()+"}/manifests/{reference:"+reference.TagRegexp.String()+"|"+digest.DigestRegexp.String()+"}", srv.handleManifests)
m.HandleFunc("/repository/{name:"+reference.NameRegexp.String()+"}", srv.handleList)
m.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
m.HandleFunc("/", srv.handleList)
srv.Handler = m
return srv, nil
}
// handleList handles requests for /repository/{name} where name is a valid
// repository path. It displays the subpaths of that path and/or any tags if
// it's a repository.
func (s *server) handleList(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"]
items, err := s.s3.List(name)
if err != nil {
http.Error(w, "error retrieving repositories", http.StatusInternalServerError)
log.Printf("error: %s", err)
return
}
var repos []string
for _, item := range items {
// Remove _layers and _manifests, which aren't repositories or part of
// a repository path
if item != "_layers" && item != "_manifests" {
repos = append(repos, item)
}
}
tags, err := s.s3.List(filepath.Join(name, "_manifests/tags"))
if err != nil {
http.Error(w, "error retrieving tags", http.StatusInternalServerError)
log.Printf("error: %s", err)
return
}
var data struct {
Name string
Repos []string
Tags []string
}
data.Name = name
data.Repos = repos
data.Tags = tags
rendered := &bytes.Buffer{}
if err := s.tmpl.ExecuteTemplate(rendered, "list.html", data); err != nil {
http.Error(w, "error rendering template", http.StatusInternalServerError)
log.Printf("error: %s", err)
return
}
w.WriteHeader(http.StatusOK)
if _, err := rendered.WriteTo(w); err != nil {
log.Printf("error: %s", err)
}
}
// handleManifests handles requests for /repository/{name}/manifests/{reference}
// where name is the name of a repository and reference is a tag or digest. It
// displays manifest information.
func (s *server) handleManifests(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"]
reference := mux.Vars(r)["reference"]
manifestInfo, err := s.r.ManifestInfo(name, reference)
if err != nil {
http.Error(w, "error fetching manifest", http.StatusInternalServerError)
log.Printf("error: %s", err)
return
}
// Find created time
var created time.Time
if manifestInfo.Config != nil {
created = manifestInfo.Config.Created
}
// Calculate the total image size
var imageSize int64
for _, l := range manifestInfo.Layers {
imageSize = imageSize + l.Size
}
// Count the layers
layersCount := len(manifestInfo.Layers)
if layersCount == 0 && manifestInfo.Config != nil {
layersCount = len(manifestInfo.Config.History)
}
var data struct {
Name string
Reference string
Created time.Time
Size int64
Layers int
Manifest *registry.ManifestInfo
}
data.Name = name
data.Reference = reference
data.Created = created
data.Size = imageSize
data.Layers = layersCount
data.Manifest = manifestInfo
rendered := &bytes.Buffer{}
if err := s.tmpl.ExecuteTemplate(rendered, "manifests.html", data); err != nil {
http.Error(w, "error rendering template", http.StatusInternalServerError)
log.Printf("error: %s", err)
return
}
w.WriteHeader(http.StatusOK)
if _, err := rendered.WriteTo(w); err != nil {
log.Printf("error: %s", err)
}
}