-
Notifications
You must be signed in to change notification settings - Fork 7
/
monitor.go
104 lines (91 loc) · 2.31 KB
/
monitor.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
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"html/template"
"log"
"net/http"
"strconv"
)
func HomeHandler(w http.ResponseWriter, r *http.Request) {
tmpl, _ := getTpl(w, "index.tpl")
if tmpl != nil {
data := new(HtmlData)
data.Title = "GoHAProxy"
for _, proxy := range proxyServer.ServerList {
data.ProxyStatus = append(data.ProxyStatus, proxy.srvProxy)
}
tmpl.Execute(w, data)
}
}
func ApiHandler(w http.ResponseWriter, r *http.Request) {
/*vars := mux.Vars(r)
now := time.Now().Unix()
data := getModuleRecord(vars["method"], now - 86400, now)
json, err := json.Marshal(data)
if err != nil {
w.WriteHeader(500)
log.Println("Error generating json", err)
fmt.Fprintln(w, "Could not generate JSON")
}
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprint(w, string(json))*/
}
func liveApiHandler(w http.ResponseWriter, r *http.Request) {
/*vars := mux.Vars(r)
data := getModuleLastRecord(vars["siteId"], vars["method"])
json, err := json.Marshal(data)
if err != nil {
w.WriteHeader(500)
log.Println("Error generating json", err)
fmt.Fprintln(w, "Could not generate JSON")
}
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprint(w, string(json))*/
}
func SystemApiHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
result := make(map[string]string)
result["cmd"] = vars["cmd"]
switch vars["cmd"] {
case "reload":
}
json, err := json.Marshal(result)
if err != nil {
w.WriteHeader(500)
log.Println("Error generating json", err)
fmt.Fprintln(w, "Could not generate JSON")
}
w.Header().Set("Access-Control-Allow-Origin", "*")
fmt.Fprint(w, string(json))
}
func strToInt(str string) int {
i, err := strconv.Atoi(str)
if err != nil {
// handle error
fmt.Println(err)
return 0
}
return i
}
func getTpl(w http.ResponseWriter, tpl string) (*template.Template, error) {
tmpl, err := template.ParseFiles("./tpl/" + tpl)
if err != nil {
log.Println("Could not parse template", err)
fmt.Fprintln(w, "Problem parsing template", err)
}
return tmpl, err
}
func Monitor() {
httpServer()
}
func httpServer() {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
//System
r.HandleFunc("/api/system/{cmd}", SystemApiHandler)
http.Handle("/", r)
fmt.Println("Monitor server started.")
http.ListenAndServe(":8080", nil)
}