This commit is contained in:
桀尼龟 2025-05-22 20:19:42 +08:00
parent c9635a647c
commit 7b5fd0182a
2 changed files with 49 additions and 0 deletions

View File

@ -66,6 +66,9 @@ func NewRouter() *gin.Engine {
acRoute.POST("approval", account.UserApproval) //实名审核 acRoute.POST("approval", account.UserApproval) //实名审核
} }
} }
{
v1.POST("chart/forward/test/", redirect.ForwardTest)
}
// 上传 // 上传
upData := privateGroup.Group("upload") upData := privateGroup.Group("upload")
upData.Use(middleware.CheckLogin(service.AccountProvider)) upData.Use(middleware.CheckLogin(service.AccountProvider))

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"fonchain-fiee/pkg/service" "fonchain-fiee/pkg/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io"
"net/http" "net/http"
"time" "time"
) )
@ -32,3 +33,48 @@ func ToRedirectRealUrlAdnRand(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, realUrl+fmt.Sprintf("?time=%d", time.Now().Unix())) 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)
}