1 Star 0 Fork 0

数字证书签名及管理系统 / backend

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
logfmt_filter.go 2.38 KB
一键复制 编辑 原始数据 按行查看 历史
ivfzhou 提交于 2024-04-14 21:44 . feat: 登出
/*
* Copyright (c) 2023 ivfzhou
* backend is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/
package filter
import (
"bytes"
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"gitee.com/CertificateAndSigningManageSystem/common/ctxs"
"gitee.com/CertificateAndSigningManageSystem/common/log"
"gitee.com/CertificateAndSigningManageSystem/common/util"
)
// LogfmtFilter 日志过滤器
func LogfmtFilter(c *gin.Context) {
// 组装上下信息
rid := c.Request.Header.Get("X-Csms-Request-Id")
if len(rid) <= 0 {
rid = strings.ReplaceAll(uuid.New().String(), "-", "")
}
ctx := ctxs.WithRequestId(c.Request.Context(), rid)
reqPath := strings.ToLower(c.Request.URL.Path)
ctx = ctxs.WithRequestPath(ctx, reqPath)
ip := c.Request.Header.Get("X-Real-IP")
if len(ip) <= 0 {
ip = c.ClientIP()
}
ctx = ctxs.WithRequestIP(ctx, ip)
c.Request = c.Request.WithContext(ctx)
// 打印请求信息
ct := c.Request.Header.Get(http.CanonicalHeaderKey("Content-Type"))
cl := c.Request.Header.Get("Content-Length")
log.Info(ctx, "START PROCESS", c.Request.Method, cl, ct, c.Request.URL.RawQuery)
clNum, _ := strconv.ParseInt(cl, 10, 64)
if s := strings.ToLower(ct); strings.HasPrefix(s, "application/json") ||
strings.HasPrefix(s, "application/x-www-form-urlencoded") && clNum < 5*1024 {
reqBody, _ := io.ReadAll(c.Request.Body)
util.CloseIO(ctx, c.Request.Body)
log.Info(ctx, "reqBody is", string(reqBody))
c.Request.Body = io.NopCloser(bytes.NewReader(reqBody))
}
// 打印响应信息
now := time.Now()
defer func() {
cost := time.Since(now)
ctx = c.Request.Context()
msg := ctxs.ErrMsg(ctx)
if len(msg) > 0 {
log.Warn(ctx, "END PROCESS",
cost, c.Writer.Status(), http.StatusText(c.Writer.Status()),
c.Writer.Header().Get("Content-Length"), msg)
} else {
log.Info(ctx, "END PROCESS",
cost, c.Writer.Status(), http.StatusText(c.Writer.Status()),
c.Writer.Header().Get("Content-Length"))
}
}()
c.Next()
}
1
https://gitee.com/CertificateAndSigningManageSystem/backend.git
git@gitee.com:CertificateAndSigningManageSystem/backend.git
CertificateAndSigningManageSystem
backend
backend
dc75ea3ac2c6

搜索帮助