-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
171 lines (128 loc) · 4.26 KB
/
main.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
package main
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
type Environment struct {
LISTEN_ADDRESS string
PLAUSIBLE_SCRIPT_URL string
PLAUSIBLE_API_URL string
}
func ParseEnvironment() *Environment {
LISTEN_ADDRESS := os.Getenv("LISTEN_ADDRESS")
PLAUSIBLE_SCRIPT_URL := os.Getenv("PLAUSIBLE_SCRIPT_URL")
PLAUSIBLE_API_URL := os.Getenv("PLAUSIBLE_API_URL")
if LISTEN_ADDRESS == "" {
LISTEN_ADDRESS = "localhost:8080"
}
if PLAUSIBLE_SCRIPT_URL == "" {
PLAUSIBLE_SCRIPT_URL = "https://plausible.io/js/%s"
}
if PLAUSIBLE_API_URL == "" {
PLAUSIBLE_API_URL = "https://plausible.io/api/event"
}
return &Environment{LISTEN_ADDRESS: LISTEN_ADDRESS, PLAUSIBLE_SCRIPT_URL: PLAUSIBLE_SCRIPT_URL, PLAUSIBLE_API_URL: PLAUSIBLE_API_URL}
}
func buildGetScriptHandler(plausibleScriptUrl string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
scriptExtension := chi.URLParam(r, "name")
url := fmt.Sprintf(plausibleScriptUrl, scriptExtension)
response, error := http.Get(url)
if error != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
if response.StatusCode != http.StatusOK {
w.WriteHeader(response.StatusCode)
w.Write([]byte(http.StatusText(response.StatusCode)))
return
}
defer response.Body.Close()
// Copying headers from the origin request to the response
for key, values := range response.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}
// Copying the status code from the origin request to the response
w.WriteHeader(response.StatusCode)
// Copying the body from the origin request to the response
_, error = io.Copy(w, response.Body)
if error != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
}
}
}
func buildPostEventHandler(plausibleApiUrl string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
request, error := http.NewRequest(r.Method, plausibleApiUrl, r.Body)
if error != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
for key, values := range r.Header {
for _, value := range values {
normalizedKey := strings.ToLower(key)
isCookieHeader := normalizedKey == "cookie"
isCloudflareHeader := strings.HasPrefix(normalizedKey, "cf-")
// Let's not copy the cookie and cloudflare headers
isAddable := !isCookieHeader && !isCloudflareHeader
if isAddable {
fmt.Println("Added", normalizedKey, value)
request.Header.Add(key, value)
}
}
}
b, err := io.ReadAll(r.Body)
if err != nil {
fmt.Println("Error reading body", err)
}
fmt.Println(string(b))
client := http.DefaultClient
response, error := client.Do(request)
if error != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
return
}
defer response.Body.Close()
// Copying headers from the origin response to the final response
for key, values := range response.Header {
for _, value := range values {
w.Header().Add(key, value)
}
}
// Copying the status code from the origin response to the final response
w.WriteHeader(response.StatusCode)
// Copying the body from the origin response to the final response
_, error = io.Copy(w, response.Body)
if error != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(http.StatusText(http.StatusInternalServerError)))
}
}
}
func buildGetHealthHandler() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(http.StatusText(http.StatusOK)))
}
}
func main() {
r := chi.NewRouter()
env := ParseEnvironment()
r.Use(middleware.Logger)
r.Get("/health", buildGetHealthHandler())
r.Get("/js/{name}", buildGetScriptHandler(env.PLAUSIBLE_SCRIPT_URL))
r.Post("/api/event", buildPostEventHandler(env.PLAUSIBLE_API_URL))
LISTEN_ADDRESS := (*env).LISTEN_ADDRESS
http.ListenAndServe(LISTEN_ADDRESS, r)
}