forked from etcd-io/etcd
/
util.go
146 lines (119 loc) · 2.85 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"fmt"
"github.com/coreos/etcd/web"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strconv"
"time"
)
//--------------------------------------
// etcd http Helper
//--------------------------------------
// Convert string duration to time format
func durationToExpireTime(strDuration string) (time.Time, error) {
if strDuration != "" {
duration, err := strconv.Atoi(strDuration)
if err != nil {
return time.Unix(0, 0), err
}
return time.Now().Add(time.Second * (time.Duration)(duration)), nil
} else {
return time.Unix(0, 0), nil
}
}
//--------------------------------------
// 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)
}
}
// sanitizeURL will cleanup a host string in the format hostname:port and
// attach a schema.
func sanitizeURL(host string, defaultScheme string) string {
// Blank URLs are fine input, just return it
if len(host) == 0 {
return host
}
p, err := url.Parse(host)
if err != nil {
fatal(err)
}
// Make sure the host is in Host:Port format
_, _, err = net.SplitHostPort(host)
if err != nil {
fatal(err)
}
p = &url.URL{Host: host, Scheme: defaultScheme}
return p.String()
}
func check(err error) {
if err != nil {
fatal(err)
}
}
//--------------------------------------
// Log
//--------------------------------------
var logger *log.Logger
func init() {
logger = log.New(os.Stdout, "[etcd] ", log.Lmicroseconds)
}
func infof(msg string, v ...interface{}) {
logger.Printf("INFO "+msg+"\n", v...)
}
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)
}