forked from hanguofeng/taiji
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.go
102 lines (78 loc) · 1.81 KB
/
utils.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
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"github.com/golang/glog"
)
func getGroupName(url string) string {
m := md5.New()
m.Write([]byte(url))
s := hex.EncodeToString(m.Sum(nil))
return s
}
func copyOffsetMap(offsetMap OffsetMap) OffsetMap {
result := make(OffsetMap)
for topic, partitionOffsets := range offsetMap {
resultOffsets := make(map[int32]int64)
for partition, offset := range partitionOffsets {
resultOffsets[partition] = offset
}
result[topic] = resultOffsets
}
return result
}
func stringKeyOffsetMap(offsetMap OffsetMap) map[string]map[string]int64 {
result := make(map[string]map[string]int64)
for topic, partitionOffsets := range offsetMap {
resultOffsets := make(map[string]int64)
for partition, offset := range partitionOffsets {
resultOffsets[strconv.FormatInt(int64(partition), 10)] = offset
}
result[topic] = resultOffsets
}
return result
}
func jsonify(w http.ResponseWriter, r *http.Request, data interface{}, code int, args ...interface{}) {
var msg string
if len(args) > 0 {
msg = fmt.Sprint(args...)
} else {
msg = "success"
}
res := map[string]interface{}{
"errno": code,
"errmsg": msg,
"data": data,
}
prettyPrint := false
r.ParseForm()
if r.Form["pretty"] != nil && len(r.Form["pretty"]) > 0 {
prettyPrint = true
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
var buffer []byte
var err error
if prettyPrint {
prefix := ""
indent := " "
buffer, err = json.MarshalIndent(res, prefix, indent)
} else {
buffer, err = json.Marshal(res)
}
if err != nil {
w.WriteHeader(500)
glog.Errorf("Marshal http response error [err:%s]", err)
} else {
w.WriteHeader(200)
w.Write(buffer)
if prettyPrint {
io.WriteString(w, "\n")
}
}
return
}