Comments + IRoutes + IRouter + unexported AbortIndex
This commit is contained in:
26
context.go
26
context.go
@ -18,6 +18,7 @@ import (
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// Content-Type MIME of the most common data formats
|
||||
const (
|
||||
MIMEJSON = binding.MIMEJSON
|
||||
MIMEHTML = binding.MIMEHTML
|
||||
@ -28,7 +29,7 @@ const (
|
||||
MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm
|
||||
)
|
||||
|
||||
const AbortIndex int8 = math.MaxInt8 / 2
|
||||
const abortIndex int8 = math.MaxInt8 / 2
|
||||
|
||||
// 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.
|
||||
@ -63,16 +64,18 @@ func (c *Context) reset() {
|
||||
c.Accepted = nil
|
||||
}
|
||||
|
||||
// Copy returns a copy of the current context that can be safely used outside the request's scope.
|
||||
// This have to be used then the context has to be passed to a goroutine.
|
||||
func (c *Context) Copy() *Context {
|
||||
var cp Context = *c
|
||||
cp.writermem.ResponseWriter = nil
|
||||
cp.Writer = &cp.writermem
|
||||
cp.index = AbortIndex
|
||||
cp.index = abortIndex
|
||||
cp.handlers = nil
|
||||
return &cp
|
||||
}
|
||||
|
||||
// Returns the main handle's name. For example if the handler is "handleGetUsers()", this
|
||||
// HandlerName returns the main handle's name. For example if the handler is "handleGetUsers()", this
|
||||
// function will return "main.handleGetUsers"
|
||||
func (c *Context) HandlerName() string {
|
||||
return nameOfFunction(c.handlers.Last())
|
||||
@ -93,27 +96,27 @@ func (c *Context) Next() {
|
||||
}
|
||||
}
|
||||
|
||||
// Returns if the currect context was aborted.
|
||||
// IsAborted returns true if the currect context was aborted.
|
||||
func (c *Context) IsAborted() bool {
|
||||
return c.index >= AbortIndex
|
||||
return c.index >= abortIndex
|
||||
}
|
||||
|
||||
// Stops the system to continue calling the pending handlers in the chain.
|
||||
// Abort stops the system to continue calling the pending handlers in the chain.
|
||||
// Let's say you have an authorization middleware that validates if the request is authorized
|
||||
// if the authorization fails (the password does not match). This method (Abort()) should be called
|
||||
// in order to stop the execution of the actual handler.
|
||||
func (c *Context) Abort() {
|
||||
c.index = AbortIndex
|
||||
c.index = abortIndex
|
||||
}
|
||||
|
||||
// It calls Abort() and writes the headers with the specified status code.
|
||||
// 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 (c *Context) AbortWithStatus(code int) {
|
||||
c.Writer.WriteHeader(code)
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
// It calls AbortWithStatus() and Error() internally. This method stops the chain, writes the status code and
|
||||
// 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 (c *Context) AbortWithError(code int, err error) *Error {
|
||||
@ -371,7 +374,7 @@ func (c *Context) Redirect(code int, location string) {
|
||||
})
|
||||
}
|
||||
|
||||
// Writes some data into the body stream and updates the HTTP code.
|
||||
// Data writes some data into the body stream and updates the HTTP code.
|
||||
func (c *Context) Data(code int, contentType string, data []byte) {
|
||||
c.Render(code, render.Data{
|
||||
ContentType: contentType,
|
||||
@ -379,11 +382,12 @@ func (c *Context) Data(code int, contentType string, data []byte) {
|
||||
})
|
||||
}
|
||||
|
||||
// Writes the specified file into the body stream in a efficient way.
|
||||
// File writes the specified file into the body stream in a efficient way.
|
||||
func (c *Context) File(filepath string) {
|
||||
http.ServeFile(c.Writer, c.Request, filepath)
|
||||
}
|
||||
|
||||
// SSEvent writes a Server-Sent Event into the body stream.
|
||||
func (c *Context) SSEvent(name string, message interface{}) {
|
||||
c.Render(-1, sse.Event{
|
||||
Event: name,
|
||||
|
Reference in New Issue
Block a user