9seconds 7 лет назад
Родитель
Сommit
045d417ece
1 измененных файлов: 17 добавлений и 3 удалений
  1. 17
    3
      stats/server.go

+ 17
- 3
stats/server.go Просмотреть файл

6
 	"sync"
6
 	"sync"
7
 	"time"
7
 	"time"
8
 
8
 
9
+	"go.uber.org/zap"
10
+
9
 	"github.com/9seconds/mtg/config"
11
 	"github.com/9seconds/mtg/config"
10
 )
12
 )
11
 
13
 
12
 var instance *stats
14
 var instance *stats
13
 
15
 
14
 func Start(conf *config.Config) {
16
 func Start(conf *config.Config) {
17
+	log := zap.S().Named("stats")
18
+
15
 	instance = &stats{
19
 	instance = &stats{
16
 		URLs:   conf.GetURLs(),
20
 		URLs:   conf.GetURLs(),
17
 		Uptime: uptime(time.Now()),
21
 		Uptime: uptime(time.Now()),
26
 		w.Header().Set("Content-Type", "application/json")
30
 		w.Header().Set("Content-Type", "application/json")
27
 
31
 
28
 		instance.mutex.Lock()
32
 		instance.mutex.Lock()
29
-		first, _ := json.Marshal(instance)
33
+		first, err := json.Marshal(instance)
30
 		instance.mutex.Unlock()
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
 		interm := map[string]interface{}{}
42
 		interm := map[string]interface{}{}
33
 		json.Unmarshal(first, &interm)
43
 		json.Unmarshal(first, &interm)
34
 
44
 
35
 		encoder := json.NewEncoder(w)
45
 		encoder := json.NewEncoder(w)
36
 		encoder.SetEscapeHTML(false)
46
 		encoder.SetEscapeHTML(false)
37
 		encoder.SetIndent("", "  ")
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
 }

Загрузка…
Отмена
Сохранить