2016-02-11 10:00:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func webStats(rw http.ResponseWriter, req *http.Request) {
|
2016-02-14 09:45:45 +00:00
|
|
|
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) {
|
2016-02-14 09:45:45 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 09:45:45 +00:00
|
|
|
const USAGE_THRESHOLD = 3
|
|
|
|
|
2016-02-13 22:53:07 +00:00
|
|
|
func webWords(rw http.ResponseWriter, req *http.Request) {
|
2016-02-14 09:45:45 +00:00
|
|
|
// Filter words under a certain usage
|
|
|
|
filtered := make(map[string]UserCount)
|
|
|
|
for word, usage := range words {
|
|
|
|
total := 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)
|
|
|
|
}
|