diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..252f74e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +*.6 +*.8 +*.o +*.so +*.cgo?.* +_cgo_* +_test* +*.out +_obj diff --git a/app.yaml b/app.yaml new file mode 100644 index 00000000..f8547e1f --- /dev/null +++ b/app.yaml @@ -0,0 +1,10 @@ +application: repostats +version: 1 +runtime: go +api_version: go1 + +default_expiration: "1d" + +handlers: +- url: /.* + script: _go_app diff --git a/repostats/page.html b/repostats/page.html new file mode 100644 index 00000000..5eeaf960 --- /dev/null +++ b/repostats/page.html @@ -0,0 +1,24 @@ +{{define "page"}} + + + +
+ +Account page: {{.Account}}
+ + + + +{{end}} diff --git a/repostats/repostats.go b/repostats/repostats.go new file mode 100644 index 00000000..edac962f --- /dev/null +++ b/repostats/repostats.go @@ -0,0 +1,51 @@ +package stats + +import ( + "appengine" + // "appengine/urlfetch" + "html/template" + "encoding/base64" + // "crypto/md5" + "net/http" + "strings" + "io" +) + +const base64GifPixel = "R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=" +var pageTemplate, _ = template.New("page").ParseFiles("repostats/page.html") + +func init() { + http.HandleFunc("/", handler) +} + +func handler(w http.ResponseWriter, r *http.Request) { + c := appengine.NewContext(r) + c.Infof("Requested URL: %v", r.URL) + + // / -> redirect + // /account -> account template + // /account/page -> GIF hit + params := strings.SplitN(strings.Trim(r.URL.Path,"/"), "/", 2) + + if len(params) == 0 { + http.Redirect(w, r, "https://github.com/igrigorik/repostats", http.StatusFound) + return + } + + if len(params) == 1 { + account := map[string]interface{}{"Account": params[0]} + if err := pageTemplate.ExecuteTemplate(w, "page", account); err != nil { + panic("Cannot execute template") + } + + } else { + w.Header().Set("Content-Type", "image/gif") + w.Header().Set("Cache-Control", "no-cache") + + output, _ := base64.StdEncoding.DecodeString(base64GifPixel) + io.WriteString(w, string(output)) + } + + c.Infof("Params size: %v, %v", len(params), params) + return +}