-
Notifications
You must be signed in to change notification settings - Fork 1
/
umami_utils.go
105 lines (86 loc) · 2.17 KB
/
umami_utils.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
package traefik_umami_feeder
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"time"
)
func sendRequest(ctx context.Context, url string, body interface{}, headers http.Header) (*http.Response, error) {
var req *http.Request
var err error
if body != nil {
bodyJson, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err = http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(bodyJson))
} else {
req, err = http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
}
if err != nil {
return nil, err
}
if headers != nil {
req.Header = headers
}
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
status := resp.StatusCode
if status < 200 || status >= 300 {
defer func() {
_ = resp.Body.Close()
}()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("request failed with status %d (failed to read body: %v)", status, err)
}
return nil, fmt.Errorf("request failed with status %d (%v)", status, string(respBody))
}
return resp, nil
}
func sendRequestAndParse(ctx context.Context, url string, body interface{}, headers http.Header, value interface{}) error {
resp, err := sendRequest(ctx, url, body, headers)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(respBody, &value)
if err != nil {
return err
}
return nil
}
// opts the port from the host.
func parseDomainFromHost(host string) string {
// check if the host has a port
if strings.Contains(host, ":") {
host = strings.Split(host, ":")[0]
}
return strings.ToLower(host)
}
const parseAcceptLanguagePattern = `([a-zA-Z\-]+)(?:;q=\d\.\d)?(?:,\s)?`
var parseAcceptLanguageRegexp = regexp.MustCompile(parseAcceptLanguagePattern)
func parseAcceptLanguage(acceptLanguage string) string {
matches := parseAcceptLanguageRegexp.FindAllStringSubmatch(acceptLanguage, -1)
if len(matches) == 0 {
return ""
}
return matches[0][1]
}