/
handlers.go
257 lines (238 loc) · 6.5 KB
/
handlers.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
// handlers module
//
// Copyright (c) 2019 - Valentin Kuznetsov <vkuznet@gmail.com>
//
import (
"encoding/json"
"fmt"
"net/http"
"os"
"runtime"
"strings"
"sync/atomic"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/process"
logs "github.com/sirupsen/logrus"
)
// TotalGetRequests counts total number of GET requests received by the server
var TotalGetRequests uint64
// TotalPostRequests counts total number of POST requests received by the server
var TotalPostRequests uint64
// ServerSettings controls server parameters
type ServerSettings struct {
Level int `json:"level"` // verbosity level
LogFormatter string `json:"logFormatter"` // logrus formatter
}
//
// ### HTTP METHODS
//
// AuthHandler authenticate incoming requests and route them to appropriate handler
func AuthHandler(w http.ResponseWriter, r *http.Request) {
// increment GET/POST counters
if r.Method == "GET" {
atomic.AddUint64(&TotalGetRequests, 1)
}
if r.Method == "POST" {
atomic.AddUint64(&TotalPostRequests, 1)
}
// check if server started with hkey file (auth is required)
var user string
var err error
user, err = username(r)
if err != nil {
return
}
logs.WithFields(logs.Fields{"User": user, "Path": r.URL.Path}).Info("")
// define all methods which requires authentication
arr := strings.Split(r.URL.Path, "/")
path := arr[len(arr)-1]
switch path {
case "faq":
FAQHandler(w, r)
case "status":
StatusHandler(w, r)
case "server":
SettingsHandler(w, r)
default:
DataHandler(w, r)
}
}
// GET Methods
// DataHandler handlers Data requests
func DataHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// check HTTP header
var accept, content string
if _, ok := r.Header["Accept"]; ok {
accept = r.Header["Accept"][0]
}
if _, ok := r.Header["Content-Type"]; ok {
content = r.Header["Content-Type"][0]
}
user, _ := username(r)
name := r.FormValue("name")
codes, _ := exitCodes(name)
arr := strings.Split(r.URL.Path, "/")
key := arr[len(arr)-1]
if len(arr) > 1 {
cfinal := make(map[string]interface{})
for k, val := range codes {
switch vvv := val.(type) {
case map[string]interface{}:
if key != "" {
if v, ok := vvv[key]; ok {
c := make(map[string]string)
c[key] = v.(string)
cfinal[k] = c
}
} else {
cfinal[k] = val
}
case string:
if k == key {
cfinal[k] = val
}
}
}
codes = cfinal
}
// return json representation of the data
if strings.Contains(accept, "json") || strings.Contains(content, "json") {
data, err := json.Marshal(codes)
if err != nil {
w.Write([]byte(fmt.Sprintf("unable to marshal data, error=%v", err)))
return
}
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
// return web UI representation of the data
var templates ServerTemplates
tmplData := make(map[string]interface{})
tmplData["User"] = user
tmplData["Date"] = time.Now().Unix()
tmplData["Name"] = name
tmplData["ExitCodes"] = codes
page := templates.Codes(Config.Templates, tmplData)
w.WriteHeader(http.StatusOK)
w.Write([]byte(_top + page + _bottom))
}
// FAQHandler handlers FAQ requests
func FAQHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
var templates ServerTemplates
tmplData := make(map[string]interface{})
page := templates.FAQ(Config.Templates, tmplData)
w.WriteHeader(http.StatusOK)
w.Write([]byte(_top + page + _bottom))
}
// Memory structure keeps track of server memory
type Memory struct {
Total uint64 `json:"total"`
Free uint64 `json:"free"`
Used uint64 `json:"used"`
UsedPercent float64 `json:"usedPercent"`
}
// Mem structure keeps track of virtual/swap memory of the server
type Mem struct {
Virtual Memory
Swap Memory
}
// StatusHandler handlers Status requests
func StatusHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
// check HTTP header
var accept, content string
if _, ok := r.Header["Accept"]; ok {
accept = r.Header["Accept"][0]
}
if _, ok := r.Header["Content-Type"]; ok {
content = r.Header["Content-Type"][0]
}
// get cpu and mem profiles
m, _ := mem.VirtualMemory()
s, _ := mem.SwapMemory()
l, _ := load.Avg()
c, _ := cpu.Percent(time.Millisecond, true)
process, perr := process.NewProcess(int32(os.Getpid()))
// get unfinished queries
var templates ServerTemplates
tmplData := make(map[string]interface{})
tmplData["NGo"] = runtime.NumGoroutine()
// virt := Memory{Total: m.Total, Free: m.Free, Used: m.Used, UsedPercent: m.UsedPercent}
// swap := Memory{Total: s.Total, Free: s.Free, Used: s.Used, UsedPercent: s.UsedPercent}
tmplData["Memory"] = m.UsedPercent
tmplData["Swap"] = s.UsedPercent
tmplData["Load1"] = l.Load1
tmplData["Load5"] = l.Load5
tmplData["Load15"] = l.Load15
tmplData["CPU"] = c
if perr == nil { // if we got process info
conn, err := process.Connections()
if err == nil {
tmplData["Connections"] = conn
}
openFiles, err := process.OpenFiles()
if err == nil {
tmplData["OpenFiles"] = openFiles
}
}
tmplData["Uptime"] = time.Since(Time0).Seconds()
tmplData["GetRequests"] = TotalGetRequests
tmplData["PostRequests"] = TotalPostRequests
page := templates.Status(Config.Templates, tmplData)
if strings.Contains(accept, "json") || strings.Contains(content, "json") {
data, err := json.Marshal(tmplData)
if err != nil {
w.Write([]byte(fmt.Sprintf("unable to marshal data, error=%v", err)))
return
}
w.WriteHeader(http.StatusOK)
w.Write(data)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(_top + page + _bottom))
}
// POST methods
// SettingsHandler handlers Settings requests
func SettingsHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
defer r.Body.Close()
var s = ServerSettings{}
err := json.NewDecoder(r.Body).Decode(&s)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if s.LogFormatter == "json" {
logs.SetFormatter(&logs.JSONFormatter{})
} else if s.LogFormatter == "text" {
logs.SetFormatter(&logs.TextFormatter{})
} else {
logs.SetFormatter(&logs.TextFormatter{})
}
logs.WithFields(logs.Fields{
"Verbose level": s.Level,
"Log formatter": s.LogFormatter,
}).Debug("update server settings")
w.WriteHeader(http.StatusOK)
return
}