Example #1
0
func handler(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/find" {
		http.Error(w, "request does not match /find", http.StatusNotFound)
		return
	}
	values := r.URL.Query()
	query := values.Get("q")
	if query == "" {
		http.Error(w, "q is empty", http.StatusBadRequest)
		return
	}

	c := appengine.NewContext(r)
	b, err := aecache.Get(c, "find:"+query)
	if err != nil {
		b, err = find(c, query)
		if err != nil {
			c.Errorf("find: %v", err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		go aecache.Set(c, "find:"+query, b, 7*24*time.Hour)
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(b)
}
Example #2
0
func handler(w http.ResponseWriter, r *http.Request) {
	m := titleRE.FindStringSubmatch(r.URL.Path)
	if m == nil {
		http.Error(w, fmt.Sprintf("request does not match %s", titleRE), http.StatusNotFound)
		return
	}
	id := m[1]

	c := appengine.NewContext(r)
	b, err := aecache.Get(c, "title:"+id)
	if err != nil {
		b, err = title(c, id)
		if err != nil {
			c.Errorf("title: %v", err)
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		go aecache.Set(c, "title:"+id, b, 24*time.Hour)
	}

	w.Header().Set("Content-Type", "application/json")
	w.Write(b)
}