gin

package module
v0.0.0-...-a9bbbbd Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2023 License: MIT Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Gin Plugin

go get -u github.com/rumorshub/gin

License

Distributed under MIT License, please see license file within the code for more details.

Documentation

Index

Constants

View Source
const PluginName = "gin"

Variables

View Source
var (
	ErrBadRequest          = func() *Error { return NewError(http.StatusBadRequest) }
	ErrUnauthorized        = func() *Error { return NewError(http.StatusUnauthorized) }
	ErrForbidden           = func() *Error { return NewError(http.StatusForbidden) }
	ErrNotFound            = func() *Error { return NewError(http.StatusNotFound) }
	ErrConflict            = func() *Error { return NewError(http.StatusConflict) }
	ErrUnprocessableEntity = func() *Error { return NewError(http.StatusUnprocessableEntity) }
	ErrInternalServerError = func() *Error { return NewError(http.StatusInternalServerError) }

	ErrorTransform = DefaultErrorTransform
)

Functions

func GetRePath

func GetRePath(str string) *regexp.Regexp

func Handler

func Handler(e *Engine, fun func(*Context)) gin.HandlerFunc

func MatchPath

func MatchPath(regPath, rPath string) bool

Types

type Config

type Config struct {
	// Mode gin mode according to input string: debug, release, test.
	Mode string `mapstructure:"mode" json:"mode,omitempty" bson:"mode,omitempty"`

	// Static files which serves from the given file system root.
	Static map[string]string `mapstructure:"static" json:"static,omitempty" bson:"static,omitempty"`

	// List of the middleware names (order will be preserved).
	Middleware []string `mapstructure:"middleware" json:"middleware,omitempty" bson:"middleware,omitempty"`

	// EnableDecoderUseNumber is used to call the UseNumber method on the JSON
	// Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
	// any as a Number instead of as a float64.
	EnableDecoderUseNumber bool `` /* 126-byte string literal not displayed */

	// EnableDecoderDisallowUnknownFields is used to call the DisallowUnknownFields method
	// on the JSON Decoder instance. DisallowUnknownFields causes the Decoder to
	// return an error when the destination is a struct and the input contains object
	// keys which do not match any non-ignored, exported fields in the destination.
	EnableDecoderDisallowUnknownFields bool `` /* 165-byte string literal not displayed */

	// RedirectTrailingSlash enables automatic redirection if the current route can't be matched but a
	// handler for the path with (without) the trailing slash exists.
	// For example if /foo/ is requested but a route only exists for /foo, the
	// client is redirected to /foo with http status code 301 for GET requests
	// and 307 for all other request methods.
	RedirectTrailingSlash *bool `mapstructure:"redirect_trailing_slash" json:"redirect_trailing_slash,omitempty" bson:"redirect_trailing_slash,omitempty"`

	// RedirectFixedPath if enabled, the router tries to fix the current request path, if no
	// handle is registered for it.
	// First superfluous path elements like ../ or // are removed.
	// Afterwards the router does a case-insensitive lookup of the cleaned path.
	// If a handle can be found for this route, the router makes a redirection
	// to the corrected path with status code 301 for GET requests and 307 for
	// all other request methods.
	// For example /FOO and /..//Foo could be redirected to /foo.
	// RedirectTrailingSlash is independent of this option.
	RedirectFixedPath bool `mapstructure:"redirect_fixed_path" json:"redirect_fixed_path,omitempty" bson:"redirect_fixed_path,omitempty"`

	// HandleMethodNotAllowed if enabled, the router checks if another method is allowed for the
	// current route, if the current request can not be routed.
	// If this is the case, the request is answered with 'Method Not Allowed'
	// and HTTP status code 405.
	// If no other Method is allowed, the request is delegated to the NotFound
	// handler.
	HandleMethodNotAllowed bool `` /* 126-byte string literal not displayed */

	// ForwardedByClientIP if enabled, client IP will be parsed from the request's headers that
	// match those stored at `(*gin.Engine).RemoteIPHeaders`. If no IP was
	// fetched, it falls back to the IP obtained from
	// `(*gin.Context).Request.RemoteAddr`
	ForwardedByClientIP *bool `mapstructure:"forwarded_by_client_ip" json:"forwarded_by_client_ip,omitempty" bson:"forwarded_by_client_ip,omitempty"`

	// UseRawPath if enabled, the url.RawPath will be used to find parameters.
	UseRawPath bool `mapstructure:"use_raw_path" json:"use_raw_path,omitempty" bson:"use_raw_path,omitempty"`

	// UnescapePathValues if true, the path value will be unescaped.
	// If UseRawPath is false (by default), the UnescapePathValues effectively is true,
	// as url.Path gonna be used, which is already unescaped.
	UnescapePathValues *bool `mapstructure:"unescape_path_values" json:"unescape_path_values,omitempty" bson:"unescape_path_values,omitempty"`

	// RemoveExtraSlash a parameter can be parsed from the URL even with extra slashes.
	// See the PR #1817 and issue #1644
	RemoveExtraSlash bool `mapstructure:"remove_extra_slash" json:"remove_extra_slash,omitempty" bson:"remove_extra_slash,omitempty"`

	// RemoteIPHeaders list of headers used to obtain the client IP when
	// `(*gin.Engine).ForwardedByClientIP` is `true` and
	// `(*gin.Context).Request.RemoteAddr` is matched by at least one of the
	// network origins of list defined by `(*gin.Engine).SetTrustedProxies()`.
	RemoteIPHeaders []string `mapstructure:"remote_ip_headers" json:"remote_ip_headers,omitempty" bson:"remote_ip_headers,omitempty"`

	// TrustedPlatform if set to a constant of value gin.Platform*, trusts the headers set by
	// that platform, for example to determine the client IP
	TrustedPlatform string `mapstructure:"trusted_platform" json:"trusted_platform,omitempty" bson:"trusted_platform,omitempty"`

	// MaxMultipartMemory value of 'maxMemory' param that is given to http.Request's ParseMultipartForm
	// method call.
	MaxMultipartMemory int64 `mapstructure:"max_multipart_memory" json:"max_multipart_memory,omitempty" bson:"max_multipart_memory,omitempty"`

	// ContextWithFallback enable fallback Context.Deadline(), Context.Done(), Context.Err() and Context.Value() when Context.Request.Context() is not nil.
	ContextWithFallback bool `mapstructure:"context_with_fallback" json:"context_with_fallback,omitempty" bson:"context_with_fallback,omitempty"`
}

func (*Config) InitDefaults

func (g *Config) InitDefaults()

type Configurer

type Configurer interface {
	Has(name string) bool
	UnmarshalKey(name string, out interface{}) error
}

type Context

type Context struct {
	*gin.Context
	// contains filtered or unexported fields
}

func (*Context) BadRequest

func (c *Context) BadRequest(err error)

func (*Context) Copy

func (c *Context) Copy() *Context

func (*Context) Created

func (c *Context) Created(location string)

func (*Context) E

func (c *Context) E(err error)

func (*Context) Forbidden

func (c *Context) Forbidden(err error)

func (*Context) NoContent

func (c *Context) NoContent()

func (*Context) OK

func (c *Context) OK(data interface{})

func (*Context) Unauthorized

func (c *Context) Unauthorized(err error)

type Engine

type Engine struct {
	*gin.Engine
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine(cfg Config, theme Theme) *Engine

type Error

type Error struct {
	Code     int
	Message  string
	Data     interface{}
	Internal error
}

func DefaultErrorTransform

func DefaultErrorTransform(err error) (e *Error)

func NewError

func NewError(code int) *Error

func (*Error) AddInternal

func (e *Error) AddInternal(err error) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

func (*Error) SetData

func (e *Error) SetData(data interface{}) *Error

func (*Error) SetInternal

func (e *Error) SetInternal(err error) *Error

func (*Error) SetMessage

func (e *Error) SetMessage(message string) *Error

func (*Error) Unwrap

func (e *Error) Unwrap() error

type ErrorTransformFunc

type ErrorTransformFunc func(err error) *Error

type FieldError

type FieldError struct {
	Namespace string `json:"namespace,omitempty"`
	Field     string `json:"field,omitempty"`
	Tag       string `json:"tag,omitempty"`
	Value     string `json:"value,omitempty"`
	Message   string `json:"message,omitempty"`
}

type HTMLRender

type HTMLRender struct {
	// contains filtered or unexported fields
}

func (*HTMLRender) Instance

func (r *HTMLRender) Instance(name string, data any) render.Render

type HandlerFunc

type HandlerFunc func(*Context)

type Logger

type Logger interface {
	NamedLogger(name string) *slog.Logger
}

type Middleware

type Middleware interface {
	Name() string
	Middleware() HandlerFunc
}

type MiddlewareWrapper

type MiddlewareWrapper struct {
	// contains filtered or unexported fields
}

func NewMiddlewareWrapper

func NewMiddlewareWrapper(name string, handler HandlerFunc) MiddlewareWrapper

func (MiddlewareWrapper) Middleware

func (w MiddlewareWrapper) Middleware() HandlerFunc

func (MiddlewareWrapper) Name

func (w MiddlewareWrapper) Name() string

type Middlewares

type Middlewares interface {
	Middlewares() []interface{}
}

type MiddlewaresWrapper

type MiddlewaresWrapper struct {
	// contains filtered or unexported fields
}

func NewMiddlewaresWrapper

func NewMiddlewaresWrapper(mdwr ...interface{}) MiddlewaresWrapper

func (MiddlewaresWrapper) Middlewares

func (w MiddlewaresWrapper) Middlewares() []interface{}

type Plugin

type Plugin struct {
	// contains filtered or unexported fields
}

func (*Plugin) Collects

func (p *Plugin) Collects() []*dep.In

func (*Plugin) Handler

func (p *Plugin) Handler() http.Handler

func (*Plugin) Init

func (p *Plugin) Init(cfg Configurer, theme Theme, logger Logger) error

func (*Plugin) Name

func (p *Plugin) Name() string

func (*Plugin) Provides

func (p *Plugin) Provides() []*dep.Out

type Router

type Router interface {
	Path() string
	Methods() []string
	Handler(c *Context)
}

type RouterGroup

type RouterGroup interface {
	Path() string
	Routers() []interface{}
}

type RouterGroupWrapper

type RouterGroupWrapper struct {
	// contains filtered or unexported fields
}

func NewRouterGroupWrapper

func NewRouterGroupWrapper(path string, routers ...interface{}) *RouterGroupWrapper

func (*RouterGroupWrapper) AddMiddleware

func (g *RouterGroupWrapper) AddMiddleware(names ...string) *RouterGroupWrapper

func (*RouterGroupWrapper) AddRouter

func (g *RouterGroupWrapper) AddRouter(routers ...interface{}) *RouterGroupWrapper

func (*RouterGroupWrapper) Middleware

func (g *RouterGroupWrapper) Middleware() []string

func (*RouterGroupWrapper) Path

func (g *RouterGroupWrapper) Path() string

func (*RouterGroupWrapper) Routers

func (g *RouterGroupWrapper) Routers() []interface{}

func (*RouterGroupWrapper) SetMiddleware

func (g *RouterGroupWrapper) SetMiddleware(names ...string) *RouterGroupWrapper

type RouterGroups

type RouterGroups interface {
	RouterGroups() []interface{}
}

type RouterGroupsWrapper

type RouterGroupsWrapper struct {
	// contains filtered or unexported fields
}

func NewRouterGroupsWrapper

func NewRouterGroupsWrapper(routerGroups ...interface{}) RouterGroupsWrapper

func (RouterGroupsWrapper) RouterGroups

func (g RouterGroupsWrapper) RouterGroups() []interface{}

type RouterWrapper

type RouterWrapper struct {
	// contains filtered or unexported fields
}

func NewRouterWrapper

func NewRouterWrapper(method, path string, handler HandlerFunc) *RouterWrapper

func (*RouterWrapper) AddAfterMiddleware

func (r *RouterWrapper) AddAfterMiddleware(names ...string) *RouterWrapper

func (*RouterWrapper) AddBeforeMiddleware

func (r *RouterWrapper) AddBeforeMiddleware(names ...string) *RouterWrapper

func (*RouterWrapper) AddMethod

func (r *RouterWrapper) AddMethod(method string) *RouterWrapper

func (*RouterWrapper) AfterMiddleware

func (r *RouterWrapper) AfterMiddleware() []string

func (*RouterWrapper) BeforeMiddleware

func (r *RouterWrapper) BeforeMiddleware() []string

func (*RouterWrapper) Handler

func (r *RouterWrapper) Handler(c *Context)

func (*RouterWrapper) Methods

func (r *RouterWrapper) Methods() []string

func (*RouterWrapper) Path

func (r *RouterWrapper) Path() string

func (*RouterWrapper) SetAfterMiddleware

func (r *RouterWrapper) SetAfterMiddleware(names ...string) *RouterWrapper

func (*RouterWrapper) SetBeforeMiddleware

func (r *RouterWrapper) SetBeforeMiddleware(names ...string) *RouterWrapper

type Static

type Static interface {
	RelativePath() string
	FileSystem() http.FileSystem
}

type Statics

type Statics interface {
	StaticsFS() []interface{}
}

type Theme

type Theme interface {
	Debug(debug bool) *et.Environment
	Global(global ...string) *et.Environment
	Load(ctx context.Context, name string) (*et.TemplateWrapper, error)
}

Directories

Path Synopsis
middleware

Jump to

Keyboard shortcuts

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