Documentation ¶
Overview ¶
Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/binding.go
Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/form.go
Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/form_mapping.go
Copyright 2019 Gin Core Team. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/multipart_form_mapping.go
Copyright 2022 Gin Core Team. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/header.go
Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/json.go
Copyright 2018 Gin Core Team. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/uri.go
Copyright 2017 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/query.go
Copyright 2017 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/default_validator.go
Copyright 2014 Manu Martinez-Almeida. All rights reserved. see: https://github.com/gin-gonic/gin/blob/master/binding/xml.go
Index ¶
- Variables
- func Crypto() crypto.Crypto
- func HashCrc32(content []byte) string
- func HashMD5(text string) string
- func HashXxh64(content []byte) string
- func MapFormWithTag(ptr any, form map[string][]string, tag string) error
- func NewKeyring(salt string, iterations int, length int, digest string) *crypto.Keyring
- func NewUID() (uid string)
- func NodeName() string
- func SecretKeyBase() string
- func SecretKeySync(sync SecretKeySyncFunc) (cancel func())
- func SecretKeys() []string
- func SetNodeName(name string)
- func SetSecretKeyBase(secret string) error
- type BindUnmarshaler
- type Binding
- type BindingDefaultStruct
- type Context
- func (ctx *Context) AddHeader(key, value string)
- func (ctx *Context) AfterSend(callback func()) error
- func (ctx *Context) BadRequest()
- func (ctx *Context) BeforeSend(callback func()) error
- func (ctx *Context) Bind(obj any) error
- func (ctx *Context) BindForm(obj any) error
- func (ctx *Context) BindFormMultipart(obj any) error
- func (ctx *Context) BindFormPost(obj any) error
- func (ctx *Context) BindHeader(obj any) error
- func (ctx *Context) BindJSON(obj any) error
- func (ctx *Context) BindPath(obj any) error
- func (ctx *Context) BindQuery(obj any) error
- func (ctx *Context) BindXML(obj any) error
- func (ctx *Context) BodyBytes() (body []byte, err error)
- func (ctx *Context) Child() *Context
- func (ctx *Context) ContentType(ctype string)
- func (ctx *Context) Created()
- func (ctx *Context) Destroy()
- func (ctx *Context) Error(error string, code int)
- func (ctx *Context) Forbidden()
- func (ctx *Context) Get(key any) (any, bool)
- func (ctx *Context) GetContentType() string
- func (ctx *Context) GetCookie(name string) *http.Cookie
- func (ctx *Context) GetHeader(key string) string
- func (ctx *Context) GetParam(name string) string
- func (ctx *Context) GetParamByIndex(index int) string
- func (ctx *Context) GetStatus() int
- func (ctx *Context) Header() http.Header
- func (ctx *Context) Host() string
- func (ctx *Context) InternalServerError()
- func (ctx *Context) Ip() string
- func (ctx *Context) Json(v any)
- func (ctx *Context) Method() string
- func (ctx *Context) MustBindWith(obj any, b Binding) error
- func (ctx *Context) NewUID() (uid string)
- func (ctx *Context) NoContent()
- func (ctx *Context) NotFound()
- func (ctx *Context) NotImplemented()
- func (ctx *Context) OK()
- func (ctx *Context) QueryParam(name string, defaultValue ...string) string
- func (ctx *Context) QueryParamInt(name string, defaultValue ...int) int
- func (ctx *Context) Redirect(url string, code int)
- func (ctx *Context) RemoveCookie(name string)
- func (ctx *Context) Router() *Router
- func (ctx *Context) ServeContent(content []byte, name string, modtime time.Time)
- func (ctx *Context) ServiceUnavailable()
- func (ctx *Context) Set(key any, value any)
- func (ctx *Context) SetCookie(cookie *http.Cookie)
- func (ctx *Context) SetHeader(key, value string)
- func (ctx *Context) ShouldBind(obj any) error
- func (ctx *Context) ShouldBindForm(obj any) error
- func (ctx *Context) ShouldBindFormMultipart(obj any) error
- func (ctx *Context) ShouldBindFormPost(obj any) error
- func (ctx *Context) ShouldBindHeader(obj any) error
- func (c *Context) ShouldBindJSON(obj any) error
- func (ctx *Context) ShouldBindPath(obj any) error
- func (ctx *Context) ShouldBindQuery(obj any) error
- func (ctx *Context) ShouldBindWith(obj any, b Binding) error
- func (c *Context) ShouldBindXML(obj any) error
- func (ctx *Context) Status(statusCode int)
- func (ctx *Context) TooManyRequests()
- func (ctx *Context) URL() *url.URL
- func (ctx *Context) Unauthorized()
- func (ctx *Context) UserAgent() string
- func (ctx *Context) WithParams(names []string, values []string) *Context
- func (ctx *Context) Write(data []byte) (int, error)
- func (ctx *Context) WriteCalled() bool
- func (ctx *Context) WriteHeader(statusCode int)
- func (ctx *Context) WriteHeaderCalled() bool
- func (ctx *Context) WriteStarted() bool
- type Group
- type Handle
- type JsonSerializer
- type Middleware
- type MiddlewareHandler
- type MiddlewareWithInitHandler
- type Registry
- type ResponseWriterSpy
- type Route
- type RouteConfigurator
- type RouteInfo
- func (d *RouteInfo) Details() (segments []string, params []string, indexes []int)
- func (d *RouteInfo) FastMatch(ctx *Context) bool
- func (d *RouteInfo) HasParameter() bool
- func (d *RouteInfo) HasStatic() bool
- func (d *RouteInfo) HasWildcard() bool
- func (d RouteInfo) Match(ctx *Context) (match bool, paramNames []string, paramValues []string)
- func (d *RouteInfo) Matches(o *RouteInfo) bool
- func (d *RouteInfo) Params() []string
- func (d *RouteInfo) ParamsIndex() []int
- func (d *RouteInfo) Path() string
- func (d *RouteInfo) Pattern() string
- func (d *RouteInfo) Priority() int
- func (d *RouteInfo) ReplacePath(ctx *Context) string
- func (d *RouteInfo) Segments() []string
- func (d *RouteInfo) String() string
- type RouteStorage
- type Router
- func (r *Router) Configure(route string, configurator RouteConfigurator)
- func (r *Router) DELETE(route string, handle any) error
- func (r *Router) GET(route string, handle any) error
- func (r *Router) Group(route string) Group
- func (r *Router) HEAD(route string, handle any) error
- func (r *Router) Handle(method string, route string, handle any) error
- func (r *Router) Lookup(method string, path string) (*Route, *Context)
- func (r *Router) OPTIONS(route string, handle any) error
- func (r *Router) PATCH(route string, handle any) error
- func (r *Router) POST(route string, handle any) error
- func (r *Router) PUT(route string, handle any) error
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) Use(args ...any) Group
- type RouterGroup
- func (r *RouterGroup) Configure(route string, configurator RouteConfigurator)
- func (r *RouterGroup) DELETE(route string, handle any) error
- func (r *RouterGroup) GET(route string, handle any) error
- func (r *RouterGroup) Group(route string) Group
- func (r *RouterGroup) HEAD(route string, handle any) error
- func (r *RouterGroup) Handle(method string, route string, handle any) error
- func (r *RouterGroup) OPTIONS(route string, handle any) error
- func (r *RouterGroup) PATCH(route string, handle any) error
- func (r *RouterGroup) POST(route string, handle any) error
- func (r *RouterGroup) PUT(route string, handle any) error
- func (r *RouterGroup) Use(args ...any) Group
- type SecretKeySyncFunc
- type Serializer
- type SliceValidationError
- type StructValidator
Constants ¶
This section is empty.
Variables ¶
var ( // ErrConvertMapStringSlice can not convert to map[string][]string ErrConvertMapStringSlice = errors.New("can not convert to map slices of strings") // ErrConvertToMapString can not convert to map[string]string ErrConvertToMapString = errors.New("can not convert to map of strings") )
var ( // ErrMultiFileHeader multipart.FileHeader invalid ErrMultiFileHeader = errors.New("unsupported field type for multipart.FileHeader") // ErrMultiFileHeaderLenInvalid array for []*multipart.FileHeader len invalid ErrMultiFileHeaderLenInvalid = errors.New("unsupported len of array for []*multipart.FileHeader") )
var ( ErrInvalidHandler = errors.New("invalid handler") ErrInvalidMethod = errors.New("method must not be empty") ErrInvalidPath = errors.New("path must begin with '/'") ErrHandlerIsNil = errors.New("handle must not be nil") )
var BodyBytesKey = bodyBytesKey{}
BodyBytesKey indicates a default body bytes key.
var ContextKey = chainContextKey{}
ContextKey is the request context key under which URL params are stored.
var EnableDecoderDisallowUnknownFields = false
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.
var EnableDecoderUseNumber = false
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.
var ErrAlreadySent = errors.New("the response was already sent")
ErrAlreadySent Error raised when trying to modify or send an already sent response
var UnixEpoch = time.Unix(0, 0)
Functions ¶
func Crypto ¶
Crypto get the reference to a structure that has shortcut to all encryption related functions
func HashXxh64 ¶ added in v1.0.3
Xxh64 return a base64-encoded checksum of a resource using Xxh64 algorithm
Encoded using Base64 URLSafe
func MapFormWithTag ¶ added in v1.0.3
func NewKeyring ¶
NewKeyring starts a Keyring that will be updated whenever SecretKeySync() is invoked
- `salt` - a salt used with SecretKeyBase to generate a secret
- `iterations` - defaults to 1000 (increase to at least 2^16 if used for passwords)
- `length` - a length in octets for the derived key. Defaults to 32
- `digest` - a hmac function to use as the pseudo-random function. Defaults to `sha256`
func SecretKeyBase ¶
func SecretKeyBase() string
SecretKeyBase A secret key used to verify and encrypt data.
This data must be never used directly, always use chain.Crypto().KeyGenerate() to derive keys from it
func SecretKeySync ¶
func SecretKeySync(sync SecretKeySyncFunc) (cancel func())
SecretKeySync is used to transmit SecretKeyBase changes
func SecretKeys ¶
func SecretKeys() []string
SecretKeys gets the list of all SecretKeyBase that have been defined. Can be used in key rotation algorithms
The LAST item in the list is the most recent key (primary key)
func SetNodeName ¶
func SetNodeName(name string)
func SetSecretKeyBase ¶
SetSecretKeyBase see SecretKeyBase()
Types ¶
type BindUnmarshaler ¶ added in v1.0.3
type BindUnmarshaler interface { // UnmarshalParam decodes and assigns a value from an form or query param. UnmarshalParam(param string) error }
BindUnmarshaler is the interface used to wrap the UnmarshalParam method.
type Binding ¶ added in v1.0.3
Binding describes the interface which needs to be implemented for binding the data present in the request such as JSON request body, query parameters or the form POST.
var ( BindingXML Binding = xmlBinding{} // xml BindingJSON Binding = jsonBinding{} // json BindingPath Binding = pathBinding{} // path BindingForm Binding = formBinding{} // form BindingFormPost Binding = formPostBinding{} // form BindingFormMultipart Binding = formMultipartBinding{} // form BindingQuery Binding = queryBinding{} // query BindingHeader Binding = headerBinding{} // header BindingDefault Binding = &BindingDefaultStruct{} // query, json, xml, form )
These implement the Binding interface and can be used to bind the data present in the request to struct instances.
type BindingDefaultStruct ¶ added in v1.0.3
type BindingDefaultStruct struct {
BindHeader bool
}
type Context ¶
type Context struct { Route *RouteInfo Writer http.ResponseWriter Request *http.Request Crypto *cryptoImpl // contains filtered or unexported fields }
Context represents a request & response Context.
func GetContext ¶
GetContext pulls the URL parameters from a request context, or returns nil if none are present.
func (*Context) AddHeader ¶
AddHeader adds the key, value pair to the header. It appends to any existing values associated with key. The key is case insensitive; it is canonicalized by CanonicalHeaderKey.
func (*Context) BadRequest ¶ added in v1.0.3
func (ctx *Context) BadRequest()
BadRequest replies to the request with an HTTP 400 bad request error.
func (*Context) BeforeSend ¶
BeforeSend Registers a callback to be invoked before the response is sent.
Callbacks are invoked in the reverse order they are defined (callbacks defined first are invoked last).
func (*Context) Bind ¶ added in v1.0.3
Bind checks the Method and Content-Type to select a binding engine automatically, Depending on the "Content-Type" header different bindings are used, for example:
"application/json" --> JSON binding "application/xml" --> XML binding
It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
func (*Context) BindForm ¶ added in v1.0.3
BindForm is a shortcut for c.MustBindWith(obj, BindingForm).
func (*Context) BindFormMultipart ¶ added in v1.0.3
BindFormMultipart is a shortcut for c.MustBindWith(obj, BindingFormMultipart).
func (*Context) BindFormPost ¶ added in v1.0.3
BindFormPost is a shortcut for c.MustBindWith(obj, BindingFormPost).
func (*Context) BindHeader ¶ added in v1.0.3
BindHeader is a shortcut for c.MustBindWith(obj, BindingHeader).
func (*Context) BindJSON ¶ added in v1.0.3
BindJSON is a shortcut for c.MustBindWith(obj, BindingJSON).
func (*Context) BindPath ¶ added in v1.0.3
BindPath is a shortcut for c.MustBindWith(obj, BindingPath).
func (*Context) BindQuery ¶ added in v1.0.3
BindQuery is a shortcut for c.MustBindWith(obj, BindingQuery).
func (*Context) BindXML ¶ added in v1.0.3
BindXML is a shortcut for c.MustBindWith(obj, binding.BindXML).
func (*Context) ContentType ¶ added in v1.0.3
ContentType set the response content type
func (*Context) Created ¶ added in v1.0.3
func (ctx *Context) Created()
Created sends an HTTP response header with the 201 Created status code.
func (*Context) Error ¶
Error replies to the request with the specified error message and HTTP code. It does not otherwise end the request; the caller should ensure no further writes are done to w. The error message should be plain text.
func (*Context) Forbidden ¶ added in v1.0.3
func (ctx *Context) Forbidden()
Unauthorized replies to the request with an HTTP 403 Forbidden error.
func (*Context) GetContentType ¶ added in v1.0.3
GetContentType returns the Content-Type header of the request.
func (*Context) GetCookie ¶
GetCookie returns the named cookie provided in the request or nil if not found. If multiple cookies match the given name, only one cookie will be returned.
func (*Context) GetHeader ¶
GetHeader gets the first value associated with the given key. If there are no values associated with the key, GetHeader returns "". It is case insensitive; textproto.CanonicalMIMEHeaderKey is used to canonicalize the provided key. Get assumes that all keys are stored in canonical form. To use non-canonical keys, access the map directly.
func (*Context) GetParam ¶
GetParam returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.
func (*Context) GetParamByIndex ¶
GetParamByIndex get one parameter per index
func (*Context) Header ¶
Header returns the header map that will be sent by WriteHeader. The Header map also is the mechanism with which Handlers can set HTTP trailers.
Changing the header map after a call to WriteHeader (or Write) has no effect unless the HTTP status code was of the 1xx class or the modified headers are trailers.
There are two ways to set Trailers. The preferred way is to predeclare in the headers which trailers you will later send by setting the "Trailer" header to the names of the trailer keys which will come later. In this case, those keys of the Header map are treated as if they were trailers. See the example. The second way, for trailer keys not known to the Handle until after the first Write, is to prefix the Header map keys with the TrailerPrefix constant value. See TrailerPrefix.
To suppress automatic response headers (such as "Date"), set their value to nil.
func (*Context) InternalServerError ¶ added in v1.0.3
func (ctx *Context) InternalServerError()
InternalServerError replies to the request with an HTTP 500 Internal Server Error error.
func (*Context) Json ¶ added in v1.0.3
Json encode and writes the data to the connection as part of an HTTP reply.
The Content-Length and Content-Type headers are added automatically.
func (*Context) MustBindWith ¶ added in v1.0.3
MustBindWith binds the passed struct pointer using the specified binding engine. It will abort the request with HTTP 400 if any error occurs. See the binding package.
func (*Context) NewUID ¶
NewUID get a new KSUID.
KSUID is for K-Sortable Unique IDentifier. It is a kind of globally unique identifier similar to a RFC 4122 UUID, built from the ground-up to be "naturally" sorted by generation timestamp without any special type-aware logic.
func (*Context) NoContent ¶ added in v1.0.3
func (ctx *Context) NoContent()
Created sends an HTTP response header with the 204 No Content status code.
func (*Context) NotFound ¶
func (ctx *Context) NotFound()
NotFound replies to the request with an HTTP 404 not found error.
func (*Context) NotImplemented ¶ added in v1.0.3
func (ctx *Context) NotImplemented()
NotImplemented replies to the request with an HTTP 501 Not Implemented error.
func (*Context) OK ¶ added in v1.0.3
func (ctx *Context) OK()
Created sends an HTTP response header with the 200 OK status code.
func (*Context) QueryParam ¶ added in v1.0.3
@TODO: cache
func (*Context) QueryParamInt ¶ added in v1.0.3
func (*Context) Redirect ¶
Redirect replies to the request with a redirect to url, which may be a path relative to the request path.
The provided code should be in the 3xx range and is usually StatusMovedPermanently, StatusFound or StatusSeeOther.
If the Content-Type header has not been set, Redirect sets it to "text/html; charset=utf-8" and writes a small HTML body.
Setting the Content-Type header to any value, including nil, disables that behavior.
func (*Context) RemoveCookie ¶ added in v1.0.3
RemoveCookie delete a cookie by name
func (*Context) ServeContent ¶ added in v1.0.3
ServeContent replies to the request using the content in the provided. The main benefit of ServeContent over io.Copy is that it handles Range requests properly, sets the MIME type, and handles If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since, and If-Range requests.
If the response's Content-Type header is not set, ServeContent first tries to deduce the type from name's file extension and, if that fails, falls back to reading the first block of the content and passing it to DetectContentType. The name is otherwise unused; in particular it can be empty and is never sent in the response.
If modtime is not the zero time or Unix epoch, ServeContent includes it in a Last-Modified header in the response. If the request includes an If-Modified-Since header, ServeContent uses modtime to decide whether the content needs to be sent at all.
The content's Seek method must work: ServeContent uses a seek to the end of the content to determine its size.
If the caller has set w's ETag header formatted per RFC 7232, section 2.3, ServeContent uses it to handle requests using If-Match, If-None-Match, or If-Range.
func (*Context) ServiceUnavailable ¶ added in v1.0.3
func (ctx *Context) ServiceUnavailable()
ServiceUnavailable replies to the request with an HTTP 503 Service Unavailable error.
func (*Context) SetCookie ¶
SetCookie adds a Set-Cookie header to the provided ResponseWriter's headers. The provided cookie must have a valid Name. Invalid cookies may be silently dropped.
func (*Context) SetHeader ¶
SetHeader sets the header entries associated with key to the single element value. It replaces any existing values associated with key. The key is case insensitive; it is canonicalized by textproto.CanonicalMIMEHeaderKey. To use non-canonical keys, assign to the map directly.
func (*Context) ShouldBind ¶ added in v1.0.3
ShouldBind checks the Method and Content-Type to select a binding engine automatically, Depending on the "Content-Type" header different bindings are used, for example:
"application/json" --> JSON binding "application/xml" --> XML binding
It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. Like c.Bind() but this method does not set the response status code to 400 or abort if input is not valid.
func (*Context) ShouldBindForm ¶ added in v1.0.3
ShouldBindForm is a shortcut for c.ShouldBindWith(obj, BindingForm).
func (*Context) ShouldBindFormMultipart ¶ added in v1.0.3
ShouldBindFormMultipart is a shortcut for c.ShouldBindWith(obj, BindingFormMultipart).
func (*Context) ShouldBindFormPost ¶ added in v1.0.3
ShouldBindFormPost is a shortcut for c.ShouldBindWith(obj, BindingFormPost).
func (*Context) ShouldBindHeader ¶ added in v1.0.3
ShouldBindHeader is a shortcut for c.ShouldBindWith(obj, BindingHeader).
func (*Context) ShouldBindJSON ¶ added in v1.0.3
ShouldBindJSON is a shortcut for c.ShouldBindWith(obj, BindingJSON).
func (*Context) ShouldBindPath ¶ added in v1.0.3
ShouldBindPath is a shortcut for c.ShouldBindWith(obj, BindingPath).
func (*Context) ShouldBindQuery ¶ added in v1.0.3
ShouldBindQuery is a shortcut for c.ShouldBindWith(obj, BindingQuery).
func (*Context) ShouldBindWith ¶ added in v1.0.3
ShouldBindWith binds the passed struct pointer using the specified binding engine. See the binding package.
func (*Context) ShouldBindXML ¶ added in v1.0.3
ShouldBindXML is a shortcut for c.ShouldBindWith(obj, BindingXML).
func (*Context) Status ¶ added in v1.0.3
WriteHeader sends an HTTP response header with the provided status code.
func (*Context) TooManyRequests ¶ added in v1.0.3
func (ctx *Context) TooManyRequests()
TooManyRequests replies to the request with an HTTP 429 Too Many Requests error.
func (*Context) Unauthorized ¶ added in v1.0.3
func (ctx *Context) Unauthorized()
Unauthorized replies to the request with an HTTP 401 Unauthorized error.
func (*Context) UserAgent ¶ added in v1.0.3
UserAgent returns the client's User-Agent, if sent in the request.
func (*Context) WithParams ¶
func (*Context) Write ¶
Write writes the data to the connection as part of an HTTP reply.
If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType. Additionally, if the total size of all written data is under a few KB and there are no Flush calls, the Content-Length header is added automatically.
Depending on the HTTP protocol version and the client, calling Write or WriteHeader may prevent future reads on the Request.Body. For HTTP/1.x requests, handlers should read any needed request body data before writing the response. Once the headers have been flushed (due to either an explicit Flusher.Flush call or writing enough data to trigger a flush), the request body may be unavailable. For HTTP/2 requests, the Go HTTP server permits handlers to continue to read the request body while concurrently writing the response. However, such behavior may not be supported by all HTTP/2 clients. Handlers should read before writing if possible to maximize compatibility.
func (*Context) WriteCalled ¶ added in v1.0.3
WriteCalled returns true if the ctx.Writer.Write method was called
func (*Context) WriteHeader ¶
WriteHeader sends an HTTP response header with the provided status code.
If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes or 1xx informational responses.
The provided code must be a valid HTTP 1xx-5xx status code. Any number of 1xx headers may be written, followed by at most one 2xx-5xx header. 1xx headers are sent immediately, but 2xx-5xx headers may be buffered. Use the Flusher interface to send buffered data. The header map is cleared when 2xx-5xx headers are sent, but not with 1xx headers.
The server will automatically send a 100 (Continue) header on the first read from the request body if the request has an "Expect: 100-continue" header.
func (*Context) WriteHeaderCalled ¶ added in v1.0.3
WriteCalled returns true if the ctx.Writer.WriteHeader method was called
func (*Context) WriteStarted ¶ added in v1.0.3
WriteStarted returns true if the ctx.Writer.Write or ctx.Writer.WriteHeader method was called
type Group ¶
type Group interface { GET(route string, handle any) error HEAD(route string, handle any) error OPTIONS(route string, handle any) error POST(route string, handle any) error PUT(route string, handle any) error PATCH(route string, handle any) error DELETE(route string, handle any) error Use(args ...any) Group Group(route string) Group Handle(method string, route string, handle any) error Configure(route string, configurator RouteConfigurator) }
type JsonSerializer ¶
type JsonSerializer struct { }
type Middleware ¶
type MiddlewareHandler ¶
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry is an algorithm-independent framework for recording routes. This division allows us to explore different algorithms without breaking the contract.
type ResponseWriterSpy ¶
type ResponseWriterSpy struct { http.ResponseWriter // contains filtered or unexported fields }
func (*ResponseWriterSpy) Status ¶ added in v1.0.4
func (w *ResponseWriterSpy) Status() int
func (*ResponseWriterSpy) WriteHeader ¶
func (w *ResponseWriterSpy) WriteHeader(status int)
type Route ¶
type Route struct { Info *RouteInfo Handle Handle Middlewares []*Middleware // contains filtered or unexported fields }
Route control of a registered route
type RouteConfigurator ¶
type RouteInfo ¶ added in v1.0.4
type RouteInfo struct {
// contains filtered or unexported fields
}
RouteInfo represents all useful information about a dynamic path (used by handlers)
func ParseRouteInfo ¶ added in v1.0.4
ParseRouteInfo obtém informações sobre um path dinamico.
func (*RouteInfo) HasParameter ¶ added in v1.0.4
func (*RouteInfo) HasWildcard ¶ added in v1.0.4
func (RouteInfo) Match ¶ added in v1.0.4
Match checks if the patch is compatible, performs the extraction of parameters
func (*RouteInfo) Matches ¶ added in v1.0.4
Matches checks if this path is applicable over the other. Used for registering middlewares in routes
func (*RouteInfo) ParamsIndex ¶ added in v1.0.4
func (*RouteInfo) ReplacePath ¶ added in v1.0.4
type RouteStorage ¶
type RouteStorage struct {
// contains filtered or unexported fields
}
type Router ¶
type Router struct { Crypto cryptoImpl // ReqContext optionally specifies a function that modifies // the context used for the request. ReqContext func(*Context) context.Context // If enabled, the router automatically replies to OPTIONS requests. // Custom OPTIONS handlers take priority over automatic replies. HandleOPTIONS bool // 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 308 for all other request methods. // For example /FOO and /..//Foo could be redirected to /foo. // RedirectTrailingSlash is independent of this option. RedirectFixedPath bool // 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 308 for all other request methods. RedirectTrailingSlash bool // 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 NotFoundHandler handler. HandleMethodNotAllowed bool // Function to handle panics recovered from http handlers. // It should be used to generate a error page and return the http error code 500 (Internal Server Error). // The handler can be used to keep your server from crashing because of unrecovered panics. PanicHandler func(http.ResponseWriter, *http.Request, any) // Function to handle errors recovered from http handlers and middlewares. // The handler can be used to do global error handling (not handled in middlewares) ErrorHandler func(*Context, error) // Configurable http.Handler function which is called when no matching route is found. If it is not set, http.NotFound is // used. NotFoundHandler http.Handler // An optional http.Handler function that is called on automatic OPTIONS requests. // The handler is only called if HandleOPTIONS is true and no OPTIONS handler for the specific path was set. // The "Allowed" header is set before calling the handler. GlobalOPTIONSHandler http.Handler // Configurable http.Handler function which is called when a request cannot be routed and HandleMethodNotAllowed is true. // If it is not set, http.Error with http.StatusMethodNotAllowed is used. // The "Allow" header with allowed request methods is set before the handler is called. MethodNotAllowedHandler http.Handler // contains filtered or unexported fields }
Router is a high-performance router.
func (*Router) Configure ¶
func (r *Router) Configure(route string, configurator RouteConfigurator)
Configure allows a RouteConfigurator to perform route configurations
func (*Router) DELETE ¶
DELETE is a shortcut for router.handleFunc(http.MethodDelete, Route, handle)
func (*Router) Lookup ¶
Lookup finds the Route and parameters for the given Route and assigns them to the given Context.
func (*Router) OPTIONS ¶
OPTIONS is a shortcut for router.handleFunc(http.MethodOptions, Route, handle)
func (*Router) ServeHTTP ¶
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServeHTTP responds to the given request.
func (*Router) Use ¶
Use registers a middleware routeT that will match requests with the provided prefix (which is optional and defaults to "/*").
router.Use(func(ctx *chain.Context) error { return ctx.NextFunc() }) router.Use(firstMiddleware, secondMiddleware) app.Use("/api", func(ctx *chain.Context) error { return ctx.NextFunc() }) app.Use("GET", "/api", func(ctx *chain.Context) error { return ctx.NextFunc() }) app.Use("GET", "/files/*filepath", func(ctx *chain.Context) error { println(ctx.GetParam("filepath")) return ctx.NextFunc() })
type RouterGroup ¶
type RouterGroup struct {
// contains filtered or unexported fields
}
func (*RouterGroup) Configure ¶
func (r *RouterGroup) Configure(route string, configurator RouteConfigurator)
func (*RouterGroup) Group ¶
func (r *RouterGroup) Group(route string) Group
func (*RouterGroup) Handle ¶
func (r *RouterGroup) Handle(method string, route string, handle any) error
func (*RouterGroup) Use ¶
func (r *RouterGroup) Use(args ...any) Group
type SecretKeySyncFunc ¶
type SecretKeySyncFunc func(key string)
type Serializer ¶
type SliceValidationError ¶ added in v1.0.3
type SliceValidationError []error
func (SliceValidationError) Error ¶ added in v1.0.3
func (err SliceValidationError) Error() string
Error concatenates all error elements in SliceValidationError into a single string separated by \n.
type StructValidator ¶ added in v1.0.3
type StructValidator interface { // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right. // If the received type is a slice|array, the validation should be performed travel on every element. // If the received type is not a struct or slice|array, any validation should be skipped and nil must be returned. // If the received type is a struct or pointer to a struct, the validation should be performed. // If the struct is not valid or the validation itself fails, a descriptive error should be returned. // Otherwise nil must be returned. ValidateStruct(any) error // Engine returns the underlying validator engine which powers the // StructValidator implementation. Engine() any }
StructValidator is the minimal interface which needs to be implemented in order for it to be used as the validator engine for ensuring the correctness of the request. Gin provides a default implementation for this using https://github.com/go-playground/validator/tree/v10.6.1.
var Validator StructValidator = &defaultValidator{}
Validator is the default validator which implements the StructValidator interface. It uses https://github.com/go-playground/validator/tree/v10.6.1 under the hood.
Source Files ¶
- chain.go
- context.go
- context_binding.go
- context_binding_form.go
- context_binding_form_mapping.go
- context_binding_header.go
- context_binding_json.go
- context_binding_path.go
- context_binding_query.go
- context_binding_validator.go
- context_binding_xml.go
- context_request.go
- context_response.go
- crypto.go
- crypto_keyring.go
- crypto_shortcuts.go
- node.go
- registry.go
- response_writer.go
- route.go
- route_info.go
- route_storage.go
- router.go
- router_group.go
- utils.go