Documentation ¶
Index ¶
- Constants
- Variables
- func BindPrometheus(s *gin.Engine)
- func EnableMetric(srv *gin.Engine, options ...MetricsOptFunc)
- func FromStd(handler http.HandlerFunc) gin.HandlerFunc
- func GetGinCtxFromStdCtx(ctx context.Context) *gin.Context
- func LoggerMiddleware(ctx *gin.Context)
- func StartHTTPMetricSrv(ctx context.Context, options ...MetricsOptFunc)
- type Auth
- type AuthCfg
- type CookieCfg
- type MetricOption
- type MetricsOptFunc
- type UserItf
Examples ¶
Constants ¶
const ( // AuthTokenName jwt token cookie name AuthTokenName = "token" // AuthUserIDCtxKey key of user ID in jwt token AuthUserIDCtxKey = "auth_uid" // AuthTokenAge how long live AuthTokenAge = 3600 * 24 * 7 // 7days )
Variables ¶
var GinCtxKey utils.CtxKeyT
GinCtxKey key of gin ctx that saved in request.context
Functions ¶
func BindPrometheus ¶ added in v1.4.1
BindPrometheus bind prometheus endpoint.
func EnableMetric ¶ added in v1.8.0
func EnableMetric(srv *gin.Engine, options ...MetricsOptFunc)
EnableMetric enable metrics for exsits gin server
func FromStd ¶
func FromStd(handler http.HandlerFunc) gin.HandlerFunc
FromStd convert std handler to gin.Handler, with gin context embedded
func GetGinCtxFromStdCtx ¶
GetGinCtxFromStdCtx get gin context from standard request.context by GinCtxKey
func LoggerMiddleware ¶
LoggerMiddleware middleware to logging
func StartHTTPMetricSrv ¶ added in v1.8.0
func StartHTTPMetricSrv(ctx context.Context, options ...MetricsOptFunc)
StartHTTPMetricSrv start new gin server with metrics api
Types ¶
type Auth ¶
type Auth struct { *AuthCfg // contains filtered or unexported fields }
Auth JWT cookie based token generator and validator. Cookie looks like <AuthTokenName>:`{<AuthUserIDCtxKey>: "xxxx"}`
Example ¶
package main import ( "net/http" middlewares "github.com/Laisky/go-utils/gin-middlewares" "github.com/Laisky/zap" "github.com/gin-gonic/gin" "github.com/Laisky/go-utils" ) type User struct{} func (u *User) GetPayload() map[string]interface{} { return map[string]interface{}{"a": "b"} } func (u *User) GetID() string { return "123" } func main() { cfg := middlewares.NewAuthCfg("f32lifj2f32fj") auth, err := middlewares.NewAuth(cfg) if err != nil { utils.Logger.Panic("try to init gin auth got error", zap.Error(err)) } ctx := &gin.Context{} uid, err := auth.ValidateAndGetUID(ctx) if err != nil { utils.Logger.Warn("user invalidate", zap.Error(err)) } else { utils.Logger.Info("user validate", zap.String("uid", uid.Hex())) } user := &User{} if err = auth.SetLoginCookie(ctx, user, middlewares.NewCookieCfg()); err != nil { utils.Logger.Error("try to set cookie got error", zap.Error(err)) } Server := gin.New() Server.Handle("ANY", "/authorized/", middlewares.FromStd(DemoHandle)) } func DemoHandle(w http.ResponseWriter, r *http.Request) { // middlewares if _, err := w.Write([]byte("hello")); err != nil { utils.Logger.Error("http write", zap.Error(err)) } }
Output:
func (*Auth) SetLoginCookie ¶
SetLoginCookie set jwt token to cookies
type AuthCfg ¶
AuthCfg configuration of Auth
func NewAuthCfg ¶
NewAuthCfg return AuthCfg with default configuration
type CookieCfg ¶ added in v1.4.1
CookieCfg configuration of cookies
func NewCookieCfg ¶ added in v1.4.1
func NewCookieCfg() *CookieCfg
NewCookieCfg create default cookie configuration
type MetricOption ¶ added in v1.8.0
type MetricOption struct {
// contains filtered or unexported fields
}
MetricOption metric option argument
func NewMetricOption ¶ added in v1.8.0
func NewMetricOption() *MetricOption
NewMetricOption create new default option
type MetricsOptFunc ¶ added in v1.8.0
type MetricsOptFunc func(*MetricOption)
MetricsOptFunc option of metrics
func WithMetricAddr ¶ added in v1.8.0
func WithMetricAddr(addr string) MetricsOptFunc
WithMetricAddr set option addr
func WithMetricGraceWait ¶ added in v1.8.0
func WithMetricGraceWait(wait time.Duration) MetricsOptFunc
WithMetricGraceWait set wating time after graceful shutdown
func WithPprofPath ¶ added in v1.8.0
func WithPprofPath(path string) MetricsOptFunc
WithPprofPath set option pprofPath