mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 19:14:01 +03:00
FILE / ScuroNeko/mtg
stats/server.go
Исходный файл и его история в репозитории.
40 lines
763 B
Go
40 lines
763 B
Go
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)
|
|
}
|