-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
http.go
62 lines (53 loc) · 1.15 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
package http
import (
"encoding/json"
"github.com/open-falcon/falcon-plus/modules/hbs/g"
"log"
"net/http"
_ "net/http/pprof"
)
type Dto struct {
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func init() {
configCommonRoutes()
configProcRoutes()
}
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 Start() {
if !g.Config().Http.Enabled {
return
}
addr := g.Config().Http.Listen
if addr == "" {
return
}
s := &http.Server{
Addr: addr,
MaxHeaderBytes: 1 << 30,
}
log.Println("http listening", addr)
log.Fatalln(s.ListenAndServe())
}