示例#1
0
func (this *SearchHandler) getHandler(w http.ResponseWriter, r *http.Request) {
	q := r.URL.Query()
	searchStr := q.Get("q") + "*"

	sInfo, err := readDir(this.D, q, searchStr)
	if err != nil {
		handlerutils.HttpError(w, err.Error(), http.StatusBadRequest)
		return
	}

	w.Header().Set("Cache-Control", fmt.Sprintf("private, max-age=%d", 20))
	w.Header().Set("Content-Type", "text/html")

	var b bytes.Buffer
	tTemplate := time.Now()
	err = templateloader.Templates.ExecuteTemplate(&b, "search.template-html", struct {
		Title string
		Names []string
	}{
		Title: searchStr,
		Names: sInfo.Names,
	})
	glog.V(2).Infof("PERF: template generation time: %v\n", time.Now().Sub(tTemplate))

	contents := b.Bytes()
	if handlerutils.EtagMatch(w, r, contents) {
		return
	}
	w.Write(contents)
}
示例#2
0
func returnWithContentType(w http.ResponseWriter, r *http.Request, content []byte,
	contentType string, timestamp int64, zip bool) {
	if handlerutils.EtagMatch(w, r, content) {
		return
	}
	if zip {
		w.Header().Set("Content-Encoding", "gzip")
	}
	setAge(w, timestamp)
	w.Header().Set("Content-Type", contentType)
	w.Header().Set("Cache-Control", fmt.Sprintf("private, max-age=%d", 20))
	w.Header().Set("Content-Length", strconv.Itoa(len(content)))
	t1 := time.Now()
	w.Write(content)
	glog.V(3).Infof("PERF: bytebuffer write time: %v\n", time.Now().Sub(t1))
}
示例#3
0
func (this *DirHandler) getHandler(w http.ResponseWriter, r *http.Request, s string) {
	q := r.URL.Query()
	sInfo, err := readDir(this.D, q, s)
	if err != nil {
		handlerutils.HttpError(w, err.Error(), http.StatusBadRequest)
		return
	}

	w.Header().Set("Cache-Control", fmt.Sprintf("private, max-age=%d", 20))

	var b bytes.Buffer
	w.Header().Set("Content-Type", "application/json")
	if err := json.NewEncoder(&b).Encode(sInfo); err != nil {
		handlerutils.HttpError(w, "An error occured during JSON marshalling.", http.StatusInternalServerError)
		return
	}

	contents := b.Bytes()
	if handlerutils.EtagMatch(w, r, contents) {
		return
	}
	w.Write(contents)
}