feat: rbac迁移完成,并已部署至dev服务器

This commit is contained in:
Blizzard
2026-05-01 01:19:50 +08:00
parent f80a3dc064
commit 8b11068fef
250 changed files with 6314 additions and 13072 deletions
+29 -7
View File
@@ -1,7 +1,9 @@
package handler
import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httputil"
"net/url"
@@ -49,19 +51,38 @@ func NewProxyRouter(upstreams []config.Upstream) *ProxyRouter {
// 自定义 Transport:超时控制
proxy.Transport = &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
IdleConnTimeout: 90 * time.Second,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
IdleConnTimeout: 90 * time.Second,
ResponseHeaderTimeout: 30 * time.Second, // 上游响应头超时,超出则触发 ErrorHandler
}
// 错误处理
prefix := u.Prefix
targetAddr := u.Target
// ErrorHandler:处理网络层错误(连接失败、超时等)
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
logx.Errorf("代理请求失败 [%s%s -> %s]: %v", prefix, r.URL.Path, targetAddr, err)
logx.Errorf("[Gateway] ❌ 上游连接失败 | %s %s -> %s | 错误: %v",
r.Method, r.URL.Path, targetAddr, err)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadGateway)
fmt.Fprintf(w, `{"code":502,"msg":"上游服务不可用: %s"}`, prefix)
fmt.Fprintf(w, `{"code":502,"msg":"上游服务不可用,请检查 %s 是否正常运行"}`, prefix)
}
// ModifyResponse:捕获上游返回的 4xx/5xx 并记录日志(网络层正常但业务异常)
proxy.ModifyResponse = func(resp *http.Response) error {
if resp.StatusCode >= 500 {
// 读取响应体用于日志(最多 1KB,读完后要写回)
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024))
resp.Body = io.NopCloser(bytes.NewBuffer(body))
logx.Errorf("[Gateway] ⚠️ 上游服务异常 | %s %s -> %s | 状态码: %d | 响应: %s",
resp.Request.Method, resp.Request.URL.Path, targetAddr,
resp.StatusCode, string(body))
} else if resp.StatusCode >= 400 {
logx.Infof("[Gateway] ️ 上游返回客户端错误 | %s %s | 状态码: %d",
resp.Request.Method, resp.Request.URL.Path, resp.StatusCode)
}
return nil
}
router.routes = append(router.routes, &route{
@@ -82,13 +103,14 @@ func (pr *ProxyRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
for _, rt := range pr.routes {
if strings.HasPrefix(path, rt.prefix) {
logx.Infof("[Gateway] %s %s -> %s", r.Method, path, rt.target)
logx.Infof("[Gateway] %s %s -> %s", r.Method, path, rt.target)
rt.proxy.ServeHTTP(w, r)
return
}
}
// 没有匹配的路由
logx.Errorf("[Gateway] ❌ 路由未找到: %s %s", r.Method, path)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, `{"code":404,"msg":"路由未找到: %s"}`, path)