Documentation ¶
Index ¶
- Constants
- func IsDebugging() bool
- func Mode() string
- func SetMode(value string)
- type Accounts
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithStatus(code int)
- func (c *Context) Bind(obj interface{}) bool
- func (c *Context) BindWith(obj interface{}, b binding.Binding) bool
- func (c *Context) ClientIP() string
- func (c *Context) Copy() *Context
- func (c *Context) Data(code int, contentType string, data []byte)
- func (c *Context) EnsureBody(item interface{}) bool
- func (c *Context) Error(err error, meta interface{})
- func (c *Context) ErrorTyped(err error, typ uint32, meta interface{})
- func (c *Context) Fail(code int, err error)
- func (c *Context) File(filepath string)
- func (c *Context) Get(key string) (interface{}, error)
- func (c *Context) HTML(code int, name string, obj interface{})
- func (c *Context) JSON(code int, obj interface{})
- func (c *Context) LastError() error
- func (c *Context) MustGet(key string) interface{}
- func (c *Context) Negotiate(code int, config Negotiate)
- func (c *Context) NegotiateFormat(offered ...string) string
- func (c *Context) Next()
- func (c *Context) ParseBody(item interface{}) error
- func (c *Context) Redirect(code int, location string)
- func (c *Context) Render(code int, render render.Render, obj ...interface{})
- func (c *Context) Set(key string, item interface{})
- func (c *Context) SetAccepted(formats ...string)
- func (c *Context) String(code int, format string, values ...interface{})
- func (c *Context) XML(code int, obj interface{})
- type Engine
- func (engine *Engine) LoadHTMLFiles(files ...string)
- func (engine *Engine) LoadHTMLGlob(pattern string)
- func (engine *Engine) LoadHTMLTemplates(pattern string)
- func (engine *Engine) NoRoute(handlers ...HandlerFunc)
- func (engine *Engine) NotFound404(handlers ...HandlerFunc)
- func (engine *Engine) Run(addr string)
- func (engine *Engine) RunTLS(addr string, cert string, key string)
- func (engine *Engine) ServeFiles(path string, root http.FileSystem)
- func (engine *Engine) ServeHTTP(writer http.ResponseWriter, request *http.Request)
- func (engine *Engine) SetHTMLTemplate(templ *template.Template)
- func (engine *Engine) Use(middlewares ...HandlerFunc)
- type H
- type HandlerFunc
- type Negotiate
- type ResponseWriter
- type RouterGroup
- func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc)
- func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc)
- func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup
- func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc)
- func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers []HandlerFunc)
- func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc)
- func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc)
- func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc)
- func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc)
- func (group *RouterGroup) Static(relativePath, root string)
- func (group *RouterGroup) Use(middlewares ...HandlerFunc)
Constants ¶
const ( ErrorTypeInternal = 1 << iota ErrorTypeExternal = 1 << iota ErrorTypeAll = 0xffffffff )
const ( AbortIndex = math.MaxInt8 / 2 MIMEJSON = "application/json" MIMEHTML = "text/html" MIMEXML = "application/xml" MIMEXML2 = "text/xml" MIMEPlain = "text/plain" MIMEPOSTForm = "application/x-www-form-urlencoded" )
const ( DebugMode string = "debug" ReleaseMode string = "release" TestMode string = "test" )
const (
AuthUserKey = "user"
)
const GIN_MODE = "GIN_MODE"
const (
NoWritten = -1
)
Variables ¶
This section is empty.
Functions ¶
func IsDebugging ¶
func IsDebugging() bool
Types ¶
type Context ¶
type Context struct { Request *http.Request Writer ResponseWriter Keys map[string]interface{} Errors errorMsgs Params httprouter.Params Engine *Engine // contains filtered or unexported fields }
Context is the most important part of gin. It allows us to pass variables between middleware, manage the flow, validate the JSON of a request and render a JSON response for example.
func (*Context) Abort ¶
func (c *Context) Abort()
Forces the system to do not continue calling the pending handlers in the chain.
func (*Context) AbortWithStatus ¶
Same than AbortWithStatus() but also writes the specified response status code. For example, the first handler checks if the request is authorized. If it's not, context.AbortWithStatus(401) should be called.
func (*Context) Bind ¶
This function 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 else --> returns an error if 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) EnsureBody ¶
DEPRECATED, use Bind() instead. Like ParseBody() but this method also writes a 400 error if the json is not valid.
func (*Context) Error ¶
Attaches an error to the current context. The error is pushed to a list of errors. 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) ErrorTyped ¶
func (*Context) Fail ¶
Fail is the same as Abort plus an error message. Calling `context.Fail(500, err)` is equivalent to: ``` context.Error("Operation aborted", err) context.AbortWithStatus(500) ```
func (*Context) Get ¶
Get returns the value for the given key or an error if the key does not exist.
func (*Context) HTML ¶
Renders the HTTP template specified by its file name. It also updates the HTTP code and sets the Content-Type as "text/html". See http://golang.org/doc/articles/wiki/
func (*Context) JSON ¶
Serializes the given struct as JSON into the response body in a fast and efficient way. It also sets the Content-Type as "application/json".
func (*Context) MustGet ¶
MustGet returns the value for the given key or panics if the value doesn't exist.
func (*Context) NegotiateFormat ¶
func (*Context) Next ¶
func (c *Context) Next()
Next should be used only in the middlewares. It executes the pending handlers in the chain inside the calling handler. See example in github.
func (*Context) ParseBody ¶
DEPRECATED use bindings directly Parses the body content as a JSON input. It decodes the json payload into the struct specified as a pointer.
func (*Context) Set ¶
Sets a new pair key/value just for the specified context. It also lazy initializes the hashmap.
func (*Context) SetAccepted ¶
type Engine ¶
type Engine struct { *RouterGroup HTMLRender render.Render Default404Body []byte // contains filtered or unexported fields }
Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares.
func Default ¶
func Default() *Engine
Returns a Engine instance with the Logger and Recovery already attached.
func New ¶
func New() *Engine
Returns a new blank Engine instance without any middleware attached. The most basic configuration
func (*Engine) LoadHTMLFiles ¶
func (*Engine) LoadHTMLGlob ¶
func (*Engine) LoadHTMLTemplates ¶
DEPRECATED use gin.LoadHTMLGlob() or gin.LoadHTMLFiles() instead
func (*Engine) NoRoute ¶
func (engine *Engine) NoRoute(handlers ...HandlerFunc)
Adds handlers for NoRoute. It return a 404 code by default.
func (*Engine) NotFound404 ¶
func (engine *Engine) NotFound404(handlers ...HandlerFunc)
DEPRECATED. Use NotFound() instead
func (*Engine) ServeFiles ¶
func (engine *Engine) ServeFiles(path string, root http.FileSystem)
DEPRECATED use gin.Static() instead ServeFiles serves files from the given file system root. The path must end with "/*filepath", files are then served from the local path /defined/root/dir/*filepath. For example if root is "/etc" and *filepath is "passwd", the local file "/etc/passwd" would be served. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler. To use the operating system's file system implementation, use http.Dir:
router.ServeFiles("/src/*filepath", http.Dir("/var/www"))
func (*Engine) ServeHTTP ¶
func (engine *Engine) ServeHTTP(writer http.ResponseWriter, request *http.Request)
ServeHTTP makes the router implement the http.Handler interface.
func (*Engine) SetHTMLTemplate ¶
func (*Engine) Use ¶
func (engine *Engine) Use(middlewares ...HandlerFunc)
type H ¶
type H map[string]interface{}
func (H) MarshalXML ¶
Allows type H to be used with xml.Marshal
type HandlerFunc ¶
type HandlerFunc func(*Context)
func BasicAuth ¶
func BasicAuth(accounts Accounts) HandlerFunc
Implements a basic Basic HTTP Authorization. It takes as argument a map[string]string where the key is the user name and the value is the password.
func ErrorLogger ¶
func ErrorLogger() HandlerFunc
func ErrorLoggerT ¶
func ErrorLoggerT(typ uint32) HandlerFunc
func Logger ¶
func Logger() HandlerFunc
func Recovery ¶
func Recovery() HandlerFunc
Recovery returns a middleware that recovers from any panics and writes a 500 if there was one. While Martini is in development mode, Recovery will also output the panic as HTML.
type ResponseWriter ¶
type RouterGroup ¶
type RouterGroup struct { Handlers []HandlerFunc // contains filtered or unexported fields }
Used internally to configure router, a RouterGroup is associated with a prefix and an array of handlers (middlewares)
func (*RouterGroup) DELETE ¶
func (group *RouterGroup) DELETE(relativePath string, handlers ...HandlerFunc)
DELETE is a shortcut for router.Handle("DELETE", path, handle)
func (*RouterGroup) GET ¶
func (group *RouterGroup) GET(relativePath string, handlers ...HandlerFunc)
GET is a shortcut for router.Handle("GET", path, handle)
func (*RouterGroup) Group ¶
func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup
Creates a new router group. You should add all the routes that have common middlwares or the same path prefix. For example, all the routes that use a common middlware for authorization could be grouped.
func (*RouterGroup) HEAD ¶
func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc)
HEAD is a shortcut for router.Handle("HEAD", path, handle)
func (*RouterGroup) Handle ¶
func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers []HandlerFunc)
Handle registers a new request handle and middlewares with the given path and method. The last handler should be the real handler, the other ones should be middlewares that can and should be shared among different routes. See the example code in github.
For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.
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 (*RouterGroup) OPTIONS ¶
func (group *RouterGroup) OPTIONS(relativePath string, handlers ...HandlerFunc)
OPTIONS is a shortcut for router.Handle("OPTIONS", path, handle)
func (*RouterGroup) PATCH ¶
func (group *RouterGroup) PATCH(relativePath string, handlers ...HandlerFunc)
PATCH is a shortcut for router.Handle("PATCH", path, handle)
func (*RouterGroup) POST ¶
func (group *RouterGroup) POST(relativePath string, handlers ...HandlerFunc)
POST is a shortcut for router.Handle("POST", path, handle)
func (*RouterGroup) PUT ¶
func (group *RouterGroup) PUT(relativePath string, handlers ...HandlerFunc)
PUT is a shortcut for router.Handle("PUT", path, handle)
func (*RouterGroup) Static ¶
func (group *RouterGroup) Static(relativePath, root string)
Static serves files from the given file system root. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler. To use the operating system's file system implementation, use :
router.Static("/static", "/var/www")
func (*RouterGroup) Use ¶
func (group *RouterGroup) Use(middlewares ...HandlerFunc)
Adds middlewares to the group, see example code in github.