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
|
package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net"
"strings"
"regexp"
"time"
)
var fs http.Handler
func init() {
fs = http.FileServer(http.Dir("static"))
}
const LogLength = 100
type accessLog struct {
ClientAddr string `json:"clientAddr"`
RequestedPath string `json:"requestedPath"`
RequestTime time.Time `json:"requestTime"`
HttpMethod string `json:"httpMethod"`
}
func CreateFilesHandler(logs *[LogLength]string, n *int, logChan chan string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
jsonData, _ := json.Marshal(accessLog{
ClientAddr: redactIP(r.RemoteAddr),
RequestedPath: r.URL.Path,
RequestTime: time.Now().UTC(),
HttpMethod: r.Method,
})
addRotLog(logs, n, logChan, string(jsonData))
// Serve the index.html file from the static directory
http.StripPrefix("/", fs).ServeHTTP(w, r)
}
}
func CreateGetLogs(logs *[LogLength]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
for _, s := range logs {
fmt.Fprintln(w, s)
}
}
}
func CreateLoginHandler(logs *[LogLength]string, n *int, logChan chan string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
defer r.Body.Close()
var data map[string]any
if json.Unmarshal(body, &data) != nil {
addRotLog(logs, n, logChan, fmt.Sprintf(`{"authRequest": %s}`, string(body)))
http.Error(w, `{"message": "Unauthorized"}`, http.StatusUnauthorized)
return
}
if email, ok := data["email"].(string); ok {
if rememberMe, ok := data["rememberMe"].(bool); ok {
addRotLog(logs, n, logChan, fmt.Sprintf(`{"authRequest": {"email": "%s", "password": "XXXXXXXX", "loginTime": "%s", "success": false, "rememberMe": %t}}`, email, time.Now().UTC(), rememberMe))
}
}
http.Error(w, `{"message": "Unauthorized"}`, http.StatusUnauthorized)
}
}
func StatusHandler(w http.ResponseWriter, r *http.Request) {
type response struct {
Status string `json:"status"`
Description string `json:"description"`
Uptime float64 `json:"uptime"`
ResponseTime int `json:"responseTime"`
ActiveIncidents int `json:"activeIncidents"`
ScheduledMaintenance int `json:"scheduledMaintenance"`
}
}
func StatusServicesHandler(w http.ResponseWriter, r *http.Request) {
type service struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Icon string `json:"icon"`
Status string `json:"status"`
ResponseTime int `json:"responseTime"`
Uptime float64 `json:"uptime"`
}
var response []service
_ = response
}
func StatusMetricsHandler(w http.ResponseWriter, r *http.Request) {
type response struct {
Uptime float64 `json:"uptime"`
ResponseTime int `json:"responseTime"`
RequestVolume int `json:"requestVolume"`
ErrorRate float64 `json:"errorRate"`
}
}
func StatusIncidentsHandler(w http.ResponseWriter, r *http.Request) {
type incident struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Status string `json:"status"`
Severity string `json:"severity"`
StartTime time.Time `json:"startTime"`
AffectedServices []string `json:"affectedServices"`
}
var response []incident
_ = response
}
func StatusMaintenanceHandler(w http.ResponseWriter, r *http.Request) {
type event struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
AffectedServices []string `json:"affectedServices"`
}
var response []event
_ = response
}
func StatusUptimeHandler(w http.ResponseWriter, r *http.Request) {
type event struct {
Date time.Time `json:"date"`
Uptime float64 `json:"uptime"`
}
var response []event
_ = response
}
// TODO: SUBSCRIBE TO EMAILS
func StatusSubscribeHandler(w http.ResponseWriter, r *http.Request) {
}
// TODO: CONTACT HANDLER
func ContactHandler(w http.ResponseWriter, r *http.Request) {
}
func redactIP(input string) string {
ipRegex := `\b(?:\d{1,3}\.){3}\d{1,3}\b`
re := regexp.MustCompile(ipRegex)
return re.ReplaceAllStringFunc(input, func(match string) string {
if ip := net.ParseIP(match); ip != nil {
parts := strings.Split(match, ".")
if len(parts) == 4 {
parts[3] = "XXX"
return strings.Join(parts, ".")
}
}
return match
})
}
func addRotLog(logs *[LogLength]string, last *int, parser chan string, value string) {
if strings.Contains(value, "\n") {
for _, v := range strings.Split(value, "\n") {
addRotLog(logs, last, parser, v)
}
} else {
if *last == LogLength {
for i := 0; i < LogLength-1; i++ {
logs[i] = logs[i+1]
}
logs[LogLength-1] = value
parser <- value
} else {
logs[*last] = value
*last++
parser <- value
}
}
}
|