stats: Added webserver
This commit is contained in:
parent
0e441c8048
commit
e800a46c5f
2 changed files with 30 additions and 0 deletions
|
@ -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)
|
||||
}
|
||||
|
|
27
stats/web.go
Normal file
27
stats/web.go
Normal 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)
|
||||
}
|
Reference in a new issue