/
http.go
113 lines (97 loc) · 2.29 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
package http
import (
"encoding/json"
"github.com/chengz0/xdashboard/global"
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
"html/template"
"log"
"net"
"net/http"
"time"
)
var (
martini_m *martini.ClassicMartini
timeout = time.Duration(2) * time.Second
)
func dialTimeout(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, timeout)
}
func StartMartini() {
// http client
transport := http.Transport{
Dial: dialTimeout,
}
client := http.Client{
Transport: &transport,
}
// martini
martini_m = martini.Classic()
martini_m.Use(render.Renderer(render.Options{
Funcs: []template.FuncMap{{
"nl2br": nl2br,
"htmlquote": htmlQuote,
"str2html": str2html,
"dateformat": dateFormat,
}},
}))
martini_m.Get("/", func(w http.ResponseWriter) string {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
ret, err := json.Marshal(global.HostIni)
if err != nil {
log.Printf("Error hostini: %s", err.Error())
return RenderErrDto(err.Error())
}
log.Println(string(ret))
return RenderDataDto(string(ret))
})
// //hosts
// martini_m.Get("/state", func(w http.ResponseWriter) string {
// w.Header().Set("Content-Type", "application/json;charset=UTF-8")
// ret, err := json.Marshal(global.HostIni)
// if err != nil {
// log.Printf("Error hostini: %s", err.Error())
// return err.Error()
// }
// log.Println(string(ret))
// return string(ret)
// })
// system state
martini_m.Get("/system", func(w http.ResponseWriter) string {
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
url := "http://172.16.110.134:3000/system"
ret := XRequest(&client, url)
log.Println(ret)
return ret
})
http.ListenAndServe(":4000", martini_m)
}
type Dto struct {
Succ bool
Msg string
Data interface{}
}
func ErrDto(message string) Dto {
return Dto{Succ: false, Msg: message}
}
func DataDto(d interface{}) Dto {
return Dto{Succ: true, Msg: "", Data: d}
}
func RenderErrDto(message string) string {
dto := ErrDto(message)
bs, err := json.Marshal(dto)
if err != nil {
return err.Error()
} else {
return string(bs)
}
}
func RenderDataDto(d interface{}) string {
dto := DataDto(d)
bs, err := json.Marshal(dto)
if err != nil {
return err.Error()
} else {
return string(bs)
}
}