From 146744864e7f33ec42ce7ae3f463bccb94f879d3 Mon Sep 17 00:00:00 2001 From: tangjiawei Date: Thu, 5 Dec 2024 20:02:48 +0800 Subject: [PATCH 1/4] =?UTF-8?q?#2473=20WeCube=E6=97=A5=E5=BF=97=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform-core/api/api.go | 31 ++++++++++++++++++ platform-core/api/middleware/response.go | 2 ++ platform-gateway/api/middleware/log.go | 32 ++++++++++++++++++- platform-gateway/api/middleware/redirect.go | 32 ------------------- .../api/support/redirect_support.go | 19 ++--------- platform-gateway/build/config/default.json | 5 +++ platform-gateway/build/start.sh | 3 ++ platform-gateway/docker-compose.tpl | 3 ++ platform-gateway/model/config.go | 7 ++++ 9 files changed, 85 insertions(+), 49 deletions(-) diff --git a/platform-core/api/api.go b/platform-core/api/api.go index 81fc7ee250..4350ae7543 100644 --- a/platform-core/api/api.go +++ b/platform-core/api/api.go @@ -321,6 +321,7 @@ func httpLogHandle() gin.HandlerFunc { c.Set(models.ContextRequestBody, string(bodyBytes)) } apiCode := apiCodeMap[c.Request.Method+"_"+c.FullPath()] + c.Writer.Header().Add("Api-Code", apiCode) log.AccessLogger.Info(fmt.Sprintf("[%s] [%s] ->", requestId, transactionId), log.String("uri", c.Request.RequestURI), log.String("serviceCode", apiCode), log.String("method", c.Request.Method), log.String("sourceIp", getRemoteIp(c)), log.String(models.ContextOperator, c.GetString(models.ContextOperator)), log.String(models.ContextRequestBody, c.GetString(models.ContextRequestBody))) c.Next() costTime := time.Since(start).Seconds() * 1000 @@ -359,3 +360,33 @@ func healthCheck(c *gin.Context) { middleware.ReturnSuccess(c) } } + +// 自定义响应写入器 +type customResponseWriter struct { + gin.ResponseWriter + headers http.Header + written bool +} + +func (w *customResponseWriter) WriteHeader(code int) { + w.ResponseWriter.WriteHeader(code) + w.written = true +} + +func (w *customResponseWriter) Header() http.Header { + if w.headers == nil { + w.headers = make(http.Header) + for k, v := range w.ResponseWriter.Header() { + w.headers[k] = v + } + } + return w.headers +} + +func (w *customResponseWriter) Write(b []byte) (int, error) { + if !w.written { + w.ResponseWriter.WriteHeader(http.StatusOK) + w.written = true + } + return w.ResponseWriter.Write(b) +} diff --git a/platform-core/api/middleware/response.go b/platform-core/api/middleware/response.go index b5f06579d7..542df49134 100644 --- a/platform-core/api/middleware/response.go +++ b/platform-core/api/middleware/response.go @@ -7,6 +7,7 @@ import ( "github.com/WeBankPartners/wecube-platform/platform-core/models" "github.com/gin-gonic/gin" "net/http" + "strconv" ) const AcceptLanguageHeader = "Accept-Language" @@ -84,6 +85,7 @@ func ReturnError(c *gin.Context, err error) { c.Set(models.ContextErrorKey, errorKey) c.Set(models.ContextErrorCode, errorCode) c.Set(models.ContextErrorMessage, errorMessage) + c.Writer.Header().Add("Error-Code", strconv.Itoa(errorCode)) c.JSON(http.StatusOK, returnObj) } diff --git a/platform-gateway/api/middleware/log.go b/platform-gateway/api/middleware/log.go index f7283a78e1..e6dbc6a938 100644 --- a/platform-gateway/api/middleware/log.go +++ b/platform-gateway/api/middleware/log.go @@ -29,7 +29,37 @@ func HttpLogHandle() gin.HandlerFunc { } c.Next() costTime := time.Now().Sub(start).Seconds() * 1000 - log.AccessLogger.Info(fmt.Sprintf("Got request -"), log.String("url", c.Request.RequestURI), log.String("method", c.Request.Method), log.Int("code", c.Writer.Status()), log.String("operator", c.GetString("user")), log.String("ip", getRemoteIp(c)), log.Float64("cost_ms", costTime), log.String("body", c.GetString("responseBody"))) + errCode := c.Writer.Header().Get("Error-Code") + if c.Writer.Status() == 200 { + // 状态码 200 需要区分是否为业务错误 + if strings.TrimSpace(errCode) == "" { + // errCode 为空,表示请求正常,对应errCode=0 + errCode = "0" + } else { + var subCode string + if strings.HasPrefix(c.Request.RequestURI, "/platform") { + subCode = model.Config.SubSystemCode.Core + } else if strings.HasPrefix(c.Request.RequestURI, "/auth") { + subCode = model.Config.SubSystemCode.Auth + } else { + subCode = model.Config.SubSystemCode.Plugin + } + // 业务错误码 以1开头表示技术类错误,其他则是业务类错误 + if strings.HasPrefix(errCode, "1") { + // 业务错误 + errCode = subCode + fmt.Sprintf("T%s", errCode) + } else { + // 非业务错误 + errCode = subCode + fmt.Sprintf("B%s", errCode) + } + } + } else { + // 状态码 不为200 都是技术错误,T表示技术类错误 + errCode = model.Config.SubSystemCode.Core + fmt.Sprintf("T00000%d", c.Writer.Status()) + } + log.AccessLogger.Info(fmt.Sprintf("Got request -"), log.String("apiCode", c.Writer.Header().Get("Api-Code")), log.String("method", c.Request.Method), + log.Int("code", c.Writer.Status()), log.String("errCode", errCode), log.String("operator", c.GetString("user")), log.String("ip", getRemoteIp(c)), log.Float64("cost_ms", costTime), + log.String("body", c.GetString("responseBody"))) } } diff --git a/platform-gateway/api/middleware/redirect.go b/platform-gateway/api/middleware/redirect.go index f0529f64b6..53f8bae611 100644 --- a/platform-gateway/api/middleware/redirect.go +++ b/platform-gateway/api/middleware/redirect.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/WeBankPartners/wecube-platform/platform-gateway/api/support" "github.com/WeBankPartners/wecube-platform/platform-gateway/common/log" - "github.com/WeBankPartners/wecube-platform/platform-gateway/common/network" "github.com/WeBankPartners/wecube-platform/platform-gateway/model" "github.com/gin-gonic/gin" "math/rand" @@ -78,27 +77,6 @@ func Redirect() gin.HandlerFunc { break } } - //validIdx := -1 - //for i := range rules { - // targetUrl := rules[i].TargetPath + uri - // invoke := support.RedirectInvoke{ - // TargetUrl: targetUrl, - // } - // err := invoke.Do(c) - // if err != nil { - // log.Logger.Warn("failed to request", log.String("targetUrl", targetUrl), log.Error(err)) - // } else { - // validIdx = i - // break - // } - //} - // - //if validIdx > 0 { - // tmp := rules[validIdx] - // rules[validIdx] = rules[0] - // rules[0] = tmp - // redirectRuleMap.Store(requestKey, rules) - //} if len(rules) > 0 { } else { @@ -111,16 +89,6 @@ func Redirect() gin.HandlerFunc { } } -func isRemoteRejected(err error) bool { - if network.IsNetworkTimeout(err) { - return true - } - if network.IsConnReset(err) { - return true - } - return false -} - func AddRedirectRule(context string, rules []RedirectRule) { log.Logger.Info("add redirect route context: " + context) diff --git a/platform-gateway/api/support/redirect_support.go b/platform-gateway/api/support/redirect_support.go index 7232808f98..0acc34ac6e 100644 --- a/platform-gateway/api/support/redirect_support.go +++ b/platform-gateway/api/support/redirect_support.go @@ -27,15 +27,6 @@ func (invoke RedirectInvoke) Do(c *gin.Context) error { log.Logger.Info(fmt.Sprintf("Redirecting request to downstream system: [Method: %s] [URL: %s] [ContentLength: %d]", c.Request.Method, invoke.TargetUrl, c.Request.ContentLength)) cloneRequest := c.Request.Clone(c.Request.Context()) // deep copy original request - - /* if invoke.RequestHandler != nil { - log.Logger.Info(fmt.Sprintf("Start to handle request [%s]-[%s]", c.GetHeader(constant.TransactionId), c.GetHeader(constant.RequestId))) - if err := invoke.RequestHandler(cloneRequest, c); err != nil { - ReturnError(c, exterror.Catch(exterror.New().ServerHandleError, fmt.Errorf("failed handle request: %s", err.Error()))) - return - } - } - */ newRequest, _ := http.NewRequest(cloneRequest.Method, invoke.TargetUrl, cloneRequest.Body) newRequest.Header = cloneRequest.Header // pass through content length @@ -44,7 +35,6 @@ func (invoke RedirectInvoke) Do(c *gin.Context) error { newRequest.Header.Set("X-Forwarded-For", clientIp) } newRequest.URL.RawQuery = cloneRequest.URL.RawQuery - //auth.SetRequestSourceAuth(newRequest, config.Config.Auth.Source.AppId, config.Config.Auth.Source.PrivateKeyBytes) client := &http.Client{ Timeout: 30 * time.Minute, @@ -70,15 +60,12 @@ func (invoke RedirectInvoke) Do(c *gin.Context) error { responseDump, _ := httputil.DumpResponse(response, true) log.Logger.Debug(fmt.Sprintf("Response from downstream system: %s [body size]: %d", string(responseDump), len(respBody))) } - - /* if response.StatusCode >= 400 { - ReturnError(c, fmt.Errorf("error from downstream system: %s", response.Status)) - return - }*/ - respHeader := c.Writer.Header() for k, v := range response.Header { respHeader[k] = v + if (k == "Api-Code" || k == "Error-Code") && len(v) > 0 { + c.Writer.Header().Add(k, v[0]) + } } c.Data(response.StatusCode, response.Header.Get("Content-Type"), respBody) diff --git a/platform-gateway/build/config/default.json b/platform-gateway/build/config/default.json index d5d8a53cc4..537c1acf38 100644 --- a/platform-gateway/build/config/default.json +++ b/platform-gateway/build/config/default.json @@ -28,5 +28,10 @@ ], "proxy_config": { "timeout":[#HTTP_TIMEOUT_MINUTE] + }, + "sub_system_code": { + "auth":"[#WECUBE_SUB_SYSTEM_AUTH_CODE]", + "core":"[#WECUBE_SUB_SYSTEM_CORE_CODE]", + "plugin":"[#WECUBE_SUB_SYSTEM_PLUGIN_CODE]" } } \ No newline at end of file diff --git a/platform-gateway/build/start.sh b/platform-gateway/build/start.sh index 5af9e9755a..46c10cb162 100644 --- a/platform-gateway/build/start.sh +++ b/platform-gateway/build/start.sh @@ -6,5 +6,8 @@ sed -i "s~\[#GATEWAY_ROUTE_CONFIG_URI\]~$GATEWAY_ROUTE_CONFIG_URI~g" /app/platfo sed -i "s~\[#WECUBE_CORE_ADDRESS\]~$WECUBE_CORE_ADDRESS~g" /app/platform-gateway/config/default.json sed -i "s~\[#AUTH_SERVER_ADDRESS\]~$AUTH_SERVER_ADDRESS~g" /app/platform-gateway/config/default.json sed -i "s~\[#HTTP_TIMEOUT_MINUTE\]~$HTTP_TIMEOUT_MINUTE~g" /app/platform-gateway/config/default.json +sed -i "s~\[#WECUBE_SUB_SYSTEM_AUTH_CODE\]~$WECUBE_SUB_SYSTEM_AUTH_CODE~g" /app/platform-gateway/config/default.json +sed -i "s~\[#WECUBE_SUB_SYSTEM_CORE_CODE\]~$WECUBE_SUB_SYSTEM_CORE_CODE~g" /app/platform-gateway/config/default.json +sed -i "s~\[#WECUBE_SUB_SYSTEM_PLUGIN_CODE\]~$WECUBE_SUB_SYSTEM_PLUGIN_CODE~g" /app/platform-gateway/config/default.json exec ./platform-gateway \ No newline at end of file diff --git a/platform-gateway/docker-compose.tpl b/platform-gateway/docker-compose.tpl index c872a6c005..0f9dba14bc 100644 --- a/platform-gateway/docker-compose.tpl +++ b/platform-gateway/docker-compose.tpl @@ -16,3 +16,6 @@ services: - WECUBE_CORE_ADDRESS=[#WECUBE_CORE_ADDRESS] - AUTH_SERVER_ADDRESS=[#AUTH_SERVER_ADDRESS] - HTTP_TIMEOUT_MINUTE=[#HTTP_TIMEOUT_MINUTE] + - WECUBE_SUB_SYSTEM_AUTH_CODE=[#WECUBE_SUB_SYSTEM_AUTH_CODE] + - WECUBE_SUB_SYSTEM_CORE_CODE=[#WECUBE_SUB_SYSTEM_CORE_CODE] + - WECUBE_SUB_SYSTEM_PLUGIN_CODE=[#WECUBE_SUB_SYSTEM_PLUGIN_CODE] diff --git a/platform-gateway/model/config.go b/platform-gateway/model/config.go index 1d01f5148d..b9837eb353 100644 --- a/platform-gateway/model/config.go +++ b/platform-gateway/model/config.go @@ -43,6 +43,12 @@ type RedirectRoute struct { TargetPath string `json:"target_path"` } +type SubSystemCode struct { + Auth string `json:"auth"` + Core string `json:"core"` + Plugin string `json:"plugin"` +} + type GlobalConfig struct { ServerAddress string `json:"server_address"` ServerPort string `json:"server_port"` @@ -51,6 +57,7 @@ type GlobalConfig struct { Remote RemoteServiceConfig `json:"remote_service"` ProxyConfig ProxyConfig `json:"proxy_config"` RedirectRoutes []RedirectRoute `json:"redirect_routes"` + SubSystemCode SubSystemCode `json:"sub_system_code"` } var ( From 3e2195f92f2e94574c2943a0ec0f25db230056b2 Mon Sep 17 00:00:00 2001 From: tangjiawei Date: Thu, 5 Dec 2024 20:09:28 +0800 Subject: [PATCH 2/4] =?UTF-8?q?#2473=20WeCube=E6=97=A5=E5=BF=97=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform-gateway/api/middleware/log.go | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/platform-gateway/api/middleware/log.go b/platform-gateway/api/middleware/log.go index e6dbc6a938..14451a68a3 100644 --- a/platform-gateway/api/middleware/log.go +++ b/platform-gateway/api/middleware/log.go @@ -30,20 +30,24 @@ func HttpLogHandle() gin.HandlerFunc { c.Next() costTime := time.Now().Sub(start).Seconds() * 1000 errCode := c.Writer.Header().Get("Error-Code") + apiCode := c.Writer.Header().Get("Api-Code") + var subCode string + if strings.HasPrefix(c.Request.RequestURI, "/platform") { + subCode = model.Config.SubSystemCode.Core + apiCode = fmt.Sprintf("platform_%s", apiCode) + } else if strings.HasPrefix(c.Request.RequestURI, "/auth") { + subCode = model.Config.SubSystemCode.Auth + apiCode = fmt.Sprintf("auth_%s", apiCode) + } else { + subCode = model.Config.SubSystemCode.Plugin + apiCode = fmt.Sprintf("plugin_%s", apiCode) + } if c.Writer.Status() == 200 { // 状态码 200 需要区分是否为业务错误 if strings.TrimSpace(errCode) == "" { // errCode 为空,表示请求正常,对应errCode=0 errCode = "0" } else { - var subCode string - if strings.HasPrefix(c.Request.RequestURI, "/platform") { - subCode = model.Config.SubSystemCode.Core - } else if strings.HasPrefix(c.Request.RequestURI, "/auth") { - subCode = model.Config.SubSystemCode.Auth - } else { - subCode = model.Config.SubSystemCode.Plugin - } // 业务错误码 以1开头表示技术类错误,其他则是业务类错误 if strings.HasPrefix(errCode, "1") { // 业务错误 @@ -57,7 +61,7 @@ func HttpLogHandle() gin.HandlerFunc { // 状态码 不为200 都是技术错误,T表示技术类错误 errCode = model.Config.SubSystemCode.Core + fmt.Sprintf("T00000%d", c.Writer.Status()) } - log.AccessLogger.Info(fmt.Sprintf("Got request -"), log.String("apiCode", c.Writer.Header().Get("Api-Code")), log.String("method", c.Request.Method), + log.AccessLogger.Info(fmt.Sprintf("Got request -"), log.String("apiCode", apiCode), log.String("method", c.Request.Method), log.Int("code", c.Writer.Status()), log.String("errCode", errCode), log.String("operator", c.GetString("user")), log.String("ip", getRemoteIp(c)), log.Float64("cost_ms", costTime), log.String("body", c.GetString("responseBody"))) From f82319e9b9a0b91d27ee086aaa50b627ad874540 Mon Sep 17 00:00:00 2001 From: tangjiawei Date: Thu, 5 Dec 2024 20:46:22 +0800 Subject: [PATCH 3/4] =?UTF-8?q?#2473=20WeCube=E6=97=A5=E5=BF=97=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform-gateway/api/middleware/log.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform-gateway/api/middleware/log.go b/platform-gateway/api/middleware/log.go index 14451a68a3..e8c1dcf725 100644 --- a/platform-gateway/api/middleware/log.go +++ b/platform-gateway/api/middleware/log.go @@ -61,7 +61,7 @@ func HttpLogHandle() gin.HandlerFunc { // 状态码 不为200 都是技术错误,T表示技术类错误 errCode = model.Config.SubSystemCode.Core + fmt.Sprintf("T00000%d", c.Writer.Status()) } - log.AccessLogger.Info(fmt.Sprintf("Got request -"), log.String("apiCode", apiCode), log.String("method", c.Request.Method), + log.AccessLogger.Info(fmt.Sprintf("Got request -"), log.String("url", c.Request.RequestURI), log.String("apiCode", apiCode), log.String("method", c.Request.Method), log.Int("code", c.Writer.Status()), log.String("errCode", errCode), log.String("operator", c.GetString("user")), log.String("ip", getRemoteIp(c)), log.Float64("cost_ms", costTime), log.String("body", c.GetString("responseBody"))) From a1e35da1aac9ca47484fa64b176b09bd36b5ee48 Mon Sep 17 00:00:00 2001 From: tangjiawei Date: Fri, 6 Dec 2024 13:08:53 +0800 Subject: [PATCH 4/4] =?UTF-8?q?#2473=20WeCube=E6=97=A5=E5=BF=97=E8=A7=84?= =?UTF-8?q?=E8=8C=83=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platform-auth-server/api/middleware/log.go | 49 ------------------ platform-auth-server/api/routers.go | 51 +++++++++++++++++-- platform-auth-server/api/support/response.go | 2 + platform-auth-server/common/exterror/error.go | 5 -- platform-core/api/api.go | 30 ----------- platform-gateway/api/middleware/log.go | 31 +++++++---- 6 files changed, 69 insertions(+), 99 deletions(-) delete mode 100644 platform-auth-server/api/middleware/log.go diff --git a/platform-auth-server/api/middleware/log.go b/platform-auth-server/api/middleware/log.go deleted file mode 100644 index 93dd5db501..0000000000 --- a/platform-auth-server/api/middleware/log.go +++ /dev/null @@ -1,49 +0,0 @@ -package middleware - -import ( - "bytes" - "github.com/WeBankPartners/wecube-platform/platform-auth-server/common/log" - "github.com/WeBankPartners/wecube-platform/platform-auth-server/model" - "io/ioutil" - "net/http/httputil" - "strings" - "time" - - "github.com/gin-gonic/gin" -) - -func HttpLogHandle() gin.HandlerFunc { - return func(c *gin.Context) { - start := time.Now() - bodyBytes, _ := ioutil.ReadAll(c.Request.Body) - c.Request.Body.Close() - c.Request.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes)) - c.Set("requestBody", string(bodyBytes)) - log.Logger.Info("Received request ", log.String("url", c.Request.RequestURI), log.String("method", c.Request.Method), log.String("body", c.GetString("requestBody"))) - if strings.EqualFold(model.Config.Log.Level, "debug") { - requestDump, _ := httputil.DumpRequest(c.Request, true) - log.Logger.Debug("Received request: " + string(requestDump)) - } - c.Next() - costTime := time.Since(start).Seconds() * 1000 - log.AccessLogger.Info("Got request -", log.String("url", c.Request.RequestURI), log.String("method", c.Request.Method), log.Int("code", c.Writer.Status()), log.String("operator", c.GetString("user")), log.String("ip", getRemoteIp(c)), log.Float64("cost_ms", costTime), log.String("body", c.GetString("responseBody"))) - - } -} - -func getRemoteIp(c *gin.Context) string { - netIp := c.RemoteIP() - if len(netIp) > 0 { - return netIp - } - return c.ClientIP() -} - -func RecoverHandle(c *gin.Context, err interface{}) { - var errorMessage string - if err != nil { - errorMessage = err.(error).Error() - } - log.Logger.Error("Handle error", log.Int("errorCode", 1), log.String("message", errorMessage)) - //c.JSON(http.StatusInternalServerError, model.ResponseWrap{ErrorCode: 10400001, ErrorMessage: errorMessage}) -} diff --git a/platform-auth-server/api/routers.go b/platform-auth-server/api/routers.go index 916aaa1924..f6cad72513 100644 --- a/platform-auth-server/api/routers.go +++ b/platform-auth-server/api/routers.go @@ -1,13 +1,18 @@ package api import ( + "bytes" "fmt" + "io/ioutil" "net/http" + "net/http/httputil" + "strings" + "time" "github.com/WeBankPartners/wecube-platform/platform-auth-server/api/middleware" "github.com/WeBankPartners/wecube-platform/platform-auth-server/common/constant" + "github.com/WeBankPartners/wecube-platform/platform-auth-server/common/log" "github.com/WeBankPartners/wecube-platform/platform-auth-server/model" - "github.com/gin-gonic/gin" ) @@ -35,9 +40,9 @@ func NewRouter() *gin.Engine { router := gin.Default() if model.Config.Log.AccessLogEnable { - router.Use(middleware.HttpLogHandle()) + router.Use(HttpLogHandle()) } - router.Use(gin.CustomRecovery(middleware.RecoverHandle)) + router.Use(gin.CustomRecovery(RecoverHandle)) authMap := buildAuthMap() authoritiesFetcher := func(path string, method string) []string { @@ -61,9 +66,8 @@ func NewRouter() *gin.Engine { case http.MethodDelete: group.DELETE(funcObj.Url, funcObj.HandlerFunc) } - apiCodeMap[fmt.Sprintf("%s_%s", funcObj.Url, funcObj.Method)] = funcObj.ApiCode + apiCodeMap[fmt.Sprintf("%s_%s%s", funcObj.Method, constant.UrlPrefix, funcObj.Url)] = funcObj.ApiCode } - return router } @@ -76,6 +80,43 @@ func buildAuthMap() map[string][]string { return authMap } +func HttpLogHandle() gin.HandlerFunc { + return func(c *gin.Context) { + start := time.Now() + bodyBytes, _ := ioutil.ReadAll(c.Request.Body) + c.Request.Body.Close() + c.Request.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes)) + c.Set("requestBody", string(bodyBytes)) + log.Logger.Info("Received request ", log.String("url", c.Request.RequestURI), log.String("method", c.Request.Method), log.String("body", c.GetString("requestBody"))) + if strings.EqualFold(model.Config.Log.Level, "debug") { + requestDump, _ := httputil.DumpRequest(c.Request, true) + log.Logger.Debug("Received request: " + string(requestDump)) + } + apiCode := apiCodeMap[c.Request.Method+"_"+c.FullPath()] + c.Writer.Header().Add("Api-Code", apiCode) + c.Next() + costTime := time.Since(start).Seconds() * 1000 + log.AccessLogger.Info("Got request -", log.String("url", c.Request.RequestURI), log.String("method", c.Request.Method), log.Int("code", c.Writer.Status()), log.String("operator", c.GetString("user")), log.String("ip", getRemoteIp(c)), log.Float64("cost_ms", costTime), log.String("body", c.GetString("responseBody"))) + } +} + +func getRemoteIp(c *gin.Context) string { + netIp := c.RemoteIP() + if len(netIp) > 0 { + return netIp + } + return c.ClientIP() +} + +func RecoverHandle(c *gin.Context, err interface{}) { + var errorMessage string + if err != nil { + errorMessage = err.(error).Error() + } + log.Logger.Error("Handle error", log.Int("errorCode", 1), log.String("message", errorMessage)) + c.JSON(http.StatusInternalServerError, model.ResponseWrap{ErrorCode: 1, Status: model.ResponseStatusError}) +} + func init() { httpHandlerFuncList = append(httpHandlerFuncList, // contract instance &handlerFuncObj{Url: constant.UriLogin, Method: http.MethodPost, HandlerFunc: Login, diff --git a/platform-auth-server/api/support/response.go b/platform-auth-server/api/support/response.go index 0d551b6ea0..79f4d4dd28 100644 --- a/platform-auth-server/api/support/response.go +++ b/platform-auth-server/api/support/response.go @@ -6,6 +6,7 @@ import ( "github.com/WeBankPartners/wecube-platform/platform-auth-server/common/log" "github.com/WeBankPartners/wecube-platform/platform-auth-server/model" "net/http" + "strconv" "github.com/gin-gonic/gin" ) @@ -44,6 +45,7 @@ func ReturnErrorWithHttpCode(c *gin.Context, err error, httpCode int) { } bodyBytes, _ := json.Marshal(errorResponse) c.Set("responseBody", string(bodyBytes)) + c.Writer.Header().Add("Error-Code", strconv.Itoa(errorResponse.ErrorCode)) c.JSON(httpCode, errorResponse) } diff --git a/platform-auth-server/common/exterror/error.go b/platform-auth-server/common/exterror/error.go index 9b58219cd1..8551fb05b7 100644 --- a/platform-auth-server/common/exterror/error.go +++ b/platform-auth-server/common/exterror/error.go @@ -214,8 +214,3 @@ func buildErrMessage(templateMessage string, params []interface{}) (message stri func IsBusinessErrorCode(errorCode int) bool { return strings.HasPrefix(fmt.Sprintf("%d", errorCode), "2") } - -func IsCustomError(err error) bool { - _, ok := err.(CustomError) - return ok -} diff --git a/platform-core/api/api.go b/platform-core/api/api.go index 4350ae7543..5d028c9eb4 100644 --- a/platform-core/api/api.go +++ b/platform-core/api/api.go @@ -360,33 +360,3 @@ func healthCheck(c *gin.Context) { middleware.ReturnSuccess(c) } } - -// 自定义响应写入器 -type customResponseWriter struct { - gin.ResponseWriter - headers http.Header - written bool -} - -func (w *customResponseWriter) WriteHeader(code int) { - w.ResponseWriter.WriteHeader(code) - w.written = true -} - -func (w *customResponseWriter) Header() http.Header { - if w.headers == nil { - w.headers = make(http.Header) - for k, v := range w.ResponseWriter.Header() { - w.headers[k] = v - } - } - return w.headers -} - -func (w *customResponseWriter) Write(b []byte) (int, error) { - if !w.written { - w.ResponseWriter.WriteHeader(http.StatusOK) - w.written = true - } - return w.ResponseWriter.Write(b) -} diff --git a/platform-gateway/api/middleware/log.go b/platform-gateway/api/middleware/log.go index e8c1dcf725..ea7fb3d7d2 100644 --- a/platform-gateway/api/middleware/log.go +++ b/platform-gateway/api/middleware/log.go @@ -16,6 +16,7 @@ import ( func HttpLogHandle() gin.HandlerFunc { return func(c *gin.Context) { start := time.Now() + var errCode string if strings.EqualFold(model.Config.Log.Level, "debug") && c.Request.RequestURI != "/platform/v1/packages" && !strings.HasSuffix(c.Request.RequestURI, "/packages/upload") { bodyBytes, _ := ioutil.ReadAll(c.Request.Body) c.Request.Body.Close() @@ -29,15 +30,32 @@ func HttpLogHandle() gin.HandlerFunc { } c.Next() costTime := time.Now().Sub(start).Seconds() * 1000 - errCode := c.Writer.Header().Get("Error-Code") apiCode := c.Writer.Header().Get("Api-Code") - var subCode string + // 业务错误码 + var subCode, subErrorCode string + errCode = c.Writer.Header().Get("Error-Code") if strings.HasPrefix(c.Request.RequestURI, "/platform") { subCode = model.Config.SubSystemCode.Core apiCode = fmt.Sprintf("platform_%s", apiCode) + // platform-core 错误码,以1开头表示技术类错误,其他则是业务类错误,本身就是8位,不需要扩展 + if strings.HasPrefix(errCode, "1") { + // 技术错误 + subErrorCode = subCode + fmt.Sprintf("T%s", errCode) + } else { + // 业务错误 + subErrorCode = subCode + fmt.Sprintf("B%s", errCode) + } } else if strings.HasPrefix(c.Request.RequestURI, "/auth") { subCode = model.Config.SubSystemCode.Auth apiCode = fmt.Sprintf("auth_%s", apiCode) + // platform-auth 错误码,以3开头表示业务类错误,其他则是技术类错误,本身4位,需要前面扩展4个0 + if strings.HasPrefix(errCode, "3") { + // 业务错误 + subErrorCode = subCode + fmt.Sprintf("B0000%s", errCode) + } else { + // 非业务错误 + subErrorCode = subCode + fmt.Sprintf("T0000%s", errCode) + } } else { subCode = model.Config.SubSystemCode.Plugin apiCode = fmt.Sprintf("plugin_%s", apiCode) @@ -48,14 +66,7 @@ func HttpLogHandle() gin.HandlerFunc { // errCode 为空,表示请求正常,对应errCode=0 errCode = "0" } else { - // 业务错误码 以1开头表示技术类错误,其他则是业务类错误 - if strings.HasPrefix(errCode, "1") { - // 业务错误 - errCode = subCode + fmt.Sprintf("T%s", errCode) - } else { - // 非业务错误 - errCode = subCode + fmt.Sprintf("B%s", errCode) - } + errCode = subErrorCode } } else { // 状态码 不为200 都是技术错误,T表示技术类错误