-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlerProxy.go
60 lines (50 loc) · 1.56 KB
/
handlerProxy.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
package main
import (
"github.com/gin-gonic/gin"
"io"
"log"
"net/http"
)
func proxyHandler(c *gin.Context) {
// Determine the target URL (modify as needed)
targetURL := config.targetUrl
targetURL.Path = c.Param("path")
log.Println("Proxying to", targetURL.String())
// Create a new request to the target service, copying the method and the body
proxyReq, err := http.NewRequest(c.Request.Method, targetURL.String(), c.Request.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create request"})
return
}
// Copy headers (optional, choose which headers to forward)
for key, value := range c.Request.Header {
proxyReq.Header[key] = value
}
// Create a client and send the request
client := &http.Client{}
resp, err := client.Do(proxyReq)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to forward request"})
return
}
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read response body"})
return
}
// also copy response headers to proxy response
for key, values := range resp.Header {
for _, value := range values {
c.Writer.Header().Add(key, value)
}
}
// forward pseudonoimzied identity header for use in downstream system
username := c.GetString("username")
if username != "" {
c.Header("X-Trantor-Identity", createHash(username))
}
// Forward the status code and response body
c.Data(resp.StatusCode, resp.Header.Get("Content-Type"), body)
}