func SendStatsJSON(w http.ResponseWriter, r *http.Request) { var sJSON []byte imgs, err := GetImgs() if err != nil { log.Fatal(err) } if len(imgs) > 0 { s := stats.CalcAverages(imgs) sJSON, err = json.Marshal(s) if err != nil { log.Fatal(err) } } else { var s stats.Stats sJSON, err = json.Marshal(s) if err != nil { log.Fatal(err) } } w.Header().Set("Content-Type", "application/json") w.Write(sJSON) }
func SendStats(w http.ResponseWriter, r *http.Request) { data := make(map[string]interface{}) data["ImgCount"] = 0 data["AvgWidth"] = 0 data["AvgHeight"] = 0 imgs, err := GetImgs() if err != nil { log.Fatal(err) } if len(imgs) > 0 { s := stats.CalcAverages(imgs) data["ImgCount"] = s.ImgCount data["AvgWidth"] = s.AvgWidth data["AvgHeight"] = s.AvgHeight } t, err := template.New("stats").Parse(PageTemplate) if err != nil { log.Fatal(err) } t, err = t.Parse(HeadTemplate) if err != nil { log.Fatal(err) } t, err = t.Parse(BodyTemplate) if err != nil { log.Fatal(err) } t, err = t.Parse(StatsTemplate) if err != nil { log.Fatal(err) } t.Execute(w, data) }