Commits

Miki Tebeka committed 7ef2bce

task

Comments (0)

Files changed (1)

shortie/shortie.go

 	"appengine"
 	"appengine/datastore"
 	"appengine/user"
+	"appengine/taskqueue"
 	"fmt"
 	"http"
 	"log"
 	counterKeyName = "counter-key-name"
 	counterKind    = "Counter"
 	urlKind        = "Url"
+	hitWorkerPath  = "/_worker/hit"
 )
 
 func base62Encode(num uint64) string {
 var homeTemplate *template.Template
 
 func init() {
-	var fmap = template.FuncMap {
-		"strftime" : timeFormatter,
+	var fmap = template.FuncMap{
+		"strftime": timeFormatter,
 	}
 	homeTemplate = template.Must(template.New("home").Funcs(fmap).Parse(homeHTML))
+	http.HandleFunc(hitWorkerPath, hitHandler)
 	http.HandleFunc("/", rootHandler)
 }
 
 		return
 	}
 
-	incHits(ctx, path)
+	task := taskqueue.NewPOSTTask(hitWorkerPath, map[string][]string{"path": {path}})
+	if _, err := taskqueue.Add(ctx, task, ""); err != nil {
+		return
+	}
+
 	http.Redirect(w, r, url.Long, http.StatusTemporaryRedirect)
 }
 
 	t := time.NanosecondsToLocalTime(nsec)
 	return t.Format("2006-01-02 15:04")
 }
+
+func hitHandler(w http.ResponseWriter, r *http.Request) {
+	ctx := appengine.NewContext(r)
+	path := r.FormValue("path")
+	if len(path) == 0 {
+		log.Println("Empty value for hit handler")
+		return
+	}
+	incHits(ctx, path)
+}