Documentation ¶
Index ¶
- Constants
- func Validate(c *Context, obj interface{}) error
- 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) ErrorLogger() HandlerFunc
- 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) Next()
- func (c *Context) ParseBody(item interface{}) error
- func (c *Context) Set(key string, value 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 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(p string, handlers ...HandlerFunc)
- func (group *RouterGroup) PUT(path string, handlers ...HandlerFunc)
- func (group *RouterGroup) Use(middleware ...HandlerFunc)
Constants ¶
const (
AbortIndex = math.MaxInt8 / 2
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
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 engine. it allow 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) shold 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 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) ErrorLogger ¶
func (c *Context) ErrorLogger() HandlerFunc
func (*Context) Fail ¶
Fail is the same than 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 html template specified by his file name. It also update the http code and set the Content-Type as "application/html"
func (*Context) JSON ¶
Serializes the given struct as a 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 middleware. It executes the pending handlers in the chain inside the calling handler.
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 wrappers the blazing fast httprouter multiplexer and a list of global middleware
func Default ¶
func Default() *Engine
Return a Engine instance with the Logger and Recover middleware
func New ¶
func New() *Engine
Return a new Blank Engine without any middleware attached the most basic configuration
func (*Engine) LoadHTMLTemplate ¶
func (*Engine) NotFound404 ¶
func (engine *Engine) NotFound404(handlers ...HandlerFunc)
Add handlers for NotFound, It return 404 code by default
type ErrorMsg ¶
type ErrorMsg struct { Err string `json:"error"` Meta interface{} `json:"meta"` }
used internally to collect a error occurred during a http request
type HandlerFunc ¶
type HandlerFunc = func(*Context)
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.
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(middleware)
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 create add all the routes that share that have common middleware or same path prefix. For example, all the routes that use a common middleware for authorization could be grouped.
func (*RouterGroup) Handle ¶
func (group *RouterGroup) Handle(method, p string, handlers []HandlerFunc)
Handle registers a new request handler and middleware with the given path and method. The laster handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
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(p string, handlers ...HandlerFunc)
POST is the 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(middleware ...HandlerFunc)
Adds middleware to the group