程序员的快乐如此简单

最近在GitHub上发起了一个关于Beego框架的小插件的开源仓库,这一举动虽然看似微小,但其中的快乐和意义却是无法用言语表达的。

Beego是一个开源的Go语言Web框架,它采用了MVC架构模式,并集成了很多常用的功能和中间件。小插件是指与Beego框架配套使用的、可扩展的、独立的软件模块或组件。通过开发小插件,可以扩展Beego框架的功能,提高开发效率和代码可维护性。

Beego-Requestid是一种中间件(Middleware),用于在处理HTTP请求时,为每个请求生成一个唯一的ID,并将其附加到请求上下文中。这样,您可以在应用程序的其他地方方便地访问这个唯一的请求ID,从而更好地跟踪和调试应用程序。

使用RequestId中间件可以帮助您解决以下问题:

  1. 跟踪请求:通过在每个请求中添加唯一的ID,您可以轻松地在应用程序中跟踪请求的来源和路径。这对于调试和性能优化非常有用。
  2. 日志记录:您可以使用请求ID将日志记录与特定请求关联起来。这样,当您在日志中查找特定请求的信息时,可以更方便地定位相关的日志条目。
  3. 错误处理:如果应用程序中出现错误,请求ID可以帮助您识别是哪个请求引发了错误。这对于故障排查和问题报告非常有帮助。
代码

话不多说直接看源码:

go 复制代码
package beego_requestid

import (
	"github.com/beego/beego"
	"github.com/beego/beego/context"
	"github.com/google/uuid"
)

const DefaultHeaderReqIdKey = "X-Request-Id"

type Option func(config *Config)

type GenRequestIdFunc func() string

type Config struct {
	genRequestIdFunc               GenRequestIdFunc
	headerReqIdKey, customReqIdKey string
}

func NewFilter(opts ...Option) beego.FilterFunc {
	cnf := &Config{
		genRequestIdFunc: DefaultGenRequestIdFunc,
		headerReqIdKey:   DefaultHeaderReqIdKey,
	}

	for _, opt := range opts {
		opt(cnf)
	}

	return func(c *context.Context) {
		reqId := c.Request.Header.Get(cnf.headerReqIdKey)
		if reqId == "" {
			reqId = cnf.genRequestIdFunc()
			c.Request.Header.Add(cnf.headerReqIdKey, reqId)
		}
		if cnf.customReqIdKey != "" {
			c.Input.SetData(cnf.customReqIdKey, reqId)
		}
	}
}

func WithGenRequestIdFunc(genFunc GenRequestIdFunc) Option {
	return func(config *Config) {
		config.genRequestIdFunc = genFunc
	}
}

func WithHeaderReqIdKey(key string) Option {
	return func(config *Config) {
		config.headerReqIdKey = key
	}
}

func WithCustomReqIdKey(key string) Option {
	return func(config *Config) {
		config.customReqIdKey = key
	}
}

func DefaultGenRequestIdFunc() string {
	return uuid.NewString()
}

使用示例:

go 复制代码
package main

import (
	"log"
	"time"

	"github.com/spf13/cast"
	"github.com/beego/beego"
	"github.com/beego/beego/context"
	beego_requestid "github.com/ibarryyan/beego-requestid"
)

func example1() {
	beego.InsertFilter("/*", beego.BeforeRouter, beego_requestid.NewFilter())

	beego.Get("/hello", func(c *context.Context) {
		reqId := c.Request.Header.Get("X-Request-Id")
		log.Printf("reqestid = %s", reqId)

		_, _ = c.ResponseWriter.Write([]byte("hello..."))
		return
	})

	beego.Run(":9900")
}

func example2() {
	beego.InsertFilter("/*", beego.BeforeRouter, beego_requestid.NewFilter(
		beego_requestid.WithGenRequestIdFunc(func() string {
			return cast.ToString(time.Now().Unix())
		}),
		beego_requestid.WithHeaderReqIdKey("my_header_reqid"),
		beego_requestid.WithCustomReqIdKey("my_reqid"),
	))

	beego.Get("/hello", func(c *context.Context) {
		reqId := c.Request.Header.Get("my_header_reqid")
		log.Printf("reqestid = %s", reqId)

		cReqId := c.Input.GetData("my_reqid")
		log.Printf("my reqestid = %s", cReqId)

		_, _ = c.ResponseWriter.Write([]byte("hello..."))
		return
	})

	beego.Run(":9900")
}

此外,前端请求时需要带上header key要与后端的一致

获得快乐

发完代码后我就直接去Beego的GitHub仓库下提了一个issue,来分享的研究的中间件,地址:https://github.com/beego/beego/issues/5419,后来没想到竟然收到了回复,哈哈哈

然后我立马就去新的issue分享了我的插件

https://github.com/beego/beego/issues/5421

再后来,我就有两个star了~

仓库地址

https://github.com/ibarryyan/beego-requestid

相关推荐
qxqxa41 分钟前
cfg80211是怎么配置无线设备的AP的?
网络·驱动开发
秋夫人2 小时前
http cache-control
网络·网络协议·http
不灭锦鲤3 小时前
ssrf学习(ctfhub靶场)
网络·学习·安全
weixin_548444264 小时前
2024年最新版本神马TV8.5影视APP源码 293TV影视点播系统源码搭建教程 神马TV8.2加强版反编译教程 保姆级小白可搭建 完整版本视频教程
网络
网络研究院6 小时前
如何安全地大规模部署 GenAI 应用程序
网络·人工智能·安全·ai·部署·观点
limengshi1383926 小时前
通信工程学习:什么是RIP路由信息协议
网络·网络协议·学习·智能路由器·信息与通信
GodK7777 小时前
HTTPS 的加密流程
网络协议·http·https
limengshi13839210 小时前
通信工程学习:什么是TFTP简单文件传输协议
网络·网络协议·学习·信息与通信
麻辣韭菜12 小时前
网络基础 【HTTP】
网络·c++·http
__AtYou__12 小时前
Golang | Leetcode Golang题解之第448题找到所有数组中消失的数字
leetcode·golang·题解