package middleware

import (
	"github.com/gogf/gf/v2/net/ghttp"
	"yx-dataset-server/library/gplus"
	"yx-dataset-server/library/utils"
)

func TraceIdMiddleware(skippers ...SkipperFunc) ghttp.HandlerFunc {
	return func(r *ghttp.Request) {
		if len(skippers) > 0 && skippers[0](r) {
			r.Middleware.Next()
			return
		}
		if r.GetCtxVar(gplus.TraceIDKey).String() == "" {
			r.SetCtxVar(gplus.TraceIDKey, utils.NewTraceID())
		}
		r.Middleware.Next()
	}
}
