forked from Project-PLATEAU/PLATEAU-VIEW-3.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
64 lines (53 loc) · 1.73 KB
/
proxy.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
package main
import (
"net/http"
"net/url"
"strings"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func proxyHandlerFunc(c echo.Context) error {
// Extract the target URL from the request path
targetPath := c.Param("*")
// This shouldn't be done by us but It'll do for now: @pyshx
if strings.HasPrefix(targetPath, "http:/") && len(targetPath) > 6 && targetPath[6] != '/' {
targetPath = "http://" + strings.TrimPrefix(targetPath, "http:/")
} else if strings.HasPrefix(targetPath, "https:/") && len(targetPath) > 7 && targetPath[7] != '/' {
targetPath = "https://" + strings.TrimPrefix(targetPath, "https:/")
}
targetURL, err := url.Parse(targetPath)
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"error": "Invalid target URL",
})
}
// Define the ProxyConfig object with custom Rewrite rules and ModifyResponse function
proxyConfig := middleware.ProxyConfig{
Balancer: middleware.NewRoundRobinBalancer([]*middleware.ProxyTarget{
{
URL: targetURL,
},
}),
}
// Create a new middleware.ProxyWithConfig() middleware with the target URL
proxyMiddleware := middleware.ProxyWithConfig(proxyConfig)(func(c echo.Context) error {
return nil
})
// Invoke the proxy middleware to handle the request and return the response
if err := proxyMiddleware(c); err != nil {
return err
}
return nil
}
func setResponseACAOHeaderFromRequest(c echo.Context) {
c.Response().Header().Set(echo.HeaderAccessControlAllowOrigin,
c.Request().Header.Get(echo.HeaderOrigin))
}
func ACAOHeaderOverwriteMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Before(func() {
setResponseACAOHeaderFromRequest(c)
})
return next(c)
}
}