Example #1
0
func (this *SrcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	tmaster := time.Now()
	switch r.Method { // Likely faster not to use a map[string]func.
	case "GET":
		if strings.Index(r.URL.Path, common.SrcsPath) != 0 {
			handlerutils.HttpError(w, "Bad path: "+r.URL.Path, http.StatusBadRequest)
			return
		}
		this.getHandler(w, r)
	case "POST":
		if strings.Index(r.URL.Path, common.SrcPath) != 0 {
			handlerutils.HttpError(w, "Bad path: "+r.URL.Path, http.StatusBadRequest)
			return
		}
		src := r.URL.Path[len(common.SrcPath):]
		this.postHandler(w, r, src)
	case "PUT":
		if strings.Index(r.URL.Path, common.SrcPath) != 0 {
			handlerutils.HttpError(w, "Bad path: "+r.URL.Path, http.StatusBadRequest)
			return
		}
		src := r.URL.Path[len(common.SrcPath):]
		this.putHandler(w, r, src)
	default:
		handlerutils.HttpError(w, "Bad method: "+r.Method, http.StatusBadRequest)
		return
	}

	glog.V(2).Infof("PERF: total service time: %v\n", time.Now().Sub(tmaster))
}
Example #2
0
func (this *SrcHandler) postHandler(w http.ResponseWriter, r *http.Request, src string) {
	glog.V(2).Infoln("src POST handler")
	inputPayload, err := getPayload(r)
	if err != nil {
		handlerutils.HttpError(w, err.Error(), http.StatusBadRequest)
		return
	}

	var rec db.WriteRecord

	if len(inputPayload) > 0 {
		err = json.Unmarshal(inputPayload, &rec)
		if err != nil {
			handlerutils.HttpError(w, "Malformed POST data.", http.StatusBadRequest)
			return
		}
	}

	if rec.RecordTimestamp == nil {
		timestamp := time.Now().UnixNano() / 1e6 // Millis.
		rec.RecordTimestamp = &timestamp
	}

	rowId, err := this.D.WriteRow(rec, src)
	if err != nil {
		handlerutils.HttpError(w, err.Error(), http.StatusBadRequest)
		return
	}
	fmt.Fprintf(w, `{"id":"%s"}`, rowId)
}
Example #3
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)
}
Example #4
0
func (this *FileHandler) getHandler(w http.ResponseWriter, r *http.Request, filePath string) {
	switch {
	case filePath == "":
		filePath = "tsviewdb.html"
	case filePath == "v":
		filePath = "viz.html"
	}

	if this.notModifiedSince(w, r) {
		return
	}

	sFile, ok := this.files[filePath]
	if !ok {
		handlerutils.HttpError(w, "Resource not found: "+filePath, http.StatusBadRequest)
		return
	}

	w.Header().Set("Content-Length", strconv.Itoa(len(sFile.Contents)))
	w.Header().Set("Content-Type", sFile.ContentType)
	handlerutils.SetGzipContentHeader(w, r)
	w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", 3600*24*7))
	w.Header().Set("Last-Modified", this.startTimeRFC1123)
	w.Write(sFile.Contents)
}
Example #5
0
func (this *RecordHandler) deleteHandler(w http.ResponseWriter, id string) {
	glog.V(3).Infof("Deleting id: %s", id)
	if err := this.D.DeleteRow(id); err != nil {
		handlerutils.HttpError(w, fmt.Sprintf("An error occured deleting id: %s", id),
			http.StatusInternalServerError)
		return
	}
}
Example #6
0
// HandleWithCache is called from a handler to either respond with a cached value
// or generate a new one.
func HandleWithCache(w http.ResponseWriter, r *http.Request, groupName, key string) {
	content, timestamp, err := getContentUsingCache(groupName, key)
	if err != nil {
		handlerutils.HttpError(w, err.Error(), http.StatusBadRequest)
		return
	}
	cInfo := contentInfos[groupName]
	returnWithContentType(w, r, content, cInfo.contentType, timestamp, cInfo.zip)
}
Example #7
0
func (this *DirHandler) deleteHandler(w http.ResponseWriter, src string) {
	glog.V(2).Infoln("search DELETE handler")
	path, file := common.GetSrcComponents(src)
	glog.V(2).Infof("Deleting path: %s, file:%s", path, file)
	if err := this.D.DeleteDir(path, file); err != nil {
		handlerutils.HttpError(w, fmt.Sprintf("An error occured deleting path: %s, file: %s", path, file),
			http.StatusInternalServerError)
		return
	}
}
Example #8
0
func (this *SearchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	tmaster := time.Now()

	switch r.Method {
	case "GET":
		this.getHandler(w, r)
	default:
		handlerutils.HttpError(w, "Bad method: "+r.Method, http.StatusBadRequest)
		return
	}

	glog.V(2).Infof("PERF: total service time: %v\n", time.Now().Sub(tmaster))
}
Example #9
0
func (this *SrcHandler) putHandler(w http.ResponseWriter, r *http.Request, src string) {
	glog.V(2).Infoln("src PUT handler")
	inputPayload, err := getPayload(r)
	if err != nil {
		handlerutils.HttpError(w, err.Error(), http.StatusBadRequest)
		return
	}

	var sInfo db.SourceInfoUncomp
	if len(inputPayload) > 0 {
		err = json.Unmarshal(inputPayload, &sInfo)
		if err != nil {
			handlerutils.HttpError(w, "Malformed PUT data.", http.StatusBadRequest)
			return
		}
	}

	if err = this.D.WriteDir(sInfo, src); err != nil {
		handlerutils.HttpError(w, err.Error(), http.StatusBadRequest)
		return
	}
}
Example #10
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)
}
Example #11
0
func (this *FileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	tmaster := time.Now()

	filePath := r.URL.Path[1:]
	glog.V(3).Infoln("filePath", filePath)

	switch r.Method {
	case "GET":
		this.getHandler(w, r, filePath)
	default:
		handlerutils.HttpError(w, "Bad method: "+r.Method, http.StatusBadRequest)
		return
	}

	glog.V(2).Infof("PERF: total service time: %v\n", time.Now().Sub(tmaster))
}
Example #12
0
func (this *DirHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	tmaster := time.Now()

	searchPath := r.URL.Path[len(common.DirPath):]
	glog.V(2).Infoln("searchPath", searchPath)

	switch r.Method {
	case "GET":
		this.getHandler(w, r, searchPath)
	case "DELETE":
		this.deleteHandler(w, searchPath)
	default:
		handlerutils.HttpError(w, "Bad method: "+r.Method, http.StatusBadRequest)
		return
	}

	glog.V(2).Infof("PERF: total service time: %v\n", time.Now().Sub(tmaster))
}
Example #13
0
func (this *SrcHandler) getHandler(w http.ResponseWriter, r *http.Request) {
	glog.V(2).Infoln("src GET handler")
	rawQuery := r.URL.RawQuery
	q, _ := url.ParseQuery(rawQuery)
	t := q.Get("type")
	if t == "" {
		t = "json"
	}
	switch t { // Likely faster not to use a map[string]func.
	case "png":
		cachinghandler.HandleWithCache(w, r, "srcs-png", rawQuery)
	case "inline-graph":
		cachinghandler.HandleWithCache(w, r, "srcs-inline-graph", rawQuery)
	case "json":
		cachinghandler.HandleWithCache(w, r, "srcs-json", rawQuery)
	default:
		handlerutils.HttpError(w, "Bad srcs 'type' parameter: "+t, http.StatusBadRequest)
	}
}
Example #14
0
func (this *RecordHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	tmaster := time.Now()

	id := r.URL.Path[len(common.RecordPath):]
	glog.V(2).Infoln("id", id)

	switch r.Method {
	case "GET":
		q := r.URL.Query()
		q.Add("id", id)
		newQS := q.Encode()
		this.getHandler(w, r, newQS)
	case "DELETE":
		this.deleteHandler(w, id)
	default:
		handlerutils.HttpError(w, "Bad method: "+r.Method, http.StatusBadRequest)
		return
	}

	glog.V(2).Infof("PERF: total service time: %v\n", time.Now().Sub(tmaster))
}