|
|
@@ -6,12 +6,16 @@ import (
|
|
6
|
6
|
"sync"
|
|
7
|
7
|
"time"
|
|
8
|
8
|
|
|
|
9
|
+ "go.uber.org/zap"
|
|
|
10
|
+
|
|
9
|
11
|
"github.com/9seconds/mtg/config"
|
|
10
|
12
|
)
|
|
11
|
13
|
|
|
12
|
14
|
var instance *stats
|
|
13
|
15
|
|
|
14
|
16
|
func Start(conf *config.Config) {
|
|
|
17
|
+ log := zap.S().Named("stats")
|
|
|
18
|
+
|
|
15
|
19
|
instance = &stats{
|
|
16
|
20
|
URLs: conf.GetURLs(),
|
|
17
|
21
|
Uptime: uptime(time.Now()),
|
|
|
@@ -26,17 +30,27 @@ func Start(conf *config.Config) {
|
|
26
|
30
|
w.Header().Set("Content-Type", "application/json")
|
|
27
|
31
|
|
|
28
|
32
|
instance.mutex.Lock()
|
|
29
|
|
- first, _ := json.Marshal(instance)
|
|
|
33
|
+ first, err := json.Marshal(instance)
|
|
30
|
34
|
instance.mutex.Unlock()
|
|
31
|
35
|
|
|
|
36
|
+ if err != nil {
|
|
|
37
|
+ log.Errorw("Cannot encode json", "error", err)
|
|
|
38
|
+ http.Error(w, "Internal server error", 500)
|
|
|
39
|
+ return
|
|
|
40
|
+ }
|
|
|
41
|
+
|
|
32
|
42
|
interm := map[string]interface{}{}
|
|
33
|
43
|
json.Unmarshal(first, &interm)
|
|
34
|
44
|
|
|
35
|
45
|
encoder := json.NewEncoder(w)
|
|
36
|
46
|
encoder.SetEscapeHTML(false)
|
|
37
|
47
|
encoder.SetIndent("", " ")
|
|
38
|
|
- encoder.Encode(interm)
|
|
|
48
|
+ if err = encoder.Encode(interm); err != nil {
|
|
|
49
|
+ log.Errorw("Cannot encode json", "error", err)
|
|
|
50
|
+ }
|
|
39
|
51
|
})
|
|
40
|
52
|
|
|
41
|
|
- http.ListenAndServe(conf.StatAddr(), nil)
|
|
|
53
|
+ if err := http.ListenAndServe(conf.StatAddr(), nil); err != nil {
|
|
|
54
|
+ log.Fatalw("Stats server has been stopped", "error", err)
|
|
|
55
|
+ }
|
|
42
|
56
|
}
|