forked from web-platform-tests/wpt.fyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_components_handler.go
47 lines (40 loc) · 1.27 KB
/
dynamic_components_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
// Copyright 2019 The WPT Dashboard Project. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//go:generate packr2
package webapp
import (
"net/http"
"text/template"
"github.com/gobuffalo/packr/v2"
"github.com/web-platform-tests/wpt.fyi/shared"
)
var componentTemplates *template.Template
func init() {
box := packr.New("dynamic components", "./dynamic-components/templates/")
componentTemplates = template.New("all.js")
for _, t := range box.List() {
tmpl := componentTemplates.New(t)
body, err := box.FindString(t)
if err != nil {
panic(err)
} else if _, err = tmpl.Parse(body); err != nil {
panic(err)
}
}
}
func flagsComponentHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "text/javascript")
ctx := r.Context()
ds := shared.NewAppEngineDatastore(ctx, false)
flags, err := shared.GetFeatureFlags(ds)
if err != nil {
// Errors aren't a big deal; log them and ignore.
log := shared.GetLogger(ctx)
log.Errorf("Error loading flags: %s", err.Error())
}
data := struct{ Flags []shared.Flag }{flags}
if componentTemplates.ExecuteTemplate(w, "wpt-env-flags.js", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}