forked from open-falcon/falcon-plus
/
http.go
134 lines (112 loc) · 2.81 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
package http
import (
"bytes"
"encoding/json"
"github.com/open-falcon/falcon-plus/modules/transfer/g"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
)
type Dto struct {
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func Start() {
go startHttpServer()
}
func startHttpServer() {
if !g.Config().Http.Enabled {
return
}
addr := g.Config().Http.Listen
if addr == "" {
return
}
configCommonRoutes()
configProcHttpRoutes()
configDebugHttpRoutes()
configApiRoutes()
s := &http.Server{
Addr: addr,
MaxHeaderBytes: 1 << 30,
}
log.Println("http.startHttpServer ok, listening", addr)
log.Fatalln(s.ListenAndServe())
}
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)
}
func RenderDataJson(w http.ResponseWriter, data interface{}) {
RenderJson(w, Dto{Msg: "success", Data: data})
}
func RenderMsgJson(w http.ResponseWriter, msg string) {
RenderJson(w, map[string]string{"msg": msg})
}
func AutoRender(w http.ResponseWriter, data interface{}, err error) {
if err != nil {
RenderMsgJson(w, err.Error())
return
}
RenderDataJson(w, data)
}
func StdRender(w http.ResponseWriter, data interface{}, err error) {
if err != nil {
w.WriteHeader(400)
RenderMsgJson(w, err.Error())
return
}
RenderJson(w, data)
}
func postByJson(rw http.ResponseWriter, req *http.Request, url string) {
buf := new(bytes.Buffer)
buf.ReadFrom(req.Body)
s := buf.String()
reqPost, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(s)))
if err != nil {
log.Println("Error =", err.Error())
}
reqPost.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(reqPost)
if err != nil {
log.Println("Error =", err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
rw.Write(body)
}
func postByForm(rw http.ResponseWriter, req *http.Request, url string) {
req.ParseForm()
client := &http.Client{}
resp, err := client.PostForm(url, req.PostForm)
if err != nil {
log.Println("Error =", err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
rw.Write(body)
}
func getRequest(rw http.ResponseWriter, url string) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Println("Error =", err.Error())
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error =", err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
rw.Header().Set("Content-Type", "application/json; charset=UTF-8")
rw.Write(body)
}