/
datadex_web.go
295 lines (252 loc) · 6.26 KB
/
datadex_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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package datadex
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/jbenet/data"
"html/template"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
)
var webTmpl *template.Template
var webDocPages = map[string]*DocWebPage{}
const baseTmplName = "base.html"
const homeTmplName = "home.html"
const userTmplName = "user.html"
const datasetTmplName = "dataset.html"
const docTmplName = "doc.html"
const listTmplName = "list.html"
func init() {
// templates
webTmpl = template.New("web")
webTmpl.Funcs(template.FuncMap{
"timeago": data.TimeAgo,
"unescaped": func(s string) interface{} { return template.HTML(s) },
"md5": md5Hash,
"title": strings.Title,
"replace": func(f, r, s string) string {
return strings.Replace(s, f, r, -1)
},
})
_, err := webTmpl.ParseGlob("web/tmpl/*.html")
if err != nil {
pErr("%v\n", err)
os.Exit(-1)
}
// doc pages
mdfiles, err := filepath.Glob("web/md/*.md")
if err != nil {
pErr("%v\n", err)
os.Exit(-1)
}
for _, f := range mdfiles {
p, err := ReadMarkdownWebPage(f)
if err != nil {
pErr("%v\n", err)
os.Exit(-1)
}
// register page
webDocPages[p.route] = p
}
}
type WebPage struct {
Title string
Description string
BodyPage interface{}
LoggedIn bool
Username string
}
type UserWebPage struct {
Username string
Name string
Bio string
Email string
Github string
Twitter string
Website string
Packages []*Dataset
}
func webUserHandler(w http.ResponseWriter, r *http.Request) {
u, err := requestedUser(r)
if err != nil {
pErr("%v\n", err)
http.Error(w, "user not found", http.StatusNotFound)
return
}
pkgs, err := indexDB.GetUserDatasets(u.Username)
if err != nil {
pErr("Error retrieving datasets for %s -- %v\n", u.Username, err)
http.Error(w, "error retrieving packages", http.StatusInternalServerError)
return
}
sort.Sort(sort.Reverse(DatasetsByLastUpdated(pkgs)))
webRenderPage(w, r, userTmplName, &WebPage{
Title: u.User(),
Description: fmt.Sprintf("%s has published %d datasets",
u.User(), len(u.Profile.Packages)),
BodyPage: &UserWebPage{
Username: u.User(),
Name: u.Profile.Name,
Email: u.Profile.Email,
Github: u.Profile.Github,
Twitter: u.Profile.Twitter,
Website: u.Profile.Website,
Packages: pkgs,
},
})
}
type DatasetWebPage struct {
I *Dataset
D *data.Datafile
Readme string
EscapeReadme bool
}
func webDsHomeHandler(w http.ResponseWriter, r *http.Request) {
ds := requestDataset(r)
h := data.NewHandle(ds)
f, err := indexDB.GetDataset(h.Path())
if err != nil {
pErr("%s 404 not found\n", ds)
http.NotFound(w, r)
return
}
ref := f.Refs.ResolveRef(h.Version)
df, err := DatafileForManifestRef(ref)
if err != nil {
pErr("Error loading Datafile: %s\n", err.Error())
http.Error(w, "error loading Datafile", http.StatusInternalServerError)
return
}
readme, err := FileForManifestRef(ref)
if err != nil {
pErr("Error finding Readme: %v\n", err)
readme = noReadmeFile
} else {
if err := readme.Read(); err != nil {
pErr("Error loading Readme: %v\n", err)
}
}
webRenderPage(w, r, datasetTmplName, &WebPage{
Title: f.Path,
Description: fmt.Sprintf("%s - %s", f.Path, f.Tagline),
BodyPage: &DatasetWebPage{
I: f,
D: df,
Readme: string(readme.RenderedBytes()),
EscapeReadme: !readme.CanBeRendered(),
},
})
}
type DocWebPage struct {
route string
Title string
Description string
Toc string
Doc string
}
func webDocHandler(w http.ResponseWriter, r *http.Request) {
p, found := webDocPages[r.URL.Path]
if !found {
pErr("%s 404 doc page not found\n", p)
http.NotFound(w, r)
return
}
webRenderPage(w, r, docTmplName, &WebPage{
Title: p.Title,
Description: p.Description,
BodyPage: p,
})
}
type ListWebPage struct {
List []interface{}
Kind string
Order string
Lists *map[string][]string
}
func webListHandler(w http.ResponseWriter, r *http.Request) {
kind := mux.Vars(r)["kind"]
order := mux.Vars(r)["order"]
availableLists := &map[string][]string{
"users": []string{"date-registered"},
"datasets": []string{"last-updated"},
}
var list []interface{}
switch kind {
case "users":
users, err := indexDB.GetUsers()
if err != nil {
pErr("error retrieving users: %v", err)
http.Error(w, "Error retrieving users.", http.StatusInternalServerError)
}
switch order {
case "date-registered":
sort.Sort(sort.Reverse(UsersByDateRegistered(users)))
}
for _, u := range users {
list = append(list, u)
}
case "datasets":
dsets, err := indexDB.GetDatasets()
if err != nil {
pErr("error retrieving datasets: %v", err)
http.Error(w, "Error retrieving users.", http.StatusInternalServerError)
}
switch order {
case "last-updated":
sort.Sort(sort.Reverse(DatasetsByLastUpdated(dsets)))
// case "downloads":
// sort.Sort(DByNumDownloads(dsets))
}
for _, d := range dsets {
list = append(list, d)
}
}
if len(list) == 0 {
pErr("list not found: %s/by-%s\n", kind, order)
http.NotFound(w, r)
return
}
webRenderPage(w, r, listTmplName, &WebPage{
Title: fmt.Sprintf("%s by %s", kind, order),
Description: fmt.Sprintf("List of %s ordered by %s", kind, order),
BodyPage: &ListWebPage{
List: list,
Kind: kind,
Order: order,
Lists: availableLists,
},
})
}
func webRenderPage(w http.ResponseWriter, r *http.Request,
tmpl string, p *WebPage) {
u := authenticatedUsername(r)
if len(u) > 0 {
p.LoggedIn = true
p.Username = u
}
err := webTmpl.ExecuteTemplate(w, tmpl, p)
if err != nil {
pErr("%v\n", err)
http.Error(w, "Error rendering page.", http.StatusInternalServerError)
}
}
func webConfigHandler(w http.ResponseWriter, r *http.Request) {
// esurl := data.ConfigGetString("db.elasticsearch", "http://localhost:9200/datadex")
config := map[string]interface{}{
// "ELASTICSEARCH_URL": esurl,
"ELASTICSEARCH_URL": "/search/datadex", // use proxy pass
}
out, err := json.Marshal(config)
if err != nil {
pErr("%v\n", err)
http.Error(w, "Server error.", http.StatusInternalServerError)
}
w.Write([]byte(fmt.Sprintf(webConfigScript, out)))
}
const webConfigScript = `(function() { this.datadex = this.datadex || {};
this.datadex.config = %s;
;}).call(this);
`