diff --git a/stats/main.go b/stats/main.go index cb3df54..1d728b0 100644 --- a/stats/main.go +++ b/stats/main.go @@ -27,6 +27,7 @@ func process(broker *tg.Broker, update tg.APIMessage) { func main() { brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port") + webBind := flag.String("webserver", "localhost:7315", "Address to bind webserver to") boltdbFile := flag.String("boltdb", "stats.db", "BoltDB database file") chatID = flag.Int("chatid", -14625256, "Telegram Chat ID to count stats for") flag.Parse() @@ -39,6 +40,8 @@ func main() { loadUsers() loadStats() + go startWebServer(*webBind) + err = tg.CreateBrokerClient(*brokerAddr, process) assert(err) } diff --git a/stats/web.go b/stats/web.go new file mode 100644 index 0000000..d1cdf02 --- /dev/null +++ b/stats/web.go @@ -0,0 +1,27 @@ +package main + +import ( + "encoding/json" + "log" + "net/http" +) + +func webStats(rw http.ResponseWriter, req *http.Request) { + 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) { + err := json.NewEncoder(rw).Encode(users) + if err != nil { + log.Println("[webUsers] JSON Encoding error: " + err.Error()) + } +} + +func startWebServer(bindAddr string) { + http.HandleFunc("/stats", webStats) + http.HandleFunc("/users", webUsers) + http.ListenAndServe(bindAddr, nil) +}