mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 23:24:01 +03:00
Add logging to stats
This commit is contained in:
+17
-3
@@ -6,12 +6,16 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/config"
|
"github.com/9seconds/mtg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
var instance *stats
|
var instance *stats
|
||||||
|
|
||||||
func Start(conf *config.Config) {
|
func Start(conf *config.Config) {
|
||||||
|
log := zap.S().Named("stats")
|
||||||
|
|
||||||
instance = &stats{
|
instance = &stats{
|
||||||
URLs: conf.GetURLs(),
|
URLs: conf.GetURLs(),
|
||||||
Uptime: uptime(time.Now()),
|
Uptime: uptime(time.Now()),
|
||||||
@@ -26,17 +30,27 @@ func Start(conf *config.Config) {
|
|||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
instance.mutex.Lock()
|
instance.mutex.Lock()
|
||||||
first, _ := json.Marshal(instance)
|
first, err := json.Marshal(instance)
|
||||||
instance.mutex.Unlock()
|
instance.mutex.Unlock()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Errorw("Cannot encode json", "error", err)
|
||||||
|
http.Error(w, "Internal server error", 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
interm := map[string]interface{}{}
|
interm := map[string]interface{}{}
|
||||||
json.Unmarshal(first, &interm)
|
json.Unmarshal(first, &interm)
|
||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
encoder.SetEscapeHTML(false)
|
encoder.SetEscapeHTML(false)
|
||||||
encoder.SetIndent("", " ")
|
encoder.SetIndent("", " ")
|
||||||
encoder.Encode(interm)
|
if err = encoder.Encode(interm); err != nil {
|
||||||
|
log.Errorw("Cannot encode json", "error", err)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
http.ListenAndServe(conf.StatAddr(), nil)
|
if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
|
||||||
|
log.Fatalw("Stats server has been stopped", "error", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user