This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 506
/
serve.go
378 lines (347 loc) · 11.5 KB
/
serve.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package playground
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/memcache"
"google.golang.org/appengine/taskqueue"
"google.golang.org/appengine/urlfetch"
)
const (
COMPONENTS_URL = "https://api.github.com/repos/ampproject/amphtml/git/trees/master?recursive=1"
COMPONENTS_MEMCACHE_KEY = "amp-components-list"
COMPONENTS_UPDATE_FREQ_SECONDS = 86400 // one day
PLAYGROUND_PATH_PREFIX = "/playground"
)
var componentRegex = regexp.MustCompile("extensions/(amp-[^/]+)/([0-9]+.[0-9]+)$")
var validRequestUrlOrigins map[string]bool
var instanceStartup = int(time.Now().Unix())
type GitHubBlob struct {
Path string `json:"path"`
Mode string `json:"mode"`
BlobType string `json:"type"`
Sha string `json:"sha"`
Size int `json:"size"`
Url string `json:"url"`
}
type GitHubApiResponse struct {
Sha string `json:"sha"`
Url string `json:"url"`
Tree []GitHubBlob `json:"tree"`
Truncated bool `json:"truncated"`
}
// An Auth key for using GitHub API should be generated and placed in Datastore
// on App Engine, with the Datastore key of "GitHubApiTokenKey". This ensures
// that requests to the GitHub API are not subject to the severe rate limiting
// imposed on unauthenticated requests, stopping the components map being built.
type GitHubApiToken struct {
AuthKey string
}
type AmpComponentsList struct {
Timestamp int
Components []byte
}
type ComponentsReqError struct {
Message string
Code int
}
func InitPlayground() {
http.HandleFunc(PLAYGROUND_PATH_PREFIX+"/fetch", handler)
http.HandleFunc(PLAYGROUND_PATH_PREFIX+"/amp-component-versions", components)
http.HandleFunc(PLAYGROUND_PATH_PREFIX+"/amp-component-versions-task", componentsTask)
validRequestUrlOrigins = map[string]bool{
"ampbyexample.com": true,
"ampstart.com": true,
"ampstart-staging.firebaseapp.com": true,
"localhost:8080": true,
"amp-by-example-staging.appspot.com": true,
"amp-by-example-sebastian.appspot.com": true,
"0.1.0.1": true,
}
}
func InitializeComponents(r *http.Request) {
getComponentsAndUpdateIfStale(r)
}
func getGitHubApiToken(ctx context.Context) (string, error) {
var apiToken GitHubApiToken
key := datastore.NewKey(ctx, "GitHubApiToken", "GitHubApiTokenKey", 0, nil)
err := datastore.Get(ctx, key, &apiToken)
if err != nil {
log.Warningf(ctx, "Error retrieving GitHub key from datastore")
return "", err
}
return apiToken.AuthKey, nil
}
func components(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "only GET request supported", http.StatusBadRequest)
return
}
if r.Header.Get("x-requested-by") != "playground" {
http.Error(w, "x-requested-by invalid", http.StatusBadRequest)
return
}
components, err := getComponentsAndUpdateIfStale(r)
if err != nil {
http.Error(w, err.Message, err.Code)
return
}
w.Header().Set("Content-type", "application/json")
w.Write(components.Components)
}
func getComponentsAndUpdateIfStale(r *http.Request) (*AmpComponentsList, *ComponentsReqError) {
curTime := int(time.Now().Unix())
ctx := appengine.NewContext(r)
latest, err := fetchComponentsFromMemCache(ctx)
// latest could be nil if not in memcache or datastore.
var timestamp int
if latest != nil {
timestamp = latest.Timestamp
}
if curTime-timestamp > COMPONENTS_UPDATE_FREQ_SECONDS {
log.Infof(ctx, "Components map is stale, requesting update")
createTaskQueueUpdate(ctx, timestamp)
}
return latest, err
}
func createTaskQueueUpdate(ctx context.Context, timestamp int) {
t := taskqueue.NewPOSTTask(PLAYGROUND_PATH_PREFIX+"/amp-component-versions-task",
url.Values{})
// Setting the name explicitly means that only one task will ever
// be in the queue, even if attempted by separate instances.
// The name chosen includes the timestamp of the last known
// components list, which means that only one update task can be
// created for each version known.
// Where the timestamp is zero, a different value must be used as zero
// would remain in the dedupe list for 9 days. The startup time of the
// instance is used instead
n := timestamp
if timestamp == 0 {
n = instanceStartup
}
t.Name = fmt.Sprintf("amp-components-list-last-known-%d", n)
minBackoff, _ := time.ParseDuration("20s")
t.RetryOptions = &taskqueue.RetryOptions{
RetryLimit: 5,
MinBackoff: minBackoff,
MaxDoublings: 3,
}
log.Infof(ctx, "Adding to taskqueue: %s", t.Name)
taskqueue.Add(ctx, t, "")
}
func componentsTask(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
_, err := fetchAndUpdateComponents(ctx)
if err != nil {
log.Warningf(ctx, "Marking task for retry, if retries remaining")
// Error code of 500 ensures task is marked for retry.
http.Error(w, "Server error", http.StatusInternalServerError)
return
}
w.Write([]byte("Updated components"))
}
func fetchAndUpdateComponents(ctx context.Context) ([]byte, *ComponentsReqError) {
log.Infof(ctx, "Fetching components list from GitHub")
components, err := fetchComponents(ctx)
if err != nil {
log.Warningf(ctx, "Failed to fetch components map from GitHub: %v, %v", err.Code, err.Message)
return nil, err
}
addComponentsToStores(ctx, components)
return components, nil
}
func fetchComponentsFromDataStore(ctx context.Context) (*AmpComponentsList, *ComponentsReqError) {
log.Infof(ctx, "Retrieving components from datastore")
var components AmpComponentsList
key := datastore.NewKey(ctx, "AmpComponentsList", "ComponentsListKey", 0, nil)
err := datastore.Get(ctx, key, &components)
if err != nil {
log.Warningf(ctx, "Error retrieving components from datastore: %v", err)
rawComponents, compErr := fetchAndUpdateComponents(ctx)
if compErr != nil {
return nil, compErr
}
components.Components = rawComponents
components.Timestamp = int(time.Now().Unix())
}
return &components, nil
}
func fetchComponentsFromMemCache(ctx context.Context) (*AmpComponentsList, *ComponentsReqError) {
log.Infof(ctx, "Retrieving components from memcache")
var components AmpComponentsList
_, err := memcache.Gob.Get(ctx, COMPONENTS_MEMCACHE_KEY, &components)
if err == nil {
return &components, nil
}
if err == memcache.ErrCacheMiss {
log.Infof(ctx, "Components not in memcache, retrieving from datastore")
} else if err != nil {
log.Errorf(ctx, "Error when retrieving components from memcache: %v", err)
}
dsComponents, compErr := fetchComponentsFromDataStore(ctx)
if compErr == nil {
log.Infof(ctx, "Setting datastore components value to memcache")
memcacheItem := &memcache.Item{
Key: COMPONENTS_MEMCACHE_KEY,
Object: dsComponents,
}
memcache.Gob.Set(ctx, memcacheItem)
return dsComponents, nil
}
return nil, compErr
}
func addComponentsToStores(ctx context.Context, rawComponents []byte) {
components := &AmpComponentsList{
Components: rawComponents,
Timestamp: int(time.Now().Unix()),
}
key := datastore.NewKey(ctx, "AmpComponentsList", "ComponentsListKey", 0, nil)
log.Infof(ctx, "Adding components to datastore")
_, err := datastore.Put(ctx, key, components)
if err == nil {
log.Infof(ctx, "Adding components to memcache")
item := &memcache.Item{
Key: COMPONENTS_MEMCACHE_KEY,
Object: components,
}
err = memcache.Gob.Set(ctx, item)
if err != nil {
log.Warningf(ctx, "Error adding components to memcache: %v", err)
}
} else {
log.Warningf(ctx, "Error adding components to datastore: %v", err)
}
}
func extractComponents(g *GitHubApiResponse) ([]byte, error) {
vers := make(map[string]string)
tree := g.Tree
for _, blob := range tree {
parseAndAddComponent(blob, vers)
}
return json.Marshal(vers)
}
func parseAndAddComponent(blob GitHubBlob, componentsWithVersion map[string]string) {
groups := componentRegex.FindStringSubmatch(blob.Path)
if len(groups) != 3 {
return
}
component := groups[1]
if strings.HasSuffix(component, "-impl") {
return
}
ver := groups[2]
elem, ok := componentsWithVersion[component]
if !ok || ver > elem {
componentsWithVersion[component] = ver
}
}
func fetchComponents(ctx context.Context) ([]byte, *ComponentsReqError) {
log.Infof(ctx, "Fetching components from GitHub")
authKey, err := getGitHubApiToken(ctx)
url := COMPONENTS_URL
if err == nil {
url = url + "&access_token=" + authKey
} else {
log.Warningf(ctx, "Using unauthenticated request to GitHub API")
}
client := urlfetch.Client(ctx)
req, err := http.NewRequest("GET", url, nil)
resp, err := client.Do(req)
if err != nil {
return nil, &ComponentsReqError{"Error in request to GitHub", http.StatusInternalServerError}
} else if resp.StatusCode != http.StatusOK {
return nil, &ComponentsReqError{"Error in request to GitHub", resp.StatusCode}
}
defer resp.Body.Close()
g := new(GitHubApiResponse)
err = json.NewDecoder(resp.Body).Decode(&g)
if err != nil {
return nil, &ComponentsReqError{"Error decoding JSON response", http.StatusInternalServerError}
}
c, err := extractComponents(g)
if err != nil {
return nil, &ComponentsReqError{"Error extracting components", http.StatusInternalServerError}
}
return c, nil
}
func handler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "only GET request supported", http.StatusBadRequest)
return
}
if r.Header.Get("x-requested-by") != "playground" {
http.Error(w, "x-requested-by invalid", http.StatusBadRequest)
return
}
param, _ := r.URL.Query()["url"]
if len(param) <= 0 {
http.Error(w, "No URL provided via 'url' query parameter", http.StatusBadRequest)
return
}
u, err := url.Parse(param[0])
if err != nil || (u.Scheme != "http" && u.Scheme != "https") {
http.Error(w, "Invalid URL scheme", http.StatusBadRequest)
return
}
// only allow URLs from trusted domains
if !validRequestUrlOrigins[u.Host] {
http.Error(w, "Untrusted origin", http.StatusBadRequest)
return
}
ctx := appengine.NewContext(r)
client := urlfetch.Client(ctx)
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
http.Error(w, fmt.Sprintf("Bad gateway (%v)", err.Error()),
http.StatusBadGateway)
return
}
req.Header.Add("User-Agent",
"Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MTC19V) "+
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.81 Mobile "+
"Safari/537.36 (compatible; validator.ampproject.org)")
resp, err := client.Do(req)
if err != nil {
http.Error(w, fmt.Sprintf("Bad gateway (%v)", err.Error()),
http.StatusBadGateway)
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Bad gateway (%v)", err.Error()),
http.StatusBadGateway)
return
}
if err != nil {
http.Error(w, fmt.Sprintf("Problem formatting json (%v)",
err.Error()),
http.StatusInternalServerError)
}
w.Header().Set("Content-type", "application/json")
w.Write(data)
}