This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/stats/web.go

55 lines
1.2 KiB
Go
Raw Normal View History

2016-02-11 10:00:35 +00:00
package main
import (
"encoding/json"
"log"
"net/http"
)
func webStats(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json")
2016-02-11 10:00:35 +00:00
err := json.NewEncoder(rw).Encode(stats)
if err != nil {
log.Println("[webStats] JSON Encoding error: " + err.Error())
}
}
func webUsers(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "application/json")
2016-02-11 10:00:35 +00:00
err := json.NewEncoder(rw).Encode(users)
if err != nil {
log.Println("[webUsers] JSON Encoding error: " + err.Error())
}
}
const USAGE_THRESHOLD = 3
2016-02-13 22:53:07 +00:00
func webWords(rw http.ResponseWriter, req *http.Request) {
// Filter words under a certain usage
filtered := make(map[string]UserCount)
for word, usage := range words {
total := uint64(0)
for _, count := range usage {
total += count
}
if total < USAGE_THRESHOLD {
continue
}
filtered[word] = usage
}
rw.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(rw).Encode(filtered)
2016-02-13 22:53:07 +00:00
if err != nil {
log.Println("[webWords] JSON Encoding error: " + err.Error())
}
}
2016-02-11 10:00:35 +00:00
func startWebServer(bindAddr string) {
http.HandleFunc("/stats", webStats)
2016-02-13 22:53:41 +00:00
http.HandleFunc("/users", webUsers)
http.HandleFunc("/words", webWords)
2016-02-11 10:00:35 +00:00
http.ListenAndServe(bindAddr, nil)
}