Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Unified Diff: doc/appengine/main.go

Issue 1393353002: Add app engine app to mirror html docs from chromium.googlesource.com (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« doc/appengine/app.yaml ('K') | « doc/appengine/favicon.ico ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: doc/appengine/main.go
diff --git a/doc/appengine/main.go b/doc/appengine/main.go
new file mode 100755
index 0000000000000000000000000000000000000000..8086ba69fce0d6f6329c3c48bee83d5cfafceafa
--- /dev/null
+++ b/doc/appengine/main.go
@@ -0,0 +1,117 @@
+// Copyright 2015 The Crashpad 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 crashpad mirrors crashpad documentation from Chromium’s git repo.
+package crashpad
+
+import (
+ "encoding/base64"
+ "fmt"
+ "io"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+ "path"
+ "strings"
+ "time"
+
+ "google.golang.org/appengine"
+ "google.golang.org/appengine/memcache"
+ "google.golang.org/appengine/urlfetch"
+)
+
+const baseURL = "https://chromium.googlesource.com/crashpad/crashpad/+/doc/doc/generated/?format=TEXT"
+
+func init() {
+ http.HandleFunc("/", handler)
+}
+
+func handler(w http.ResponseWriter, r *http.Request) {
+ ctx := appengine.NewContext(r)
+ client := urlfetch.Client(ctx)
+
+ // Don’t show dotfiles.
+ if strings.HasPrefix(path.Base(r.URL.Path), ".") {
+ http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+ return
+ }
+
+ u, err := url.Parse(baseURL)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ // Redirect directories to their index pages (/doc/ -> /doc/index.html).
+ if strings.HasSuffix(r.URL.Path, "/") {
+ http.Redirect(w, r, r.URL.Path+"index.html", http.StatusFound)
+ return
+ }
+
+ u.Path = path.Join(u.Path, r.URL.Path)
+ urlStr := u.String()
+
+ item, err := memcache.Get(ctx, urlStr)
+ if err == memcache.ErrCacheMiss {
+ resp, err := client.Get(urlStr)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ w.Header().Set("Content-Type", "text/html")
Mark Mentovai 2015/10/08 21:46:16 If you’re going to copy the response: carry the st
Bons 2015/10/08 22:31:16 Done.
+ io.Copy(w, resp.Body)
+ return
+ }
+ decoder := base64.NewDecoder(base64.StdEncoding, resp.Body)
+ b, err := ioutil.ReadAll(decoder)
+ if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ item = &memcache.Item{
+ Key: urlStr,
+ Value: b,
+ Expiration: 24 * time.Hour,
Mark Mentovai 2015/10/08 21:46:17 Maybe that’s a little long, is one hour OK? Robert
Bons 2015/10/08 22:31:17 Done.
+ }
+ if err := memcache.Set(ctx, item); err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+ } else if err != nil {
+ http.Error(w, err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", contentType(path.Base(u.Path)))
Mark Mentovai 2015/10/08 21:46:16 Sensible to set Content-Length too, since I think
Bons 2015/10/08 22:31:16 that’s done automatically.
+ fmt.Fprintf(w, "%s", item.Value)
+}
+
+var contentTypes = map[string]string{
Mark Mentovai 2015/10/08 21:46:16 I don’t know anything about Go, but should this be
Bons 2015/10/08 22:31:17 Yeah. Moved.
+ ".html": "text/html",
+ ".css": "text/css",
+ ".js": "text/javascript",
+ ".png": "image/png",
+}
+
+// contentType returns the appropriate content type header for file.
+func contentType(file string) string {
+ for suffix, typ := range contentTypes {
+ if strings.HasSuffix(file, suffix) {
+ return typ + "; charset=UTF-8"
Mark Mentovai 2015/10/08 21:46:16 charset doesn’t make sense for png.
Bons 2015/10/08 22:31:17 Done.
+ }
+ }
+ return "text/plain; charset=UTF-8"
+}
« doc/appengine/app.yaml ('K') | « doc/appengine/favicon.ico ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698