-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhttp.go
83 lines (72 loc) · 2.35 KB
/
http.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
package gofast
import "fmt"
import "time"
import "strings"
import "net/http"
import "github.com/bnclabs/gson"
// Statshandler http handler to handle statistics endpoint, returns
// statistics for specified transport or aggregate statistics of all
// transports, based on the query parameters.
//
// NOTE: This handler is used by gofast/http package. Typically
// application are not expected to use this function directly.
func Statshandler(w http.ResponseWriter, r *http.Request) {
if query := r.URL.Query(); query != nil {
name, _ := query["name"]
keyparam, _ := query["keys"]
keys := []string{}
if len(keyparam) > 0 {
keys = strings.Split(strings.Trim(keyparam[0], " \r\n\t"), ",")
}
var stats map[string]uint64
if len(name) == 0 {
stats = Stats()
} else if stats = Stat(name[0]); stats == nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("invalid name %q\n", name[0])))
return
}
stats = filterstats(stats, keys)
stats["timestamp"] = uint64(time.Now().UnixNano())
buf, conf := make([]byte, 0, 1024), gson.NewDefaultConfig()
jsonstats := conf.NewValue(stats).Tojson(conf.NewJson(buf)).Bytes()
// TODO: remove this once gson becomes stable.
//jsonstats, err := json.Marshal(stats)
//if err != nil {
// w.WriteHeader(http.StatusInternalServerError)
// w.Write([]byte(err.Error() + "\n"))
// return
//}
header := w.Header()
header["Content-Type"] = []string{"application/json"}
header["Access-Control-Allow-Origin"] = []string{"*"}
w.WriteHeader(200)
w.Write(jsonstats)
w.Write([]byte("\n"))
}
}
// Listhandler http handler to return list of active transport.
//
// NOTE: This handler is used by gofast/http package. Typically
// application are not expected to use this function directly.
func Listhandler(w http.ResponseWriter, r *http.Request) {
list := listtransports()
buf, conf := make([]byte, 0, 1024), gson.NewDefaultConfig()
names := conf.NewValue(list).Tojson(conf.NewJson(buf)).Bytes()
header := w.Header()
header["Content-Type"] = []string{"application/json"}
header["Access-Control-Allow-Origin"] = []string{"*"}
w.WriteHeader(200)
w.Write(names)
w.Write([]byte("\n"))
}
func filterstats(stats map[string]uint64, keys []string) map[string]uint64 {
if len(keys) == 0 {
return stats
}
m := map[string]uint64{}
for _, key := range keys {
m[key] = stats[key]
}
return m
}