forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
89 lines (73 loc) · 1.76 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"encoding/json"
"fmt"
"github.com/coreos/etcd/web"
"io"
"log"
"net/http"
"os"
)
//--------------------------------------
// Web Helper
//--------------------------------------
var storeMsg chan string
// Help to send msg from store to webHub
func webHelper() {
storeMsg = make(chan string)
etcdStore.SetMessager(storeMsg)
for {
// transfer the new msg to webHub
web.Hub().Send(<-storeMsg)
}
}
//--------------------------------------
// HTTP Utilities
//--------------------------------------
func decodeJsonRequest(req *http.Request, data interface{}) error {
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&data); err != nil && err != io.EOF {
warnf("Malformed json request: %v", err)
return fmt.Errorf("Malformed json request: %v", err)
}
return nil
}
func encodeJsonResponse(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if data != nil {
encoder := json.NewEncoder(w)
encoder.Encode(data)
}
}
//--------------------------------------
// Log
//--------------------------------------
var logger *log.Logger
func init() {
logger = log.New(os.Stdout, "[etcd] ", log.Lmicroseconds)
}
func debugf(msg string, v ...interface{}) {
if verbose {
logger.Printf("DEBUG "+msg+"\n", v...)
}
}
func debug(v ...interface{}) {
if verbose {
logger.Println("DEBUG " + fmt.Sprint(v...))
}
}
func warnf(msg string, v ...interface{}) {
logger.Printf("WARN "+msg+"\n", v...)
}
func warn(v ...interface{}) {
logger.Println("WARN " + fmt.Sprint(v...))
}
func fatalf(msg string, v ...interface{}) {
logger.Printf("FATAL "+msg+"\n", v...)
os.Exit(1)
}
func fatal(v ...interface{}) {
logger.Println("FATAL " + fmt.Sprint(v...))
os.Exit(1)
}