golang-gin使用中间件处理文本-时间字符串格式

一、问题

面临的问题:需要对已有业务代码修改时间格式,原本的时间格式里面带了一个T。因为时间分散在各处,又没有统一的管理入口。这面临着大量的代码修改,于是想找一个没有那么大修改量的方式。

二、思路

思考了一下,两个地方是肯定会经过的,一个是JSON转化的地方,一个是中间件。

  1. 在JSON序列化处修改。

如果能添加一个类似Python的encoder之类的东西,那么就可以解决所有的日期的序列化。可惜在一番寻找后,并没有发现这样的东西。其间发现gorm支持在tag上添加序列化,但发现gin是直接使用的原生的json模块,并没有走gorm的逻辑。而原生的json没有统一的修改序列化的地方。

  1. 在中间件处修改。

这个是gin支持的中间件机制。之前有使用过。

  1. monkey patch到time模块上,修改其序列化方法。

直接monkey patch修改原生time类型的序列化方法。这类方法比较脏,不确定是否可能有潜在风险。

三、日志

我上手尝试了一下中间件机制。发现可行就没有再继续验证其它方法。

  1. 创建一个简单的REST API
Go 复制代码
package main
import (
    "regexp"
    "time"
    "github.com/gin-gonic/gin"
)
type StructA struct {
    FieldA string    `form:"field_a"  binding:"required"`
    Ctime  time.Time `form:"ctime" binding:"required"`
}
type StructB struct {
    // 嵌套结构体
    StructA
    FieldB string `form:"field_b"`
}
func GetB(c *gin.Context) {
    // 绑定表单数据至自定义结构体
    var b StructB
    b.Ctime = time.Now()
    c.JSON(200, b)
}
func main() {
    r := gin.Default()
    r.GET("/getb", GetB)
    r.Run()
}
  1. 添加中间件
Go 复制代码
type copyWriter struct {
    gin.ResponseWriter
}
func (cw copyWriter) Write(b []byte) (int, error) {
    // time_re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}\.\d+)([+-]\d{2}:\d{2})`)
    time_re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}\.\d{2})\d*([+-]\d{2}:\d{2})`)
    // time_re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})`)
    s := string(b)
    result := time_re.ReplaceAllString(s, "$1 $2")
    return cw.ResponseWriter.WriteString(result)
}

func main() {
    r := gin.Default()
    r.Use(func(ctx *gin.Context) {
        cw := &copyWriter{ResponseWriter: ctx.Writer}
        ctx.Writer = cw
        ctx.Next()
        // read data in buf
        // do something for data
        // write to ResponseWriter
    })
    r.GET("/getb", GetB)
    r.Run()
}

其中提供了几种不同输出时间格式的正则:

Go 复制代码
    // time_re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}\.\d+)([+-]\d{2}:\d{2})`)
    time_re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}\.\d{2})\d*([+-]\d{2}:\d{2})`)
    // time_re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})`)
  1. 输出结果如下:
Go 复制代码
{
    "FieldA": "",
    "Ctime": "2024-08-19 19:33:42.75",
    "FieldB": ""
}

还可以根据需要,在中间件中增加过滤条件。例如,在仅支持json格式的过滤,防止将文件下载等里的日期也替换掉。

  1. 完整代码
Go 复制代码
package main
import (
    "regexp"
    "time"
    "github.com/gin-gonic/gin"
)
type StructA struct {
    FieldA string    `form:"field_a"  binding:"required"`
    Ctime  time.Time `form:"ctime" binding:"required"`
}
type StructB struct {
    // 嵌套结构体
    StructA
    FieldB string `form:"field_b"`
}
func GetB(c *gin.Context) {
    // 绑定表单数据至自定义结构体
    var b StructB
    b.Ctime = time.Now()
    c.JSON(200, b)
}
type copyWriter struct {
    gin.ResponseWriter
}
func (cw copyWriter) Write(b []byte) (int, error) {
    // time_re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}\.\d+)([+-]\d{2}:\d{2})`)
    time_re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}\.\d{2})\d*([+-]\d{2}:\d{2})`)
    // time_re := regexp.MustCompile(`(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})`)
    s := string(b)
    result := time_re.ReplaceAllString(s, "$1 $2")
    return cw.ResponseWriter.WriteString(result)
}
func main() {
    r := gin.Default()
    // 创建一个middleware函数,替换返回中的字符串
    r.Use(func(ctx *gin.Context) {
        cw := &copyWriter{ResponseWriter: ctx.Writer}
        ctx.Writer = cw
        ctx.Next()
        // read data in buf
        // do something for data
        // write to ResponseWriter
    })
    r.GET("/getb", GetB)
    r.Run()
}

四、总结

中间件是典型的面向层的,而不是面向对象的思想的产物。利用gin的中间件,基本上可以完成大部分的统一的文本处理需求。

引用:

How to rewrite response body in middleware? · Issue #3384 · gin-gonic/gin (github.com)

相关推荐
源代码•宸15 分钟前
Golang语法进阶(Context)
开发语言·后端·算法·golang·context·withvalue·withcancel
源代码•宸16 分钟前
Golang语法进阶(Sync、Select)
开发语言·经验分享·后端·算法·golang·select·pool
yuanmenghao23 分钟前
自动驾驶中间件iceoryx - 同步与通知机制(一)
开发语言·网络·驱动开发·中间件·自动驾驶
IT=>小脑虎12 小时前
Go语言零基础小白学习知识点【基础版详解】
开发语言·后端·学习·golang
源代码•宸13 小时前
Golang语法进阶(并发概述、Goroutine、Channel)
服务器·开发语言·后端·算法·golang·channel·goroutine
WayneJoon.H13 小时前
2023CISCN go_session
网络安全·golang·ctf·代码审计·ciscn
-曾牛14 小时前
Yak语言核心基础:语句、变量与表达式详解
数据库·python·网络安全·golang·渗透测试·安全开发·yak
源代码•宸1 天前
Leetcode—1123. 最深叶节点的最近公共祖先【中等】
经验分享·算法·leetcode·职场和发展·golang·dfs
源代码•宸1 天前
Golang基础语法(go语言error、go语言defer、go语言异常捕获、依赖管理、Go Modules命令)
开发语言·数据库·后端·算法·golang·defer·recover
optimistic_chen1 天前
【Redis 系列】持久化特性
linux·数据库·redis·分布式·中间件·持久化