middleware

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package middleware 提供AIDI PDT后端服务请求中间件功能,包括链路追踪,支持gin和beego框架

链路追踪功能包括在收到HTTP请求时自动获取或者初始化TraceID并写入到context中,发起HTTP请求时自动将TraceID信息写入到HTTP中

Index

Constants

View Source
const (
	HobotSessionKey = "hsess"              // oauth session key name(cookie name)
	HobotCtxUser    = "Hobot-User"         // middleware user info
	CookieTokenName = "user-token"         // oauth Deprecated cookie token
	HeaderTokenNew  = "X-Forwarded-Token"  // oauth token
	HeaderToken     = "X-Forwarded-User"   // oauth token, Deprecated for uname
	HeaderUName     = "X-Forwarded-Uname"  // oauth header uname
	HeaderUID       = "X-Forwarded-Uid"    // oauth header uid
	HeaderDomain    = "X-Forwarded-Orga"   // oauth header domain
	HeaderDomainID  = "X-Forwarded-Orgaid" // oauth header domain id
	HeaderHost      = "X-Forwarded-Host"   // oauth header host
	SessionURIName  = "uri-session"        //oauth cookie username
	CookieDeviceID  = "device-id"          //oauth cookie uniq id for device
)
View Source
const StartedSpanOPName = "StartHTTPRequest"

Variables

View Source
var (
	Scheme         = "http"
	DevDomain      = "aidi-dev.hobot.cc"
	TestDomain     = "aidi-test.hobot.cc"
	ProdDomain     = "aidi.hobot.cc"
	SaasTestDomain = "aidi-test.hobot.ai"
	SaasProdDomain = "aidi.horizon.ai"
	SCMap          = map[env.RunMode]string{
		env.DevMode:      DevDomain,
		env.TestMode:     TestDomain,
		env.ProdMode:     ProdDomain,
		env.SaasTestMode: SaasTestDomain,
		env.SaasProdMode: SaasProdDomain,
	}
)
View Source
var BeegoTrace = func(c *bctx.Context) {
	var span opentracing.Span
	c.Request, span = startSpan(c.Request)
	defer span.Finish()

	c.Request = SetTraceID(c.Request, c.ResponseWriter)
}

BeegoTrace beego中间件过滤器,用于后台服务的链路追踪

Functions

func Close

func Close()

func GetTraceID

func GetTraceID(ctx context.Context) string

GetTraceID 从span中获取trace id

func GinAccessLog

func GinAccessLog() gin.HandlerFunc

GinAccessLog gin访问日志中间件,用于打印单次请求的详细日志信息

func GinIdentify

func GinIdentify(skippedAuthURLs []string, baseDomain string) gin.HandlerFunc

GinIdentify parse user info by token and save to gin context if you are not use gin, you can implement a self method by use UserAuthSvc.ParseToken

func GinTrace

func GinTrace() gin.HandlerFunc

GinTrace gin中间件,用于后台服务的链路追踪

func GinUserAuth

func GinUserAuth(skippedAuthURLs []string) gin.HandlerFunc

GinUserAuth gin用户认证中间件,进行用户鉴权并将用户信息写入ctx Deprecated: use GinIdentify instead

func InjectTraceID

func InjectTraceID(ctx context.Context, operationName string, header http.Header)

InjectTraceID 在http请求头中注入HTTP请求 TODO: 提供http client,关闭span

func Recovery

func Recovery(callback PanicHandleFunc) gin.HandlerFunc

Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.

func RecoveryWithWriter

func RecoveryWithWriter(callback PanicHandleFunc) gin.HandlerFunc

RecoveryWithWriter returns a middleware for a given writer that recovers from any panics and writes a 500 if there was one. "broken pipe": 一般是客户端主动断开而服务端还在发数据导致,引发的panic可以忽略 处理方式参考框架中的例子: https://github.com/gin-gonic/gin/blob/master/recovery.go#L52

func SetTraceID

func SetTraceID(req *http.Request, resp http.ResponseWriter) *http.Request

SetTraceID 将trace id信息注入到ctx中,同时加入到响应头中

func TransGinSet

func TransGinSet() gin.HandlerFunc

将gin Context中 用户set的内容都copy到 Context中, 以便在aidi-gin中使用

Types

type PanicHandleFunc

type PanicHandleFunc func(ctx context.Context, msg string)

PanicHandleFunc panic 回调函数 msg: 表示panic时的栈帧,可以发送邮件或者飞书

type UserAuthSvc

type UserAuthSvc struct {
	Endpoint string        //auth identify base domain
	Request  *http.Request //current request
	// contains filtered or unexported fields
}

UserAuthSvc userauth identify service

func (*UserAuthSvc) GetEndpoint

func (us *UserAuthSvc) GetEndpoint() string

GetEndpoint try get base domain from env and config and http request

func (*UserAuthSvc) GetSessKey

func (us *UserAuthSvc) GetSessKey() (sesskey string)

GetSessKey get session key from cookie

func (*UserAuthSvc) GetUserToken

func (us *UserAuthSvc) GetUserToken() string

GetUserToken get user token from cookie > header

func (*UserAuthSvc) ParseToken

func (us *UserAuthSvc) ParseToken() (user *UserInfo, err error)

ParseToken parse token from {userauth}/api/oauth/oauth_login req {basedomain}/api/oauth/oauth_login to parse token

func (*UserAuthSvc) SetEndpoint

func (us *UserAuthSvc) SetEndpoint(domain string)

SetDomain set req base domain

func (*UserAuthSvc) SetToken

func (us *UserAuthSvc) SetToken(token string)

SetToken set req header token

type UserInfo

type UserInfo struct {
	UID    int64  `json:"u_id"`
	UName  string `json:"u_name"`
	OUID   int64  `json:"ou_id"`
	OUName string `json:"ou_name"`
	Token  string `json:"token"`
}

UserInfo user basic info

func GinUserInfo

func GinUserInfo(c *gin.Context) *UserInfo

GinUserInfo get user info from gin context

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL