forked from web-platform-tests/wpt.fyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin_handler.go
126 lines (112 loc) · 3.69 KB
/
admin_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
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
// Copyright 2018 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.
package webapp
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/web-platform-tests/wpt.fyi/shared"
)
func checkAdmin(acl shared.GitHubAccessControl, log shared.Logger, w http.ResponseWriter) bool {
if acl == nil {
http.Error(w, "Log in from the homepage first", http.StatusUnauthorized)
return false
}
admin, err := acl.IsValidAdmin()
if err != nil {
log.Errorf("Error checking admin: %s", err.Error())
http.Error(w, "Error checking admin", http.StatusInternalServerError)
return false
}
if !admin {
http.Error(w, "Admin only", http.StatusForbidden)
return false
}
return true
}
func adminUploadHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
a := shared.NewAppEngineAPI(ctx)
ds := shared.NewAppEngineDatastore(ctx, false)
log := shared.GetLogger(ctx)
acl, err := shared.NewGitHubAccessControlFromRequest(a, ds, r)
if err != nil {
log.Errorf("Error creating GitHubAccessControl: %s", err.Error())
http.Error(w, "Error creating GitHubAccessControl", http.StatusInternalServerError)
return
}
showAdminUploadForm(a, acl, log, w)
}
func showAdminUploadForm(a shared.AppEngineAPI, acl shared.GitHubAccessControl, log shared.Logger, w http.ResponseWriter) {
if !checkAdmin(acl, log, w) {
return
}
data := struct {
CallbackURL string
}{
CallbackURL: fmt.Sprintf("https://%s/api/results/create", a.GetVersionedHostname()),
}
// We don't need user info in this template.
RenderTemplate(w, nil, "admin_upload.html", data)
}
func adminFlagsHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
a := shared.NewAppEngineAPI(ctx)
ds := shared.NewAppEngineDatastore(ctx, false)
log := shared.GetLogger(ctx)
acl, err := shared.NewGitHubAccessControlFromRequest(a, ds, r)
if err != nil {
log.Errorf("Error creating GitHubAccessControl: %s", err.Error())
http.Error(w, "Error creating GitHubAccessControl", http.StatusInternalServerError)
return
}
handleAdminFlags(a, ds, acl, log, w, r)
}
func handleAdminFlags(a shared.AppEngineAPI, ds shared.Datastore, acl shared.GitHubAccessControl, log shared.Logger, w http.ResponseWriter, r *http.Request) {
if !checkAdmin(acl, log, w) {
return
}
if r.Method == "GET" {
data := struct {
Host string
}{
Host: a.GetHostname(),
}
// We don't need user info in this template.
RenderTemplate(w, nil, "admin_flags.html", data)
} else if r.Method == "POST" {
var flag shared.Flag
if bytes, err := ioutil.ReadAll(r.Body); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if err = json.Unmarshal(bytes, &flag); err != nil {
http.Error(w, fmt.Sprintf("Failed to unmarshal flag: %s", err.Error()), http.StatusBadRequest)
return
} else if err = shared.SetFeature(ds, flag); err != nil {
http.Error(w, fmt.Sprintf("Failed to save feature %s: %s", flag.Name, err.Error()), http.StatusInternalServerError)
return
}
}
}
func adminCacheFlushHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
a := shared.NewAppEngineAPI(ctx)
ds := shared.NewAppEngineDatastore(ctx, false)
log := shared.GetLogger(ctx)
acl, err := shared.NewGitHubAccessControlFromRequest(a, ds, r)
if err != nil {
log.Errorf("Error creating GitHubAccessControl: %s", err.Error())
http.Error(w, "Error creating GitHubAccessControl", http.StatusInternalServerError)
return
}
if !checkAdmin(acl, log, w) {
return
}
if err := shared.FlushCache(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.Write([]byte("Successfully flushed cache"))
}
}