Пример #1
0
func result(w http.ResponseWriter, r *http.Request) {
	w.Header().Add("content-type", "application/json;charset=utf8")
	decoder := json.NewEncoder(w)
	if p := strings.TrimPrefix(r.URL.Path, "/result/"); len(p) < len(r.URL.Path) {
		if file, err := os.OpenFile("./tests/"+p, os.O_RDONLY, 0666); err != nil {
			log.Println("Can't read", p, err)
			http.NotFound(w, r)
		} else {
			defer file.Close()
			r := bufio.NewReader(file)
			result := make([]ChartData, 0)
			starttime := start_time(p)
			for line, err := r.ReadString('\n'); err == nil; line, err = r.ReadString('\n') {
				valtime := strings.Split(strings.TrimSuffix(line, "\n"), " ")
				if len(valtime) > 1 {
					if time, converterr := strconv.ParseUint(valtime[1], 10, 64); converterr == nil {
						if val, converterr := strconv.ParseFloat(valtime[0], 64); converterr == nil {
							result = append(result, ChartData{time - starttime, val})
						}
					}
				}
			}
			decoder.Encode(result)
		}
	} else {
		http.NotFound(w, r)
	}
}