-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
129 lines (106 loc) · 3.21 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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/inferablehq/sentinel/pkg/strategies"
)
var config strategies.Config
func copyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func main() {
config = getConfig()
if os.Getenv("TARGET_URL") == "" {
log.Printf("TARGET_URL is not set")
os.Exit(1)
}
http.HandleFunc("/", router)
fmt.Println("Server is running on http://localhost:8080")
err := http.ListenAndServe("localhost:8080", nil)
if err != nil {
fmt.Printf("Error starting server: %s\n", err)
}
}
func router(w http.ResponseWriter, r *http.Request) {
log.Printf("Received request: %s %s", r.Method, r.URL.Path)
var found *strategies.Route
for _, route := range config.Routes {
if r.Method == route.Method && route.PathMatcher(r.URL.Path) {
log.Printf("Processing request: %s %s", r.Method, r.URL.Path)
found = &route
break
}
}
if found == nil {
log.Printf("Request not found: %s %s", r.Method, r.URL.Path)
http.NotFound(w, r)
return
}
targetURL := os.Getenv("TARGET_URL") + r.URL.Path
if r.URL.RawQuery != "" {
targetURL += "?" + r.URL.RawQuery
}
// Read the request body
requestBody, err := io.ReadAll(r.Body)
if err != nil {
log.Printf("Error reading request body: %v", err)
http.Error(w, "Error reading request", http.StatusInternalServerError)
return
}
// Process the request body
processedRequestBody, err := found.RequestBodyHandler(string(requestBody))
if err != nil {
log.Printf("Error handling request body: %v", err)
http.Error(w, "Error processing request", http.StatusInternalServerError)
return
}
// Create the proxy request
proxyReq, err := http.NewRequest(r.Method, targetURL, io.NopCloser(strings.NewReader(processedRequestBody)))
if err != nil {
log.Printf("Error creating proxy request: %v", err)
http.Error(w, "Error creating proxy request", http.StatusInternalServerError)
return
}
// Copy headers from the original request to the proxy request
copyHeader(proxyReq.Header, r.Header)
// Set the Content-Length header
proxyReq.ContentLength = int64(len(processedRequestBody))
proxyReq.Header.Set("Content-Length", fmt.Sprintf("%d", proxyReq.ContentLength))
proxyClient := &http.Client{}
proxyResp, err := proxyClient.Do(proxyReq)
if err != nil {
log.Printf("Error proxying request: %v", err)
http.Error(w, "Error proxying request", http.StatusInternalServerError)
return
}
defer proxyResp.Body.Close()
copyHeader(w.Header(), proxyResp.Header)
w.WriteHeader(proxyResp.StatusCode)
responseBody, err := io.ReadAll(proxyResp.Body)
if err != nil {
log.Printf("Error reading response body: %v", err)
http.Error(w, "Error reading response", http.StatusInternalServerError)
return
}
// Process the response body
processedResponseBody, err := found.ResponseBodyHandler(string(responseBody))
if err != nil {
log.Printf("Error processing response body: %v", err)
http.Error(w, "Error processing response", http.StatusInternalServerError)
return
}
_, err = w.Write([]byte(processedResponseBody))
if err != nil {
log.Printf("Error writing response: %v", err)
http.Error(w, "Error writing response", http.StatusInternalServerError)
return
}
}