/
handler.go
182 lines (131 loc) · 4.05 KB
/
handler.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package main
import (
"net/http"
"strings"
"time"
dg "github.com/iobear/dashgoat/common"
"github.com/labstack/echo/v4"
)
// updateStatus - service update
func updateStatus(c echo.Context) error {
ss.mutex.Lock()
defer ss.mutex.Unlock()
var result = map[string]string{}
var postService dg.ServiceState
if err := c.Bind(&postService); err != nil {
return err
}
if !checkUpdatekey(postService.UpdateKey) {
return c.JSON(http.StatusUnauthorized, "Check your updatekey!")
}
postService.UpdateKey = "valid"
postService = dg.FilterUpdate(postService)
postService = runDependOn(postService)
// TODO
// if err := validator.Validate(postService); err != nil {
// return c.JSON(http.StatusBadRequest, ss.serviceStateList)
// }
strID := ""
if postService.Host != "" && postService.Service != "" {
strID = postService.Host + postService.Service
} else {
return c.JSON(http.StatusBadRequest, postService)
}
if len(postService.From) == 0 { //From can't be empty
postService.From = append(postService.From, "127.0.0.1")
}
iSnewState(postService) // Informs abount state change
if _, ok := ss.serviceStateList[strID]; ok {
if postService.Change == 0 {
if postService.Status != ss.serviceStateList[strID].Status {
postService.Change = time.Now().Unix()
} else {
postService.Change = ss.serviceStateList[strID].Change
}
} else if postService.Probe <= ss.serviceStateList[strID].Probe { // Already reported
return c.JSON(http.StatusAlreadyReported, "")
}
}
if _, exists := ss.serviceStateList[strID]; !exists {
postService.Change = time.Now().Unix()
}
ss.serviceStateList[strID] = postService
go updateBuddy(postService, "")
result["id"] = strID
return c.JSON(http.StatusOK, result)
}
// getStatus - get status of service with service id
func getStatus(c echo.Context) error {
id := c.Param("id")
ss.mutex.RLock()
defer ss.mutex.RUnlock()
return c.JSON(http.StatusOK, ss.serviceStateList[id])
}
// getStatusList - return all data
func getStatusList(c echo.Context) error {
ss.mutex.RLock()
defer ss.mutex.RUnlock()
if len(ss.serviceStateList) == 0 {
return c.JSON(http.StatusNoContent, "")
}
return c.JSON(http.StatusOK, ss.serviceStateList)
}
// getStatusList HPO/MSO
func getStatusListMSO(c echo.Context) error {
ss.mutex.RLock()
defer ss.mutex.RUnlock()
var tmpServiceStateMSO ServiceStateMSO
serviceStateMSOlist := make(map[string]ServiceStateMSO)
for index, event := range ss.serviceStateList {
tmpServiceStateMSO.Status = event.Status
tmpServiceStateMSO.Message = "[" + event.Status + "] " + event.Service + " " + event.Host + "-" + event.Message
serviceStateMSOlist[index] = tmpServiceStateMSO
}
return c.JSON(http.StatusOK, serviceStateMSOlist)
}
// serviceFilter - list services with item value of..
func serviceFilter(c echo.Context) error {
//placeholder func
ss.mutex.RLock()
defer ss.mutex.RUnlock()
return c.JSON(http.StatusOK, ss.serviceStateList)
}
// getUniq - list unique values of service items
func getUniq(c echo.Context) error {
item := c.Param("serviceitem")
var result = []string{}
if item == "id" {
result = listServiceIDs()
} else {
result = uniqList(item)
}
return c.JSON(http.StatusOK, result)
}
// deleteServiceHandler - removes service from serviceStateList
func deleteServiceHandler(c echo.Context) error {
ss.mutex.RLock()
defer ss.mutex.RUnlock()
id := c.Param("id")
id = strings.Replace(id, " ", "-", -1)
_, mapContainsKey := ss.serviceStateList[id]
if mapContainsKey {
go deleteServiceState(id)
return c.NoContent(http.StatusNoContent)
}
return c.NoContent(http.StatusNotFound)
}
// health of dashGoat app
func health(c echo.Context) error {
if !isDashGoatReady() {
return c.NoContent(http.StatusServiceUnavailable)
}
return c.JSON(http.StatusOK, readHostFacts())
}
func checkUpdatekey(key string) bool {
return key == config.UpdateKey
}