diff --git a/pkg/router/router.go b/pkg/router/router.go index 0e53fcb..4c53531 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -66,6 +66,9 @@ func NewRouter() *gin.Engine { acRoute.POST("approval", account.UserApproval) //实名审核 } } + { + v1.POST("chart/forward/test/", redirect.ForwardTest) + } // 上传 upData := privateGroup.Group("upload") upData.Use(middleware.CheckLogin(service.AccountProvider)) diff --git a/pkg/service/redirect/redirect.go b/pkg/service/redirect/redirect.go index e5faaf5..b67a911 100644 --- a/pkg/service/redirect/redirect.go +++ b/pkg/service/redirect/redirect.go @@ -5,6 +5,7 @@ import ( "fmt" "fonchain-fiee/pkg/service" "github.com/gin-gonic/gin" + "io" "net/http" "time" ) @@ -32,3 +33,48 @@ func ToRedirectRealUrlAdnRand(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, realUrl+fmt.Sprintf("?time=%d", time.Now().Unix())) } +func ForwardTest(c *gin.Context) { + // 1. 创建HTTP客户端(设置超时避免长时间等待) + client := &http.Client{ + Timeout: 10 * time.Second, + } + + // 2. 构造请求 + req, err := http.NewRequest("GET", "https://stockanalysis.com/chart/__data.json", nil) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "Failed to create request", + }) + return + } + + // 3. 添加原始请求的查询参数(可选) + req.URL.RawQuery = c.Request.URL.RawQuery + + // 4. 添加必要的请求头(根据目标API要求) + req.Header.Set("User-Agent", "Mozilla/5.0") + // 可以添加其他必要的headers,如: + // req.Header.Set("Accept", "application/json") + + // 5. 发送请求 + resp, err := client.Do(req) + if err != nil { + c.JSON(http.StatusBadGateway, gin.H{ + "error": "Failed to fetch data from target API", + }) + return + } + defer resp.Body.Close() + + // 6. 读取响应体 + body, err := io.ReadAll(resp.Body) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "Failed to read response body", + }) + return + } + + // 7. 转发响应 + c.Data(resp.StatusCode, resp.Header.Get("Content-Type"), body) +}