Add base stats

This commit is contained in:
9seconds
2018-07-09 09:14:11 +03:00
parent 71614ee615
commit ff4be53d94
5 changed files with 131 additions and 2 deletions
+39
View File
@@ -0,0 +1,39 @@
package stats
import (
"encoding/json"
"net/http"
"sync"
"time"
"github.com/9seconds/mtg/config"
)
var instance *stats
func Start(conf *config.Config) {
instance = &stats{
URLs: conf.GetURLs(),
Uptime: uptime(time.Now()),
speedCurrent: &speed{},
mutex: &sync.RWMutex{},
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
instance.mutex.Lock()
first, _ := json.Marshal(instance)
instance.mutex.Unlock()
interm := map[string]interface{}{}
json.Unmarshal(first, &interm)
encoder := json.NewEncoder(w)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
encoder.Encode(interm)
})
http.ListenAndServe(conf.StatAddr(), nil)
}