mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:14:01 +03:00
Add base stats
This commit is contained in:
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user