feat(server): 添加反向代理网关功能并升级依赖
- 新增 gateway.go 文件实现 HTTP 反向代理功能 - 添加 BuildRequest 函数用于创建反向代理处理器 - 实现自定义 Director 保留原始路径和查询参数 - 集成错误处理机制返回适当的 HTTP 状态码 - 升级 gf-common 依赖从 v1.0.1010 到 v1.0.1011 - 更新 go.mod 和 go.sum 文件依赖信息 - 更新 IDE 缓存配置文件中的路径映射信息main
parent
5affb5e653
commit
3096d7dedd
|
|
@ -153,6 +153,7 @@
|
|||
<entry key="logWriter">
|
||||
<value>
|
||||
<set>
|
||||
<option value="file://$PROJECT_DIR$/../gf-common/log/log.go" />
|
||||
<option value="file://$PROJECT_DIR$/log/log.go" />
|
||||
<option value="file://$PROJECT_DIR$/../yingji/api-gateway/internal/utils/log.go" />
|
||||
</set>
|
||||
|
|
@ -176,6 +177,25 @@
|
|||
</option>
|
||||
<option name="scannedPathMapping">
|
||||
<map>
|
||||
<entry key="file://$PROJECT_DIR$/../gf-common/log/log.go">
|
||||
<value>
|
||||
<ScannedPath>
|
||||
<option name="lastModified" value="1772763640548" />
|
||||
<option name="schema">
|
||||
<list>
|
||||
<option value="logWriter" />
|
||||
</list>
|
||||
</option>
|
||||
</ScannedPath>
|
||||
</value>
|
||||
</entry>
|
||||
<entry key="file://$PROJECT_DIR$/../gf-common/server/gateway.go">
|
||||
<value>
|
||||
<ScannedPath>
|
||||
<option name="lastModified" value="1772764256932" />
|
||||
</ScannedPath>
|
||||
</value>
|
||||
</entry>
|
||||
<entry key="file://$PROJECT_DIR$/config/fun.go">
|
||||
<value>
|
||||
<ScannedPath>
|
||||
|
|
@ -323,6 +343,13 @@
|
|||
</ScannedPath>
|
||||
</value>
|
||||
</entry>
|
||||
<entry key="file://$PROJECT_DIR$/server/gateway.go">
|
||||
<value>
|
||||
<ScannedPath>
|
||||
<option name="lastModified" value="1772764771892" />
|
||||
</ScannedPath>
|
||||
</value>
|
||||
</entry>
|
||||
<entry key="file://$PROJECT_DIR$/server/server.go">
|
||||
<value>
|
||||
<ScannedPath>
|
||||
|
|
@ -436,7 +463,7 @@
|
|||
<entry key="file://$PROJECT_DIR$/../yingji/api-gateway/internal/cmd/cmd.go">
|
||||
<value>
|
||||
<ScannedPath>
|
||||
<option name="lastModified" value="1772762475609" />
|
||||
<option name="lastModified" value="1772763999954" />
|
||||
</ScannedPath>
|
||||
</value>
|
||||
</entry>
|
||||
|
|
|
|||
2
go.mod
2
go.mod
|
|
@ -3,7 +3,7 @@ module git.magicany.cc/black1552/gin-base
|
|||
go 1.25.0
|
||||
|
||||
require (
|
||||
git.magicany.cc/black1552/gf-common v1.0.1010
|
||||
git.magicany.cc/black1552/gf-common v1.0.1011
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1
|
||||
github.com/fsnotify/fsnotify v1.9.0
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -1,7 +1,7 @@
|
|||
filippo.io/edwards25519 v1.1.1 h1:YpjwWWlNmGIDyXOn8zLzqiD+9TyIlPhGFG96P39uBpw=
|
||||
filippo.io/edwards25519 v1.1.1/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
git.magicany.cc/black1552/gf-common v1.0.1010 h1:HCSxfSpmvcjUw6KcsuEpWAMxxxiq+K73VsA2lHThkTo=
|
||||
git.magicany.cc/black1552/gf-common v1.0.1010/go.mod h1:TRyOXXb0no1RqiCtbVgg7/V7+v3m6dlS0Sg6fcfKOgw=
|
||||
git.magicany.cc/black1552/gf-common v1.0.1011 h1:utJu6F5xTi/Wbr19cXvVN3xi75nbqS3QE0U8sp8naFo=
|
||||
git.magicany.cc/black1552/gf-common v1.0.1011/go.mod h1:TRyOXXb0no1RqiCtbVgg7/V7+v3m6dlS0Sg6fcfKOgw=
|
||||
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
|
||||
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"git.magicany.cc/black1552/gin-base/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// BuildRequest 创建 HTTP 反向代理 Handler
|
||||
// @Param host 目标服务器地址,例如 "http://127.0.0.1:8081"
|
||||
func BuildRequest(host string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
targetURL, err := url.Parse(host)
|
||||
if err != nil {
|
||||
log.Error("parse target host error:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "invalid target host"})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
// 创建反向代理
|
||||
proxy := httputil.NewSingleHostReverseProxy(targetURL)
|
||||
|
||||
// 自定义 Director 修改请求,保留原始路径和查询参数
|
||||
originalDirector := proxy.Director
|
||||
proxy.Director = func(req *http.Request) {
|
||||
originalDirector(req)
|
||||
// 保留原始请求的路径和查询参数
|
||||
req.URL.Path = c.Request.URL.Path
|
||||
req.URL.RawQuery = c.Request.URL.RawQuery
|
||||
req.Host = targetURL.Host
|
||||
}
|
||||
|
||||
// 处理代理错误
|
||||
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
|
||||
log.Error("proxy error:", err)
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": "proxy error: " + err.Error()})
|
||||
}
|
||||
|
||||
proxy.ServeHTTP(c.Writer, c.Request)
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue