stats: Added webserver

This commit is contained in:
Hamcha 2016-02-11 10:00:35 +00:00
parent 0e441c8048
commit e800a46c5f
2 changed files with 30 additions and 0 deletions

View file

@ -27,6 +27,7 @@ func process(broker *tg.Broker, update tg.APIMessage) {
func main() { func main() {
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port") 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") boltdbFile := flag.String("boltdb", "stats.db", "BoltDB database file")
chatID = flag.Int("chatid", -14625256, "Telegram Chat ID to count stats for") chatID = flag.Int("chatid", -14625256, "Telegram Chat ID to count stats for")
flag.Parse() flag.Parse()
@ -39,6 +40,8 @@ func main() {
loadUsers() loadUsers()
loadStats() loadStats()
go startWebServer(*webBind)
err = tg.CreateBrokerClient(*brokerAddr, process) err = tg.CreateBrokerClient(*brokerAddr, process)
assert(err) assert(err)
} }

27
stats/web.go Normal file
View file

@ -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)
}