Documentation
¶
Index ¶
- Constants
- Variables
- type Binding
- type BindingBody
- type Config
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithError(code int, err error)
- func (c *Context) AbortWithStatus(code int)
- func (c *Context) AbortWithStatusJSON(code int, obj interface{})
- func (c *Context) Bind(obj interface{}) error
- func (c *Context) BindJSON(obj interface{}) error
- func (c *Context) BindQuery(obj interface{}) error
- func (c *Context) BindXML(obj interface{}) error
- func (c *Context) BindYAML(obj interface{}) error
- func (c *Context) ClientIP() string
- func (c *Context) Cookie(name string) (string, error)
- func (c *Context) Data(contentType string, data []byte)
- func (c *Context) DataFromReader(code int, contentLength int64, contentType string, reader io.Reader, ...)
- func (c *Context) DefaultPostForm(key, defaultValue string) string
- func (c *Context) DefaultQuery(key, defaultValue string) string
- func (c *Context) File(filepath string)
- func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
- func (c *Context) Get(key string) (interface{}, bool)
- func (c *Context) GetHeader(key string) string
- func (c *Context) GetPostForm(key string) (string, bool)
- func (c *Context) GetPostFormArray(key string) (values []string, ok bool)
- func (c *Context) GetPostFormMap(key string) (map[string]string, bool)
- func (c *Context) GetQuery(key string) (string, bool)
- func (c *Context) GetQueryArray(key string) (values []string, ok bool)
- func (c *Context) GetQueryMap(key string) (map[string]string, bool)
- func (c *Context) GetRawData() ([]byte, error)
- func (c *Context) HTML(html string)
- func (c *Context) Header(key, value string)
- func (c *Context) JSON(code int, obj interface{})
- func (c *Context) MultipartForm() (*multipart.Form, error)
- func (c *Context) MustBindWith(obj interface{}, b Binding) error
- func (c *Context) MustGet(key string) interface{}
- func (c *Context) Next()
- func (c *Context) Param(key string) string
- func (c *Context) PostForm(key string) string
- func (c *Context) PostFormArray(key string) (values []string)
- func (c *Context) PostFormMap(key string) (dicts map[string]string)
- func (c *Context) ProtoBuf(code int, obj proto.Message)
- func (c *Context) Query(key string) string
- func (c *Context) QueryArray(key string) (values []string)
- func (c *Context) QueryMap(key string) (dicts map[string]string)
- func (c *Context) Redirect(code int, location string)
- func (c *Context) RemoteIP() string
- func (c *Context) Request() *http.Request
- func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error
- func (c *Context) Set(key string, value interface{})
- func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
- func (c *Context) ShouldBind(obj interface{}) error
- func (c *Context) ShouldBindBodyWith(obj interface{}, bb BindingBody) error
- func (c *Context) ShouldBindJSON(obj interface{}) error
- func (c *Context) ShouldBindQuery(obj interface{}) error
- func (c *Context) ShouldBindWith(obj interface{}, b Binding) error
- func (c *Context) ShouldBindXML(obj interface{}) error
- func (c *Context) ShouldBindYAML(obj interface{}) error
- func (c *Context) Status(code int) *Context
- func (c *Context) Stream(step func(w io.Writer) bool)
- func (c *Context) String(code int, format string, values ...interface{})
- func (c *Context) UpgradeWebSocket(upgrader *websocket.Upgrader) (*websocket.Conn, error)
- func (c *Context) Writer() http.ResponseWriter
- func (c *Context) XML(code int, obj interface{})
- func (c *Context) YAML(code int, obj interface{})
- type Engine
- func (e *Engine) DELETE(path string, handler HandlerFunc)
- func (e *Engine) GET(path string, handler HandlerFunc)
- func (e *Engine) Group(prefix string) *RouterGroup
- func (e *Engine) HEAD(path string, handler HandlerFunc)
- func (e *Engine) OPTIONS(path string, handler HandlerFunc)
- func (e *Engine) OnBoot(eng gnet.Engine) gnet.Action
- func (e *Engine) OnClose(c gnet.Conn, err error) gnet.Action
- func (e *Engine) OnOpen(c gnet.Conn) ([]byte, gnet.Action)
- func (e *Engine) OnTraffic(c gnet.Conn) gnet.Action
- func (e *Engine) PATCH(path string, handler HandlerFunc)
- func (e *Engine) POST(path string, handler HandlerFunc)
- func (e *Engine) PUT(path string, handler HandlerFunc)
- func (e *Engine) Run(addr ...string) error
- func (e *Engine) SetTrustedProxies(trustedProxies []string) error
- func (e *Engine) Static(relativePath, root string)
- func (e *Engine) StaticFS(relativePath string, fs http.FileSystem)
- func (e *Engine) StaticFile(relativePath, filepath string)
- func (e *Engine) Use(middleware ...Middleware)
- type H
- type HandlerFunc
- type Middleware
- type Param
- type Params
- type Response
- type RouteInfo
- type Router
- type RouterGroup
- func (rg *RouterGroup) DELETE(path string, handler HandlerFunc)
- func (rg *RouterGroup) GET(path string, handler HandlerFunc)
- func (rg *RouterGroup) Group(prefix string) *RouterGroup
- func (rg *RouterGroup) HEAD(path string, handler HandlerFunc)
- func (rg *RouterGroup) OPTIONS(path string, handler HandlerFunc)
- func (rg *RouterGroup) PATCH(path string, handler HandlerFunc)
- func (rg *RouterGroup) POST(path string, handler HandlerFunc)
- func (rg *RouterGroup) PUT(path string, handler HandlerFunc)
- func (rg *RouterGroup) Use(middleware ...Middleware) *RouterGroup
Constants ¶
const ( PlatformGoogleAppEngine = "X-Appengine-Remote-Addr" PlatformCloudflare = "CF-Connecting-IP" PlatformFlyIO = "Fly-Client-IP" )
Trusted platform constants for common CDN/proxy providers
Variables ¶
var ( JSON = jsonBinding{} XML = xmlBinding{} Form = formBinding{} Query = queryBinding{} FormPost = formPostBinding{} FormMultipart = formMultipartBinding{} )
Binding instances
var (
CRLF = []byte("\r\n\r\n")
)
var Validator = validator.New()
Validator instance
Functions ¶
This section is empty.
Types ¶
type Binding ¶
Binding interface defines the binding behavior
func DefaultBinding ¶
DefaultBinding returns the appropriate binding based on method and content type
type BindingBody ¶
BindingBody interface extends Binding for body-based bindings
type Config ¶
type Config struct {
// Multicore indicates whether the server should be started in multi-core mode
Multicore bool
// JSONEncoder is the function used to encode JSON responses
// If not set, encoding/json.Marshal will be used
JSONEncoder func(v interface{}) ([]byte, error)
// JSONDecoder is the function used to decode JSON requests
// If not set, encoding/json.Unmarshal will be used
JSONDecoder func(data []byte, v interface{}) error
// XMLEncoder is the function used to encode XML responses
// If not set, encoding/xml.Marshal will be used
XMLEncoder func(v interface{}) ([]byte, error)
// YAMLEncoder is the function used to encode YAML responses
// If not set, gopkg.in/yaml.v3.Marshal will be used
YAMLEncoder func(v interface{}) ([]byte, error)
// ProtoBufEncoder is the function used to encode ProtoBuf responses
// If not set, google.golang.org/protobuf/proto.Marshal will be used
ProtoBufEncoder func(v proto.Message) ([]byte, error)
}
Config represents the configuration for the Engine
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context represents the context of the current HTTP request
func (*Context) AbortWithError ¶
AbortWithError aborts with error
func (*Context) AbortWithStatus ¶
AbortWithStatus aborts with a status code
func (*Context) AbortWithStatusJSON ¶
AbortWithStatusJSON aborts with a JSON response
func (*Context) ClientIP ¶
ClientIP implements a best effort algorithm to return the real client IP. It checks, in order:
- TrustedPlatform header (e.g., CF-Connecting-IP for Cloudflare)
- RemoteIPHeaders (X-Forwarded-For, X-Real-Ip) if the remote IP is a trusted proxy
- Falls back to Request.RemoteAddr
Use engine.SetTrustedProxies() to configure which proxies are trusted.
func (*Context) DataFromReader ¶
func (c *Context) DataFromReader(code int, contentLength int64, contentType string, reader io.Reader, extraHeaders map[string]string)
DataFromReader sends data from a reader with optional extra headers
func (*Context) DefaultPostForm ¶
DefaultPostForm returns the form parameter value or default value
func (*Context) DefaultQuery ¶
DefaultQuery returns the query parameter value or default value
func (*Context) FormFile ¶
func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
FormFile returns the first file for the provided form key
func (*Context) GetPostForm ¶
GetPostForm is like PostForm(key). It returns the specified key from a POST urlencoded form or multipart form when it exists `(value, true)` (even when the value is an empty string), otherwise it returns ("", false). For example, during a PATCH request to update the user's email:
email=mail@example.com --> ("mail@example.com", true) := GetPostForm("email") // set email to "mail@example.com"
email= --> ("", true) := GetPostForm("email") // set email to ""
--> ("", false) := GetPostForm("email") // do nothing with email
func (*Context) GetPostFormArray ¶
GetPostFormArray returns a slice of strings for a given form key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetPostFormMap ¶
GetPostFormMap returns a map for a given form key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetQuery ¶
GetQuery is like Query(), it returns the keyed url query value if it exists `(value, true)` (even when the value is an empty string), otherwise it returns `("", false)`. It is shortcut for `c.Request().URL.Query().Get(key)`
GET /?name=Manu&lastname=
("Manu", true) == c.GetQuery("name")
("", false) == c.GetQuery("id")
("", true) == c.GetQuery("lastname")
func (*Context) GetQueryArray ¶
GetQueryArray returns a slice of strings for a given query key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetQueryMap ¶
GetQueryMap returns a map for a given query key, plus a boolean value whether at least one value exists for the given key.
func (*Context) GetRawData ¶
GetRawData returns stream data.
func (*Context) MultipartForm ¶
MultipartForm returns the parsed multipart form, including file uploads
func (*Context) MustBindWith ¶
MustBindWith binds using the specified binding and aborts on error
func (*Context) PostFormArray ¶
PostFormArray returns a slice of strings for a given form key. The length of the slice depends on the number of params with the given key.
func (*Context) PostFormMap ¶
PostFormMap returns a map for a given form key.
func (*Context) QueryArray ¶
QueryArray returns a slice of strings for a given query key. The length of the slice depends on the number of params with the given key.
func (*Context) SaveUploadedFile ¶
func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error
SaveUploadedFile uploads the form file to specific dst
func (*Context) SetCookie ¶
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
SetCookie adds a Set-Cookie header to the ResponseWriter's headers
func (*Context) ShouldBind ¶
ShouldBind binds the request to obj based on content type
func (*Context) ShouldBindBodyWith ¶
func (c *Context) ShouldBindBodyWith(obj interface{}, bb BindingBody) error
ShouldBindBodyWith binds the request body and stores it in context for reuse
func (*Context) ShouldBindJSON ¶
ShouldBindJSON binds the request body as JSON
func (*Context) ShouldBindQuery ¶
ShouldBindQuery binds the query parameters
func (*Context) ShouldBindWith ¶
ShouldBindWith binds the request using the specified binding
func (*Context) ShouldBindXML ¶
ShouldBindXML binds the request body as XML
func (*Context) ShouldBindYAML ¶
ShouldBindYAML is not implemented yet
func (*Context) Stream ¶
Stream sends a streaming response using chunked transfer encoding The step function receives an io.Writer and should return true to continue streaming, false to stop
func (*Context) UpgradeWebSocket ¶
UpgradeWebSocket upgrades the HTTP connection to a WebSocket connection using the provided upgrader This method follows the Gin framework pattern for WebSocket upgrades
func (*Context) Writer ¶
func (c *Context) Writer() http.ResponseWriter
Writer returns an http.ResponseWriter compatible interface for websocket upgrading
type Engine ¶
type Engine struct {
gnet.BuiltinEventEngine
MaxMultipartMemory int64 // Maximum memory for multipart forms (default 32 MiB)
// ForwardedByClientIP if enabled, client IP will be parsed from the request's headers
// that match those stored at `RemoteIPHeaders`. If no IP was fetched, it falls back
// to the IP obtained from `Request.RemoteAddr`.
ForwardedByClientIP bool
// 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
// RemoteIPHeaders list of headers used to obtain the client IP when
// `ForwardedByClientIP` is `true` and `Request.RemoteAddr` is matched by at least one of the
// network origins of list defined by `SetTrustedProxies`.
RemoteIPHeaders []string
// contains filtered or unexported fields
}
Engine is the main router engine
func NewWithConfig ¶
NewWithConfig creates a new Engine instance with custom configuration
func (*Engine) DELETE ¶
func (e *Engine) DELETE(path string, handler HandlerFunc)
DELETE registers a DELETE route
func (*Engine) GET ¶
func (e *Engine) GET(path string, handler HandlerFunc)
GET registers a GET route
func (*Engine) Group ¶
func (e *Engine) Group(prefix string) *RouterGroup
Group creates a new router group with the given path prefix
func (*Engine) HEAD ¶
func (e *Engine) HEAD(path string, handler HandlerFunc)
HEAD registers a HEAD route
func (*Engine) OPTIONS ¶
func (e *Engine) OPTIONS(path string, handler HandlerFunc)
OPTIONS registers an OPTIONS route
func (*Engine) PATCH ¶
func (e *Engine) PATCH(path string, handler HandlerFunc)
PATCH registers a PATCH route
func (*Engine) POST ¶
func (e *Engine) POST(path string, handler HandlerFunc)
POST registers a POST route
func (*Engine) PUT ¶
func (e *Engine) PUT(path string, handler HandlerFunc)
PUT registers a PUT route
func (*Engine) SetTrustedProxies ¶
SetTrustedProxies set a list of network origins (IPv4 addresses, IPv4 CIDRs, IPv6 addresses or IPv6 CIDRs) from which to trust request's headers that contain alternative client IP when `ForwardedByClientIP` is enabled. `SetTrustedProxies(nil)` can disable the feature, client IP will be obtained directly from `Request.RemoteAddr`.
func (*Engine) StaticFS ¶
func (e *Engine) StaticFS(relativePath string, fs http.FileSystem)
StaticFS serves files from the given file system
func (*Engine) StaticFile ¶
StaticFile serves a single file
func (*Engine) Use ¶
func (e *Engine) Use(middleware ...Middleware)
Use adds middleware to the engine
type Middleware ¶
type Middleware func(HandlerFunc) HandlerFunc
Middleware defines the middleware function type
type Params ¶
type Params []Param
Params is a Params-slice, returned by the router. The slice is ordered, first URL parameter is also the first slice value.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response represents the HTTP response
type RouteInfo ¶
type RouteInfo struct {
Method string
Path string
Handler HandlerFunc
}
RouteInfo contains route information
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router handles route registration and matching
type RouterGroup ¶
type RouterGroup struct {
// contains filtered or unexported fields
}
RouterGroup represents a group of routes with a common prefix and middleware
func (*RouterGroup) DELETE ¶
func (rg *RouterGroup) DELETE(path string, handler HandlerFunc)
DELETE registers a DELETE route in the router group
func (*RouterGroup) GET ¶
func (rg *RouterGroup) GET(path string, handler HandlerFunc)
GET registers a GET route in the router group
func (*RouterGroup) Group ¶
func (rg *RouterGroup) Group(prefix string) *RouterGroup
Group creates a new nested router group with the given path prefix
func (*RouterGroup) HEAD ¶
func (rg *RouterGroup) HEAD(path string, handler HandlerFunc)
HEAD registers a HEAD route in the router group
func (*RouterGroup) OPTIONS ¶
func (rg *RouterGroup) OPTIONS(path string, handler HandlerFunc)
OPTIONS registers an OPTIONS route in the router group
func (*RouterGroup) PATCH ¶
func (rg *RouterGroup) PATCH(path string, handler HandlerFunc)
PATCH registers a PATCH route in the router group
func (*RouterGroup) POST ¶
func (rg *RouterGroup) POST(path string, handler HandlerFunc)
POST registers a POST route in the router group
func (*RouterGroup) PUT ¶
func (rg *RouterGroup) PUT(path string, handler HandlerFunc)
PUT registers a PUT route in the router group
func (*RouterGroup) Use ¶
func (rg *RouterGroup) Use(middleware ...Middleware) *RouterGroup
Use adds middleware to the router group