chore: fix make lint
Some checks failed
CI Pipeline / Security Scan (push) Successful in 2m44s
CI Pipeline / Lint (push) Successful in 6m47s
CI Pipeline / Lint (pull_request) Successful in 6m38s
CI Pipeline / Security Scan (pull_request) Successful in 3m32s
CI Pipeline / Test (push) Successful in 7m50s
CI Pipeline / Build (push) Failing after 32s
CI Pipeline / Notification (push) Successful in 2s
CI Pipeline / Test (pull_request) Successful in 7m39s
CI Pipeline / Build (pull_request) Failing after 22s
CI Pipeline / Notification (pull_request) Successful in 1s

This commit is contained in:
ulflow_phattt2901 2025-05-26 10:50:43 +07:00
parent 4779071fcd
commit 3aa4f3532b
2 changed files with 16 additions and 24 deletions

View File

@ -24,32 +24,20 @@ func Logger() gin.HandlerFunc {
path := c.Request.URL.Path
// Ghi log
logMessage := "[GIN] " + time.Now().Format("2006/01/02 - 15:04:05") +
" | " + method +
" | " + path +
" | " + latency.String() +
" | " + c.ClientIP() +
" | " + c.Request.UserAgent()
if status >= 400 {
// Log lỗi
// Ở đây bạn có thể sử dụng logger của dự án thay vì in ra console
// Ví dụ: logger.Error("Request failed", "method", method, "path", path, "status", status, "latency", latency)
gin.DefaultErrorWriter.Write([]byte(
"[GIN] " + time.Now().Format("2006/01/02 - 15:04:05") +
" | " + method +
" | " + path +
" | " + latency.String() +
" | " + c.ClientIP() +
" | " + c.Request.UserAgent() +
" | " + c.Errors.ByType(gin.ErrorTypePrivate).String() +
"\n",
))
logMessage += " | " + c.Errors.ByType(gin.ErrorTypePrivate).String()
_, _ = gin.DefaultErrorWriter.Write([]byte(logMessage + "\n"))
} else {
// Log thông thường
// Ví dụ: logger.Info("Request processed", "method", method, "path", path, "status", status, "latency", latency)
gin.DefaultWriter.Write([]byte(
"[GIN] " + time.Now().Format("2006/01/02 - 15:04:05") +
" | " + method +
" | " + path +
" | " + latency.String() +
" | " + c.ClientIP() +
" | " + c.Request.UserAgent() +
"\n",
))
_, _ = gin.DefaultWriter.Write([]byte(logMessage + "\n"))
}
}
}

View File

@ -41,7 +41,10 @@ func TestCORS(t *testing.T) {
client := &http.Client{}
resp, err := client.Do(req)
assert.NoError(t, err)
defer resp.Body.Close()
defer func() {
err := resp.Body.Close()
assert.NoError(t, err, "Failed to close response body")
}()
assert.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin"), "CORS header not set correctly")
})
@ -66,5 +69,6 @@ func TestRateLimit(t *testing.T) {
resp, err := http.Get(ts.URL)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
resp.Body.Close()
err = resp.Body.Close()
assert.NoError(t, err, "Failed to close response body")
}