-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
71 lines (53 loc) · 1.21 KB
/
http.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
package utils
import (
"encoding/json"
http "github.com/Noooste/fhttp"
"io/ioutil"
"strings"
)
func GetRequestBody(request *http.Request) []byte {
var body []byte
encoding := request.Header.Get("Content-Encoding")
bodyBytes, err := ioutil.ReadAll(request.Body)
if err != nil {
body = []byte{}
} else if encoding != "" {
body = DecompressBody(bodyBytes, encoding)
} else {
body = bodyBytes
}
return body
}
func GetResponseBody(response *http.Response) []byte {
defer response.Body.Close()
encoding := response.Header.Get("content-encoding")
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
return []byte{}
}
return DecompressBody(bodyBytes, encoding)
}
type SuccessReturn struct {
Success bool `json:"success"`
Error string `json:"error"`
}
type SuccessInitSensor struct {
Success bool `json:"success"`
Id string `json:"id"`
}
func ReturnWeb(success any) []byte {
dumped, err := json.Marshal(success)
if err != nil {
return []byte("{}")
}
return dumped
}
func ReturnWebError(error string) []byte {
return ReturnWeb(SuccessReturn{
Success: false,
Error: error,
})
}
func GetIP(r *http.Request) string {
return strings.Split(r.RemoteAddr, ":")[0]
}