Documentation ¶
Index ¶
- Variables
- func Dir(root string, listDirectory bool) http.FileSystem
- func IsDebugging() bool
- type Array
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithError(code int, err error) *Error
- func (c *Context) AbortWithStatus(status int)
- func (c *Context) Bind(obj interface{}) error
- func (c *Context) BindJSON(obj interface{}) error
- func (c *Context) BindWith(obj interface{}, b binding.Binding) error
- func (c *Context) ClientIP() string
- func (c *Context) ContentType() string
- func (c *Context) Cookie(name string) (string, error)
- func (c *Context) Data(status int, contentType string, data []byte) error
- func (c *Context) Deadline() (deadline time.Time, ok bool)
- func (c *Context) Done() <-chan struct{}
- func (c *Context) Err() error
- func (c *Context) Error(err error) *Error
- func (c *Context) File(filePath string)
- func (c *Context) Get(key string) (value interface{}, exists bool)
- func (c *Context) GetPostForm(key string) (string, bool)
- func (c *Context) GetPostForms(key string) ([]string, bool)
- func (c *Context) GetQuery(key string) (string, bool)
- func (c *Context) GetQuerys(key string) ([]string, bool)
- func (c *Context) HTML(status int, name string, obj interface{}) error
- func (c *Context) Header(key, value string)
- func (c *Context) IsAborted() bool
- func (c *Context) JSON(status int, obj interface{}, indented ...bool) error
- func (c *Context) MustGet(key string) interface{}
- func (c *Context) Next()
- func (ctx *Context) Param(key string, defaults ...string) string
- func (ctx *Context) ParamBool(key string, defaults ...bool) bool
- func (ctx *Context) ParamEscape(key string, defaults ...string) string
- func (ctx *Context) ParamFloat32(key string, defaults ...float32) float32
- func (ctx *Context) ParamFloat64(key string, defaults ...float64) float64
- func (ctx *Context) ParamInt(key string, defaults ...int) int
- func (ctx *Context) ParamInt32(key string, defaults ...int32) int32
- func (ctx *Context) ParamInt64(key string, defaults ...int64) int64
- func (ctx *Context) ParamStrings(key string, defaults ...[]string) []string
- func (ctx *Context) ParamUint(key string, defaults ...uint) uint
- func (ctx *Context) ParamUint32(key string, defaults ...uint32) uint32
- func (ctx *Context) ParamUint64(key string, defaults ...uint64) uint64
- func (c *Context) PostForm(key string, defaultValue ...string) string
- func (c *Context) PostForms(key string) []string
- func (c *Context) Query(key string, defaultValue ...string) string
- func (c *Context) Querys(key string) []string
- func (c *Context) Redirect(code int, location string)
- func (c *Context) SSE(name string, message interface{}) error
- func (c *Context) Set(key string, value interface{})
- func (c *Context) SetCookie(cookie *Cookie)
- func (c *Context) Status(status int)
- func (c *Context) Stream(step func(w io.Writer) bool)
- func (c *Context) Text(status int, format string, data ...interface{}) error
- func (c *Context) Value(key interface{}) interface{}
- func (c *Context) XML(status int, obj interface{}) error
- func (c *Context) YAML(status int, obj interface{}) error
- type Cookie
- type Error
- type ErrorType
- type Errors
- type Handler
- type Map
- type Mel
- func (mel *Mel) GetVar(key string) (value interface{}, exists bool)
- func (mel *Mel) LoadTemplateGlob(pattern string)
- func (mel *Mel) LoadTemplates(files ...string)
- func (mel *Mel) MustGetVar(key string) interface{}
- func (mel *Mel) NoMethod(handlers ...Handler)
- func (mel *Mel) NoRoute(handlers ...Handler)
- func (mel *Mel) Run(addrs ...string) (err error)
- func (mel *Mel) RunTLS(addr string, certFile string, keyFile string) (err error)
- func (mel *Mel) RunUnix(file string) (err error)
- func (mel *Mel) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (mel *Mel) SetTemplate(template *template.Template)
- func (mel *Mel) SetVar(key string, value interface{})
- func (mel *Mel) Use(middleware ...Handler)
- type Object
- type Param
- type Params
- func (p *Params) Bool(key string) (bool, error)
- func (p *Params) Escape(key string) (string, error)
- func (p *Params) Float32(key string) (float32, error)
- func (p *Params) Float64(key string) (float64, error)
- func (p *Params) Get(key string) string
- func (p *Params) Int(key string) (int, error)
- func (p *Params) Int32(key string) (int32, error)
- func (p *Params) Int64(key string) (int64, error)
- func (p *Params) MustBool(key string, defaults ...bool) bool
- func (p *Params) MustEscape(key string, defaults ...string) string
- func (p *Params) MustFloat32(key string, defaults ...float32) float32
- func (p *Params) MustFloat64(key string, defaults ...float64) float64
- func (p *Params) MustInt(key string, defaults ...int) int
- func (p *Params) MustInt32(key string, defaults ...int32) int32
- func (p *Params) MustInt64(key string, defaults ...int64) int64
- func (p *Params) MustString(key string, defaults ...string) string
- func (p *Params) MustStrings(key string, defaults ...[]string) []string
- func (p *Params) MustUint(key string, defaults ...uint) uint
- func (p *Params) MustUint32(key string, defaults ...uint32) uint32
- func (p *Params) MustUint64(key string, defaults ...uint64) uint64
- func (p *Params) Set(key, value string)
- func (p *Params) String(key string) (string, error)
- func (p *Params) Strings(key string) ([]string, error)
- func (p *Params) Uint(key string) (uint, error)
- func (p *Params) Uint32(key string) (uint32, error)
- func (p *Params) Uint64(key string) (uint64, error)
- type ResponseWriter
- type Route
- type RouteKind
- type Router
- type RoutesGroup
- func (group *RoutesGroup) Any(relativePath string, handlers ...interface{})
- func (group *RoutesGroup) Delete(relativePath string, handlers ...interface{})
- func (group *RoutesGroup) Get(relativePath string, handlers ...interface{})
- func (group *RoutesGroup) Group(relativePath string, handlers ...Handler) *RoutesGroup
- func (group *RoutesGroup) Handle(httpMethod, relativePath string, handlers ...interface{})
- func (group *RoutesGroup) Head(relativePath string, handlers ...interface{})
- func (group *RoutesGroup) Options(relativePath string, handlers ...interface{})
- func (group *RoutesGroup) Patch(relativePath string, handlers ...interface{})
- func (group *RoutesGroup) Post(relativePath string, handlers ...interface{})
- func (group *RoutesGroup) Put(relativePath string, handlers ...interface{})
- func (group *RoutesGroup) StaticDir(relativePath, root string)
- func (group *RoutesGroup) StaticFile(relativePath, filePath string)
- func (group *RoutesGroup) Trace(relativePath string, handlers ...interface{})
- func (group *RoutesGroup) Use(middlewares ...Handler)
Constants ¶
This section is empty.
Variables ¶
var Methods = []string{
"GET",
"POST",
"HEAD",
"DELETE",
"PUT",
"OPTIONS",
"TRACE",
"PATCH",
}
Functions ¶
func Dir ¶
func Dir(root string, listDirectory bool) http.FileSystem
Dir returns a http.Filesystem that can be used by http.FileServer(). It is used interally in router.Static(). if listDirectory == true, then it works the same as http.Dir() otherwise it returns a filesystem that prevents http.FileServer() to list the directory files.
func IsDebugging ¶
func IsDebugging() bool
IsDebugging returns true if the framework is running in debug mode. Use SetMode(gin.Release) to switch to disable the debug mode.
Types ¶
type Context ¶
type Context struct { Request *http.Request Writer ResponseWriter Params Keys map[string]interface{} Errors *Mel // contains filtered or unexported fields }
func (*Context) Abort ¶
func (c *Context) Abort()
Abort prevents pending handlers from being called. Note that this will not stop the current handler. Let's say you have an authorization middleware that validates that the current request is authorized. If the authorization fails, call Abort to ensure the remaining handlers for this request are not called.
func (*Context) AbortWithError ¶
AbortWithError calls `AbortWithStatus()` and `Error()` internally. This method stops the chain, writes the status code and pushes the specified error to `c.Errors`. See Context.Error() for more details.
func (*Context) AbortWithStatus ¶
AbortWithStatus calls `Abort()` and writes the headers with the specified status code. For example, a failed attempt to authentificate a request could use: context.AbortWithStatus(401).
func (*Context) Bind ¶
Bind checks the Content-Type to select a binding engine automatically, Depending the "Content-Type" header different bindings are used:
"application/json" --> JSON binding "application/xml" --> XML binding
otherwise --> returns an error 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 ParseBody() but this method also writes a 400 error if the json is not valid.
func (*Context) BindWith ¶
BindWith binds the passed struct pointer using the specified binding engine. See the binding package.
func (*Context) ClientIP ¶
ClientIP implements a best effort algorithm to return the real client IP. It parses X-Real-IP and X-Forwarded-For in order to work properly with reverse-proxies such as nginx or haproxy.
func (*Context) ContentType ¶
ContentType returns the Content-Type header of the request.
func (*Context) Error ¶
Error adds an error to the context. It's a good idea to call Error for each error that occurred during the resolution of a request. A middleware can be used to collect all the errors and push them to a database together, print a log, or append it in the HTTP response.
func (*Context) Get ¶
Get returns the value for the given key, i.e., (value, true). If the value does not exists it returns (nil, false).
func (*Context) GetPostForms ¶
GetPostForms 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) GetQuery ¶
GetQuery returns the keyed url query value. If it exists, it returns `(value, true)` (even when the value is an empty string), othewise it returns `("", false)`.
GET /?name=Manu&lastname= ("Manu", true) == c.GetQuery("name") ("", false) == c.GetQuery("id") ("", true) == c.GetQuery("lastname")
func (*Context) GetQuerys ¶
GetQuerys 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) Header ¶
Header sets the value associated with key in the header. If value == "", it removes the key in the header.
func (*Context) MustGet ¶
MustGet returns the value for the given key if it exists, otherwise it panics.
func (*Context) Next ¶
func (c *Context) Next()
Next executes the pending handlers in the chain inside the calling handler. It should be used only inside middleware.
func (*Context) ParamEscape ¶
func (*Context) ParamFloat32 ¶
func (*Context) ParamFloat64 ¶
func (*Context) ParamStrings ¶
func (*Context) ParamUint32 ¶
func (*Context) ParamUint64 ¶
func (*Context) Query ¶
Query returns the keyed url query value if it exists. If the key does not exist, it returns the defaultValue string, or an empty string (no defaultValue specified).
GET /?name=Manu&lastname= c.Query("name") == "Manu" c.Query("lastname") == "" c.Query("id") == "" c.Query("name", "unknown") == "Manu" c.Query("lastname", "none") == "" c.Query("id", "none") == "none"
func (*Context) Querys ¶
Querys 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.
type Cookie ¶
type Cookie struct { Name string Value string Path string Domain string MaxAge int Secure bool HttpOnly bool }
Cookie represents an HTTP cookie as sent in the Set-Cookie header of an HTTP response or the Cookie header of an HTTP request. Fields are a subset of http.Cookie fields.
type Error ¶
func (*Error) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface
type Errors ¶
type Errors []*Error
func (Errors) ByType ¶
ByType returns a readonly copy filtered by the type. Example, ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic
func (Errors) Errors ¶
Errors returns a slice of all the error messages. Example:
c.Error(errors.New("first")) c.Error(errors.New("second")) c.Error(errors.New("third")) c.Errors.Errors() // == []string{"first", "second", "third"}
func (Errors) MarshalJSON ¶
type Mel ¶
type Mel struct { *Router RedirectTrailingSlash bool RedirectFixedPath bool HandleMethodNotAllowed bool ForwardedByClientIP bool Template *template.Template // contains filtered or unexported fields }
func (*Mel) GetVar ¶
GetVar returns the value for the given key, i.e., (value, true). If the value does not exists it returns (nil, false).
func (*Mel) LoadTemplateGlob ¶
func (*Mel) LoadTemplates ¶
func (*Mel) MustGetVar ¶
MustGetVar returns the value for the given key if it exists, otherwise it panics.
func (*Mel) NoMethod ¶
NoMethod sets handlers for requests that match a route for another HTTP method. It return a 405 code by default.
func (*Mel) NoRoute ¶
NoRoute sets handlers for requests that match no route. It return a 404 code by default.
func (*Mel) Run ¶
Run attaches `mel` to a http.Server and starts listening and serving HTTP requests. Note: this method will block the calling goroutine indefinitely unless an error happens.
func (*Mel) RunTLS ¶
RunTLS attaches `mel` to a http.Server and starts listening and serving HTTPS (secure) requests. Note: this method will block the calling goroutine indefinitely unless an error happens.
func (*Mel) RunUnix ¶
RunUnix attaches `mel` to a http.Server and starts listening and serving HTTP requests through the specified unix socket (ie. a file). Note: this method will block the calling goroutine indefinitely unless an error happens.
func (*Mel) ServeHTTP ¶
func (mel *Mel) ServeHTTP(w http.ResponseWriter, req *http.Request)
ServerHTTP implements the http.Handler interface.
func (*Mel) SetTemplate ¶
func (*Mel) SetVar ¶
SetVar stores a new key/value pair exclusivelly for this app. It also lazy initializes mel.vars if it was not used previously.
type Params ¶
type Params []Param
Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.
func (*Params) MustStrings ¶
type ResponseWriter ¶
type ResponseWriter interface { http.ResponseWriter http.Flusher http.Hijacker http.CloseNotifier // Reset resets the embedded http.ResponseWriter. Reset(writer http.ResponseWriter) // Status returns the HTTP response status code, // or 0 if the response has not been written. // If status was passed in, reset the HTTP response status. Status(status ...int) int // Written returns whether or not the response has been written. Written() bool // WriteString writes the string into the response body. WriteString(string) (int, error) // Size returns the number of bytes already written into the response http body. Size() int }
ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response.
type Router ¶
type Router struct { RoutesGroup AllowCustomMethod bool RemoveTrailingSlash bool // contains filtered or unexported fields }
func (*Router) PrintTrees ¶
func (r *Router) PrintTrees()
type RoutesGroup ¶
type RoutesGroup struct { BasePath string Handlers []Handler // contains filtered or unexported fields }
RoutesGroup is used internally to configure router. A RoutesGroup is associated with a prefix and an array of handlers (middleware).
func (*RoutesGroup) Any ¶
func (group *RoutesGroup) Any(relativePath string, handlers ...interface{})
Any registers a route that matches all the HTTP methods.
func (*RoutesGroup) Delete ¶
func (group *RoutesGroup) Delete(relativePath string, handlers ...interface{})
func (*RoutesGroup) Get ¶
func (group *RoutesGroup) Get(relativePath string, handlers ...interface{})
func (*RoutesGroup) Group ¶
func (group *RoutesGroup) Group(relativePath string, handlers ...Handler) *RoutesGroup
Group creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
func (*RoutesGroup) Handle ¶
func (group *RoutesGroup) Handle(httpMethod, relativePath string, handlers ...interface{})
Handle registers a new request handle and middleware with the given path and method. The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).
func (*RoutesGroup) Head ¶
func (group *RoutesGroup) Head(relativePath string, handlers ...interface{})
func (*RoutesGroup) Options ¶
func (group *RoutesGroup) Options(relativePath string, handlers ...interface{})
func (*RoutesGroup) Patch ¶
func (group *RoutesGroup) Patch(relativePath string, handlers ...interface{})
func (*RoutesGroup) Post ¶
func (group *RoutesGroup) Post(relativePath string, handlers ...interface{})
func (*RoutesGroup) Put ¶
func (group *RoutesGroup) Put(relativePath string, handlers ...interface{})
func (*RoutesGroup) StaticDir ¶
func (group *RoutesGroup) StaticDir(relativePath, root string)
StaticDir serves files from the given file system root. router.StaticDir("/static", "/var/www")
func (*RoutesGroup) StaticFile ¶
func (group *RoutesGroup) StaticFile(relativePath, filePath string)
StaticFile registers a single route in order to server a single file of the local filesystem. router.StaticFile("favicon.ico", "./resources/favicon.ico")
func (*RoutesGroup) Trace ¶
func (group *RoutesGroup) Trace(relativePath string, handlers ...interface{})
func (*RoutesGroup) Use ¶
func (group *RoutesGroup) Use(middlewares ...Handler)
Use adds middleware to the group.