-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
116 lines (106 loc) · 3.94 KB
/
web.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
package helper
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
)
func GetFavicon(w http.ResponseWriter, r *http.Request) {
iconData, err := os.ReadFile("./favicon.ico")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Println(" - Error: ", err)
return
}
w.Header().Set("Content-Type", "image/x-icon")
w.Write(iconData)
}
// CatMode is a function that will print the request information
// It will print the URL, Method, Headers and Body of the request
// It is used to debug the request information
func CatMode(r *http.Request) {
enableCatMode := os.Getenv("CAT_MODE")
if enableCatMode == "1" && r.URL.Path != "/health" && r.URL.Path != "/favicon.ico" && r.URL.Path != "/healthz" {
log.Println(r)
}
}
// CheckBody is a function that will print the request information
// It will print the URL, Method, Headers and Body of the request
// It is used to debug the request information
// Needs to be used with io.TeeReader to be able to read the body multiple times
// Example:
// - http.HandleFunc("/", CheckBody)
//
// Output:
// - Request information:
// - Request URL: /?test=1
// - Request Method: GET
// - Request Headers: map[Accept:[*/*] Accept-Encoding:[gzip] User-Agent:[Go-http-client/1.1]]
// - Request Body: test=1
func CheckBody(w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, " * Error: "+err.Error(), http.StatusBadRequest)
return
}
log.Println(" -------------------------------------------------------------")
log.Println(" Request information:")
log.Println(" - Request URL: ", r.URL)
log.Println(" - Request Method: ", r.Method)
log.Println(" - Request Headers: ", r.Header)
log.Println(" - Request Body: ", string(body))
log.Println(" -------------------------------------------------------------")
}
func MessageHelper(w http.ResponseWriter, message string) {
output := Message{
Message: message,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
}
func HealthCheck(w http.ResponseWriter, r *http.Request) {
message := Message{
Message: "Thanks for checking in! I'm doing fine.",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
}
func WrongParameter(w http.ResponseWriter, r *http.Request) {
var (
paramString string
parameters = r.URL.Query()
)
for key, values := range parameters {
valueStr := strings.Join(values, ", ")
paramString += fmt.Sprintf("%s:%s ", key, valueStr)
}
Logger(fmt.Sprintf(" - Wrong parameters were passed: [%s]", paramString), "notice")
http.Error(w, WrongMessage, http.StatusTeapot)
}
func UndefinedAnswer(w http.ResponseWriter, r *http.Request) {
Logger("=============================================================", "warning")
Logger(" Wrong request with the following parameters:", "warning")
Logger(fmt.Sprint(" - Host: ", r.Host), "warning")
Logger(fmt.Sprint(" - Method: ", r.Method), "warning")
Logger(fmt.Sprint(" - Protocol: ", r.Proto), "warning")
Logger(fmt.Sprint(" - RequestURI: ", r.RequestURI), "warning")
Logger(fmt.Sprint(" - RemoteAddr: ", r.RemoteAddr), "warning")
Logger("=============================================================", "warning")
http.Error(w, WrongMessage, http.StatusTeapot)
}
func WrongPath() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Logger("=============================================================", "warning")
Logger(" Wrong request with the following parameters:", "warning")
Logger(fmt.Sprint(" - Host: ", r.Host), "warning")
Logger(fmt.Sprint(" - Method: ", r.Method), "warning")
Logger(fmt.Sprint(" - Protocol: ", r.Proto), "warning")
Logger(fmt.Sprint(" - RequestURI: ", r.RequestURI), "warning")
Logger(fmt.Sprint(" - RemoteAddr: ", r.RemoteAddr), "warning")
Logger("=============================================================", "warning")
http.Error(w, WrongMessage, http.StatusTeapot)
})
}