feat: 添加反向代理功能并更新宠物里程碑描述
添加反向代理功能,支持将/bbs/*路径请求转发至指定上游服务器 更新宠物里程碑描述,增加击杀和捕捉相关说明
This commit is contained in:
@@ -7,6 +7,8 @@ import (
|
||||
"blazing/modules/config/service"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
@@ -15,6 +17,8 @@ import (
|
||||
"github.com/lxzan/gws"
|
||||
)
|
||||
|
||||
const UpStream = "http://43.248.3.21:45632/"
|
||||
|
||||
func MiddlewareCORS(r *ghttp.Request) {
|
||||
r.Response.CORSDefault()
|
||||
corsOptions := r.Response.DefaultCORSOptions()
|
||||
@@ -34,6 +38,34 @@ func MiddlewareCORS(r *ghttp.Request) {
|
||||
// r.Response.Header().Set("Server", "blazing")
|
||||
r.Middleware.Next()
|
||||
}
|
||||
func StartServerProxy() {
|
||||
s := g.Server()
|
||||
// Parse the upstream URL
|
||||
u, _ := url.Parse(UpStream)
|
||||
// Create a new reverse proxy instance
|
||||
proxy := httputil.NewSingleHostReverseProxy(u)
|
||||
// Configure error handling for proxy failures
|
||||
proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, e error) {
|
||||
writer.WriteHeader(http.StatusBadGateway)
|
||||
}
|
||||
// Handle all requests with path prefix "/proxy/*"
|
||||
s.BindHandler("/bbs/*url", func(r *ghttp.Request) {
|
||||
var (
|
||||
originalPath = r.Request.URL.Path
|
||||
proxyToPath = "/" + r.Get("url").String()
|
||||
)
|
||||
// Rewrite the request path
|
||||
r.Request.URL.Path = proxyToPath
|
||||
// Log the proxy operation
|
||||
g.Log().Infof(r.Context(), `proxy:"%s" -> backend:"%s"`, originalPath, proxyToPath)
|
||||
// Ensure request body can be read multiple times if needed
|
||||
r.MakeBodyRepeatableRead(false)
|
||||
// Forward the request to the backend server
|
||||
proxy.ServeHTTP(r.Response.Writer, r.Request)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func init() {
|
||||
if config.Config.Middleware.Authority.Enable {
|
||||
g.Server().BindMiddleware("/admin/*/open/*", BaseAuthorityMiddlewareOpen)
|
||||
@@ -47,6 +79,7 @@ func init() {
|
||||
if config.Config.Middleware.Log.Enable {
|
||||
g.Server().BindMiddleware("/admin/*", BaseLog)
|
||||
}
|
||||
StartServerProxy()
|
||||
tt := rpc.CServer()
|
||||
|
||||
g.Server().BindHandler("/rpc/*", func(r *ghttp.Request) {
|
||||
|
||||
Reference in New Issue
Block a user