Documentation
¶
Index ¶
- Constants
- func Validate(c *Context, obj interface{}) error
- type Account
- type Accounts
- type BasicAuthPair
- type Context
- func (c *Context) Abort(code int)
- func (c *Context) Data(code int, data []byte)
- func (c *Context) EnsureBody(item interface{}) bool
- func (c *Context) Error(err error, meta interface{})
- func (c *Context) Fail(code int, err error)
- func (c *Context) Get(key string) interface{}
- func (c *Context) HTML(code int, name string, data interface{})
- func (c *Context) JSON(code int, obj interface{})
- func (c *Context) LastError() error
- func (c *Context) Next()
- func (c *Context) ParseBody(item interface{}) error
- func (c *Context) Set(key string, item interface{})
- func (c *Context) String(code int, msg string)
- func (c *Context) XML(code int, obj interface{})
- type Engine
- type ErrorMsg
- type ErrorMsgs
- type H
- type HandlerFunc
- type Pairs
- type RouterGroup
- func (group *RouterGroup) DELETE(path string, handlers ...HandlerFunc)
- func (group *RouterGroup) GET(path string, handlers ...HandlerFunc)
- func (group *RouterGroup) Group(component string, handlers ...HandlerFunc) *RouterGroup
- func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc)
- func (group *RouterGroup) PATCH(path string, handlers ...HandlerFunc)
- func (group *RouterGroup) POST(path string, handlers ...HandlerFunc)
- func (group *RouterGroup) PUT(path string, handlers ...HandlerFunc)
- func (group *RouterGroup) Use(middlewares ...HandlerFunc)
Constants ¶
const (
AbortIndex = math.MaxInt8 / 2
)
const (
AuthUserKey = "user"
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BasicAuthPair ¶
type Context ¶
type Context struct {
Req *http.Request
Writer http.ResponseWriter
Keys map[string]interface{}
Errors ErrorMsgs
Params httprouter.Params
// 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 ¶
Forces the system to do not continue calling the pending handlers. For example, the first handler checks if the request is authorized. If it's not, context.Abort(401) should be called. The rest of pending handlers would never be called for that request.
func (*Context) EnsureBody ¶
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) 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.Abort(500) ```
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) 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 ¶
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.
type Engine ¶
type Engine struct {
*RouterGroup
HTMLTemplates *template.Template
// 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) LoadHTMLTemplates ¶
func (*Engine) NotFound404 ¶
func (engine *Engine) NotFound404(handlers ...HandlerFunc)
Adds handlers for NotFound. It return a 404 code by default.
func (*Engine) ServeFiles ¶
func (engine *Engine) ServeFiles(path string, root http.FileSystem)
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"))
type ErrorMsg ¶
type ErrorMsg struct {
Err string `json:"error"`
Meta interface{} `json:"meta"`
}
Used internally to collect errors that occurred during an http request.
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 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 Pairs ¶
type Pairs []BasicAuthPair
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(path string, handlers ...HandlerFunc)
DELETE is a shortcut for router.Handle("DELETE", path, handle)
func (*RouterGroup) GET ¶
func (group *RouterGroup) GET(path string, handlers ...HandlerFunc)
GET is a shortcut for router.Handle("GET", path, handle)
func (*RouterGroup) Group ¶
func (group *RouterGroup) Group(component 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) Handle ¶
func (group *RouterGroup) Handle(method, p 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) PATCH ¶
func (group *RouterGroup) PATCH(path string, handlers ...HandlerFunc)
PATCH is a shortcut for router.Handle("PATCH", path, handle)
func (*RouterGroup) POST ¶
func (group *RouterGroup) POST(path string, handlers ...HandlerFunc)
POST is a shortcut for router.Handle("POST", path, handle)
func (*RouterGroup) PUT ¶
func (group *RouterGroup) PUT(path string, handlers ...HandlerFunc)
PUT is a shortcut for router.Handle("PUT", path, handle)
func (*RouterGroup) Use ¶
func (group *RouterGroup) Use(middlewares ...HandlerFunc)
Adds middlewares to the group, see example code in github.
