-
Notifications
You must be signed in to change notification settings - Fork 26
/
http.go
156 lines (140 loc) · 3.34 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
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
147
148
149
150
151
152
153
154
155
156
package goperfcounter
import (
"encoding/json"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"strings"
)
func startHttp(addr string, debug bool) {
configCommonRoutes()
configProcRoutes()
if len(addr) >= 9 { //x.x.x.x:x
s := &http.Server{
Addr: addr,
MaxHeaderBytes: 1 << 30,
}
go func() {
if debug {
log.Println("[perfcounter] http server start, listening on", addr)
}
s.ListenAndServe()
if debug {
log.Println("[perfcounter] http server stop,", addr)
}
}()
}
}
// routers
func configProcRoutes() {
http.HandleFunc("/pfc/proc/metrics/json", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
RenderJson(w, rawMetrics())
})
http.HandleFunc("/pfc/proc/metrics/falcon", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
RenderJson(w, falconMetrics())
})
// url=/pfc/proc/metric/{json,falcon}
http.HandleFunc("/pfc/proc/metrics/", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
urlParam := r.URL.Path[len("/pfc/proc/metrics/"):]
args := strings.Split(urlParam, "/")
argsLen := len(args)
if argsLen != 2 {
RenderJson(w, "")
return
}
types := []string{}
typeslice := strings.Split(args[0], ",")
for _, t := range typeslice {
nt := strings.TrimSpace(t)
if nt != "" {
types = append(types, nt)
}
}
if args[1] == "json" {
RenderJson(w, rawMetric(types))
return
}
if args[1] == "falcon" {
RenderJson(w, falconMetric(types))
return
}
})
http.HandleFunc("/pfc/proc/metrics/size", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
RenderJson(w, rawSizes())
})
}
func configCommonRoutes() {
http.HandleFunc("/pfc/health", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
w.Write([]byte("ok"))
})
http.HandleFunc("/pfc/version", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
w.Write([]byte(fmt.Sprintf("%s\n", VERSION)))
})
http.HandleFunc("/pfc/config", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
RenderJson(w, config())
})
http.HandleFunc("/pfc/config/reload", func(w http.ResponseWriter, r *http.Request) {
if !isLocalReq(r.RemoteAddr) {
RenderJson(w, "no privilege")
return
}
loadConfig()
RenderJson(w, "ok")
})
}
func isLocalReq(raddr string) bool {
if strings.HasPrefix(raddr, "127.0.0.1") {
return true
}
return false
}
// render
func RenderJson(w http.ResponseWriter, data interface{}) {
renderJson(w, Dto{Msg: "success", Data: data})
}
func RenderString(w http.ResponseWriter, msg string) {
renderJson(w, map[string]string{"msg": msg})
}
func renderJson(w http.ResponseWriter, v interface{}) {
bs, err := json.Marshal(v)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(bs)
}
// common http return
type Dto struct {
Msg string `json:"msg"`
Data interface{} `json:"data"`
}