Documentation
¶
Overview ¶
Package middleware 提供AIDI PDT后端服务请求中间件功能,包括链路追踪,支持gin和beego框架
链路追踪功能包括在收到HTTP请求时自动获取或者初始化TraceID并写入到context中,发起HTTP请求时自动将TraceID信息写入到HTTP中
Index ¶
- Constants
- Variables
- func Close()
- func GetTraceID(ctx context.Context) string
- func GinAccessLog() gin.HandlerFunc
- func GinIdentify(skippedAuthURLs []string, baseDomain string) gin.HandlerFunc
- func GinTrace() gin.HandlerFunc
- func GinUserAuth(skippedAuthURLs []string) gin.HandlerFunc
- func InjectTraceID(ctx context.Context, operationName string, header http.Header)
- func Recovery(callback PanicHandleFunc) gin.HandlerFunc
- func RecoveryWithWriter(callback PanicHandleFunc) gin.HandlerFunc
- func SetTraceID(req *http.Request, resp http.ResponseWriter) *http.Request
- func TransGinSet() gin.HandlerFunc
- type PanicHandleFunc
- type UserAuthSvc
- func (us *UserAuthSvc) GetEndpoint() string
- func (us *UserAuthSvc) GetSessKey() (sesskey string)
- func (us *UserAuthSvc) GetUserToken() string
- func (us *UserAuthSvc) ParseToken() (user *UserInfo, err error)
- func (us *UserAuthSvc) SetEndpoint(domain string)
- func (us *UserAuthSvc) SetToken(token string)
- type UserInfo
Constants ¶
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 )
const StartedSpanOPName = "StartHTTPRequest"
Variables ¶
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, } )
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 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 GinUserAuth ¶
func GinUserAuth(skippedAuthURLs []string) gin.HandlerFunc
GinUserAuth gin用户认证中间件,进行用户鉴权并将用户信息写入ctx Deprecated: use GinIdentify instead
func InjectTraceID ¶
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 ¶
SetTraceID 将trace id信息注入到ctx中,同时加入到响应头中
func TransGinSet ¶
func TransGinSet() gin.HandlerFunc
将gin Context中 用户set的内容都copy到 Context中, 以便在aidi-gin中使用
Types ¶
type PanicHandleFunc ¶
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