Documentation ¶
Index ¶
- Variables
- func AddBinding(tag string, fn validator.Func) error
- func AddTranslator(translator ut.Translator, tag, message string, override bool) error
- func DumpRequest(c *gin.Context, options ...DumpRequestOption) []string
- func EnableParamRegexpBinding() error
- func EnableParamRegexpBindingTranslator(translator ut.Translator) error
- func EnableRFC3339DateBinding() error
- func EnableRFC3339DateBindingTranslator(translator ut.Translator) error
- func EnableRFC3339DateTimeBinding() error
- func EnableRFC3339DateTimeBindingTranslator(translator ut.Translator) error
- func GetValidatorEngine() (*validator.Validate, error)
- func GetValidatorTranslator(locTranslator locales.Translator, ...) (ut.Translator, error)
- func LogToLogger(logger logrus.StdLogger, c *gin.Context, start, end time.Time, ...)
- func LogToLogrus(logger *logrus.Logger, c *gin.Context, start, end time.Time, ...)
- func PprofWrap(router *gin.Engine)
- func WithExtraFields(fields map[string]interface{}) logop.LoggerOption
- func WithExtraFieldsV(fields ...interface{}) logop.LoggerOption
- func WithExtraText(text string) logop.LoggerOption
- type AppRouter
- func (a *AppRouter) Any(relativePath string, handlers ...gin.HandlerFunc)
- func (a *AppRouter) DELETE(relativePath string, handlers ...gin.HandlerFunc)
- func (a *AppRouter) GET(relativePath string, handlers ...gin.HandlerFunc)
- func (a *AppRouter) HEAD(relativePath string, handlers ...gin.HandlerFunc)
- func (a *AppRouter) OPTIONS(relativePath string, handlers ...gin.HandlerFunc)
- func (a *AppRouter) PATCH(relativePath string, handlers ...gin.HandlerFunc)
- func (a *AppRouter) POST(relativePath string, handlers ...gin.HandlerFunc)
- func (a *AppRouter) PUT(relativePath string, handlers ...gin.HandlerFunc)
- func (a *AppRouter) Register()
- type DumpRequestOption
Constants ¶
This section is empty.
Variables ¶
var PrintAppRouterRegisterFunc func(index, count int, method, relativePath, handlerFuncname string, handlersCount int, layerFakePath string)
PrintAppRouterRegisterFunc is a logger function for AppRouter.Register, logs after gin's [GIN-debug] logger.
Functions ¶
func AddBinding ¶
AddBinding adds user defined binding to gin's validator engine. You can use your custom validator.Func or xvalidator's functions such as xvalidator.RegexpValidator and xvalidator.DateTimeValidator.
Binding notes:
- `required` + non-pointer (common) A uint64 `binding:"required"` // cannot be nil and 0 B string `binding:"required"` // cannot be nil and ""
- `required` + pointer (common) A *uint64 `binding:"required"` // cannot be nil, can be 0 B *string `binding:"required"` // cannot be nil, can be ""
- `omitempty` + non-pointer (common) A uint64 `binding:"omitempty"` // can be nil and 0 B string `binding:"omitempty"` // can be nil and ""
- `omitempty` + pointer => same as 3 A *uint64 `binding:"omitempty"` // can be nil and 0 B *string `binding:"omitempty"` // can be nil and ""
- `required` + `omitempty` + non-pointer => same as 1 A uint64 `binding:"required,omitempty"` // cannot be nil and 0 B string `binding:"required,omitempty"` // cannot be nil and ""
- `required` + `omitempty` + pointer => same as 2 A *uint64 `binding:"required,omitempty"` // cannot be nil, can be 0 B *string `binding:"required,omitempty"` // cannot be nil, can be ""
Also see https://godoc.org/github.com/go-playground/validator.
func AddTranslator ¶
AddTranslator adds user defined validator's translator to given ut.Translator using given tag, message and override. Also see xvalidator.AddToTranslatorFunc and xvalidator.DefaultTranslateFunc.
Example:
translator, _ := xgin.GetValidatorTranslator(xvalidator.EnLocaleTranslator(), xvalidator.EnTranslationRegisterFunc()) err := xgin.AddTranslator(translator, "regexp", "{0} must matches regexp /{1}/", true) err := xgin.AddTranslator(translator, "email", "{0} must be an email", true)
func DumpRequest ¶
func DumpRequest(c *gin.Context, options ...DumpRequestOption) []string
DumpRequest dumps and formats http.Request from gin.Context to string slice, using given DumpRequestOption-s. The first element must be request line "METHOD /ENDPOINT HTTP/1.1", and the remaining elements are the request headers "XXX: YYY", returns an empty slice when using nil gin.Context.
func EnableParamRegexpBinding ¶
func EnableParamRegexpBinding() error
EnableParamRegexpBinding enables parametered regexp validator to `regexp`, see xvalidator.ParamRegexpValidator.
func EnableParamRegexpBindingTranslator ¶
func EnableParamRegexpBindingTranslator(translator ut.Translator) error
EnableParamRegexpBindingTranslator enables parametered regexp validator (`regexp`)'s translator to given ut.Translator.
func EnableRFC3339DateBinding ¶
func EnableRFC3339DateBinding() error
EnableRFC3339DateBinding enables rfc3339 date validator to `date`, see xvalidator.DateTimeValidator.
func EnableRFC3339DateBindingTranslator ¶
func EnableRFC3339DateBindingTranslator(translator ut.Translator) error
EnableRFC3339DateBindingTranslator enables rfc3339 date validator (`date`)'s translator to given ut.Translator.
func EnableRFC3339DateTimeBinding ¶
func EnableRFC3339DateTimeBinding() error
EnableRFC3339DateTimeBinding enables rfc3339 datetime validator to `datetime`, see xvalidator.DateTimeValidator.
func EnableRFC3339DateTimeBindingTranslator ¶
func EnableRFC3339DateTimeBindingTranslator(translator ut.Translator) error
EnableRFC3339DateTimeBindingTranslator enables rfc3339 datetime validator (`datetime`)'s translator to given ut.Translator.
func GetValidatorEngine ¶
func GetValidatorEngine() (*validator.Validate, error)
GetValidatorEngine returns gin's binding validator engine, which only supports validator.Validate from github.com/go-playground/validator/v10, see binding.Validator.
func GetValidatorTranslator ¶
func GetValidatorTranslator(locTranslator locales.Translator, registerFn xvalidator.TranslationRegisterHandler) (ut.Translator, error)
GetValidatorTranslator applies and returns ut.Translator for validator.Validate using given locales.Translator and xvalidator.TranslationRegisterHandler. Also see xvalidator.ApplyTranslator.
Example:
translator, _ := xgin.GetValidatorTranslator(xvalidator.EnLocaleTranslator(), xvalidator.EnTranslationRegisterFunc()) val := validator.New() result := val.Struct(&testStruct{}).(validator.ValidationErrors).Translate(translator) // validator.ValidationErrorsTranslations
func LogToLogger ¶
func LogToLogger(logger logrus.StdLogger, c *gin.Context, start, end time.Time, options ...logop.LoggerOption)
LogToLogger logs gin's request and response information to logrus.StdLogger using given gin.Context and times.
func LogToLogrus ¶
func LogToLogrus(logger *logrus.Logger, c *gin.Context, start, end time.Time, options ...logop.LoggerOption)
LogToLogrus logs gin's request and response information to logrus.Logger using given gin.Context and times.
func PprofWrap ¶
PprofWrap adds several routes from package `net/http/pprof` to gin.Engine. Reference from https://github.com/DeanThompson/ginpprof.
func WithExtraFields ¶
func WithExtraFields(fields map[string]interface{}) logop.LoggerOption
WithExtraFields creates a logger option to log with extra fields.
func WithExtraFieldsV ¶
func WithExtraFieldsV(fields ...interface{}) logop.LoggerOption
WithExtraFieldsV creates a logger option to log with extra fields in vararg.
func WithExtraText ¶
func WithExtraText(text string) logop.LoggerOption
WithExtraText creates a logger option to log with extra text.
Types ¶
type AppRouter ¶
type AppRouter struct {
// contains filtered or unexported fields
}
AppRouter represents a group of routers with gin.Engine and gin.IRouter, is a replacement of gin's trie router model, see https://github.com/gin-gonic/gin/blob/master/tree.go.
Note:
- gin.Engine supports: router.GET(":a", fn) router.GET(":a/b", fn)
- gin.Engine does not support: (contains "x" and ":x" in the same layer) router.GET(":a", fn) router.GET("a", fn) // 'a' in new path '/a' conflicts with existing wildcard ':a' in existing prefix '/:a' // and router.GET(":a/b", fn) router.GET("a", fn) // 'a' in new path '/a' conflicts with existing wildcard ':a' in existing prefix '/:a' // and router.GET(":a/:b", fn) router.GET(":a/b", fn) // 'b' in new path '/:a/b' conflicts with existing wildcard ':b' in existing prefix '/:a/:b' // and router.GET(":a", fn) router.GET(":b", fn) // ':b' in new path '/:b' conflicts with existing wildcard ':a' in existing prefix '/:a'
- xgin.AppRouter supports: ap.GET(":a", fn) ap.GET("a", fn) ap.GET(":a/:b", fn) ap.GET(":a/b", fn)
- xgin.AppRouter does not support: ap.GET(":a", fn) ap.GET(":b", fn) // X
func NewAppRouter ¶
NewAppRouter creates an empty AppRouter using given gin.Engine and gin.IRouter.
Example:
app := gin.New() v1 := app.Group("v1") ap := xgin.NewAppRouter(app, v1) ap.GET("a", fn) // /v1/a ap.GET(":a", fn) // /v1/:a ap.GET(":a/b", fn) // /v1/:a/b ap.GET(":a/:b", fn) // /v1/:a/:b ap.Register()
func (*AppRouter) Any ¶
func (a *AppRouter) Any(relativePath string, handlers ...gin.HandlerFunc)
Any registers a new list of handlers to given path and uses all the supported http methods: get, post, delete, patch, put, options, head.
func (*AppRouter) DELETE ¶
func (a *AppRouter) DELETE(relativePath string, handlers ...gin.HandlerFunc)
DELETE registers a new list of handlers to given path and uses delete method.
func (*AppRouter) GET ¶
func (a *AppRouter) GET(relativePath string, handlers ...gin.HandlerFunc)
GET registers a new list of handlers to given path and uses get method.
func (*AppRouter) HEAD ¶
func (a *AppRouter) HEAD(relativePath string, handlers ...gin.HandlerFunc)
HEAD registers a new list of handlers to given path and uses head method.
func (*AppRouter) OPTIONS ¶
func (a *AppRouter) OPTIONS(relativePath string, handlers ...gin.HandlerFunc)
OPTIONS registers a new list of handlers to given path and uses options method.
func (*AppRouter) PATCH ¶
func (a *AppRouter) PATCH(relativePath string, handlers ...gin.HandlerFunc)
PATCH registers a new list of handlers to given path and uses patch method.
func (*AppRouter) POST ¶
func (a *AppRouter) POST(relativePath string, handlers ...gin.HandlerFunc)
POST registers a new list of handlers to given path and uses post method.
type DumpRequestOption ¶
type DumpRequestOption func(*dumpRequestOptions)
DumpRequestOption represents an option for DumpRequest, can be created by WithXXX functions.
func WithIgnoreHeaders ¶
func WithIgnoreHeaders(headers ...string) DumpRequestOption
WithIgnoreHeaders creates a DumpRequestOption for ignore headers. This option will be ignored when WithRetainHeaders is used in DumpRequest.
func WithRetainHeaders ¶
func WithRetainHeaders(headers ...string) DumpRequestOption
WithRetainHeaders creates a DumpRequestOption for retained header. Set this option will make DumpRequest ignore the WithIgnoreHeaders option.
func WithSecretHeaders ¶
func WithSecretHeaders(headers ...string) DumpRequestOption
WithSecretHeaders creates a DumpRequestOption for secret headers, such as Authorization.
func WithSecretReplace ¶
func WithSecretReplace(secret string) DumpRequestOption
WithSecretReplace creates a DumpRequestOption for secret header replace string, defaults to "*".