forked from mirror/gin
chore: add some annotations (#1550)
ref #1075 should all annotations and can close #1075 .
This commit is contained in:
parent
d510595aa5
commit
6db092f778
|
@ -711,6 +711,7 @@ func (c *Context) Cookie(name string) (string, error) {
|
|||
return val, nil
|
||||
}
|
||||
|
||||
// Render writes the response headers and calls render.Render to render data.
|
||||
func (c *Context) Render(code int, r render.Render) {
|
||||
c.Status(code)
|
||||
|
||||
|
@ -833,6 +834,7 @@ func (c *Context) SSEvent(name string, message interface{}) {
|
|||
})
|
||||
}
|
||||
|
||||
// Stream sends a streaming response.
|
||||
func (c *Context) Stream(step func(w io.Writer) bool) {
|
||||
w := c.Writer
|
||||
clientGone := w.CloseNotify()
|
||||
|
@ -854,6 +856,7 @@ func (c *Context) Stream(step func(w io.Writer) bool) {
|
|||
/******** CONTENT NEGOTIATION *******/
|
||||
/************************************/
|
||||
|
||||
// Negotiate contains all negotiations data.
|
||||
type Negotiate struct {
|
||||
Offered []string
|
||||
HTMLName string
|
||||
|
@ -863,6 +866,7 @@ type Negotiate struct {
|
|||
Data interface{}
|
||||
}
|
||||
|
||||
// Negotiate calls different Render according acceptable Accept format.
|
||||
func (c *Context) Negotiate(code int, config Negotiate) {
|
||||
switch c.NegotiateFormat(config.Offered...) {
|
||||
case binding.MIMEJSON:
|
||||
|
@ -882,6 +886,7 @@ func (c *Context) Negotiate(code int, config Negotiate) {
|
|||
}
|
||||
}
|
||||
|
||||
// NegotiateFormat returns an acceptable Accept format.
|
||||
func (c *Context) NegotiateFormat(offered ...string) string {
|
||||
assert1(len(offered) > 0, "you must provide at least one offer")
|
||||
|
||||
|
@ -901,6 +906,7 @@ func (c *Context) NegotiateFormat(offered ...string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
// SetAccepted sets Accept header data.
|
||||
func (c *Context) SetAccepted(formats ...string) {
|
||||
c.Accepted = formats
|
||||
}
|
||||
|
|
20
errors.go
20
errors.go
|
@ -12,18 +12,24 @@ import (
|
|||
"github.com/gin-gonic/gin/internal/json"
|
||||
)
|
||||
|
||||
// ErrorType is an unsigned 64-bit error code as defined in the gin spec.
|
||||
type ErrorType uint64
|
||||
|
||||
const (
|
||||
ErrorTypeBind ErrorType = 1 << 63 // used when c.Bind() fails
|
||||
ErrorTypeRender ErrorType = 1 << 62 // used when c.Render() fails
|
||||
// ErrorTypeBind is used when Context.Bind() fails.
|
||||
ErrorTypeBind ErrorType = 1 << 63
|
||||
// ErrorTypeRender is used when Context.Render() fails.
|
||||
ErrorTypeRender ErrorType = 1 << 62
|
||||
// ErrorTypePrivate indicates a private error.
|
||||
ErrorTypePrivate ErrorType = 1 << 0
|
||||
ErrorTypePublic ErrorType = 1 << 1
|
||||
|
||||
// ErrorTypePublic indicates a public error.
|
||||
ErrorTypePublic ErrorType = 1 << 1
|
||||
// ErrorTypeAny indicates other any error.
|
||||
ErrorTypeAny ErrorType = 1<<64 - 1
|
||||
ErrorTypeNu = 2
|
||||
)
|
||||
|
||||
// Error represents a error's specification.
|
||||
type Error struct {
|
||||
Err error
|
||||
Type ErrorType
|
||||
|
@ -34,11 +40,13 @@ type errorMsgs []*Error
|
|||
|
||||
var _ error = &Error{}
|
||||
|
||||
// SetType sets the error's type.
|
||||
func (msg *Error) SetType(flags ErrorType) *Error {
|
||||
msg.Type = flags
|
||||
return msg
|
||||
}
|
||||
|
||||
// SetMeta sets the error's meta data.
|
||||
func (msg *Error) SetMeta(data interface{}) *Error {
|
||||
msg.Meta = data
|
||||
return msg
|
||||
|
@ -70,11 +78,12 @@ func (msg *Error) MarshalJSON() ([]byte, error) {
|
|||
return json.Marshal(msg.JSON())
|
||||
}
|
||||
|
||||
// Error implements the error interface
|
||||
// Error implements the error interface.
|
||||
func (msg Error) Error() string {
|
||||
return msg.Err.Error()
|
||||
}
|
||||
|
||||
// IsType judges one error.
|
||||
func (msg *Error) IsType(flags ErrorType) bool {
|
||||
return (msg.Type & flags) > 0
|
||||
}
|
||||
|
@ -138,6 +147,7 @@ func (a errorMsgs) JSON() interface{} {
|
|||
}
|
||||
}
|
||||
|
||||
// MarshalJSON implements the json.Marshaller interface.
|
||||
func (a errorMsgs) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(a.JSON())
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ func ConfigRuntime() {
|
|||
fmt.Printf("Running with %d CPUs\n", nuCPU)
|
||||
}
|
||||
|
||||
// StartWrokers start starsWorker by goroutine.
|
||||
// StartWorkers start starsWorker by goroutine.
|
||||
func StartWorkers() {
|
||||
go statsWorker()
|
||||
}
|
||||
|
|
6
gin.go
6
gin.go
|
@ -26,7 +26,10 @@ var (
|
|||
defaultAppEngine bool
|
||||
)
|
||||
|
||||
// HandlerFunc defines the handler used by gin middleware as return value.
|
||||
type HandlerFunc func(*Context)
|
||||
|
||||
// HandlersChain defines a HandlerFunc array.
|
||||
type HandlersChain []HandlerFunc
|
||||
|
||||
// Last returns the last handler in the chain. ie. the last handler is the main own.
|
||||
|
@ -37,12 +40,14 @@ func (c HandlersChain) Last() HandlerFunc {
|
|||
return nil
|
||||
}
|
||||
|
||||
// RouteInfo represents a request route's specification which contains method and path and its handler.
|
||||
type RouteInfo struct {
|
||||
Method string
|
||||
Path string
|
||||
Handler string
|
||||
}
|
||||
|
||||
// RoutesInfo defines a RouteInfo array.
|
||||
type RoutesInfo []RouteInfo
|
||||
|
||||
// Engine is the framework's instance, it contains the muxer, middleware and configuration settings.
|
||||
|
@ -155,6 +160,7 @@ func (engine *Engine) allocateContext() *Context {
|
|||
return &Context{engine: engine}
|
||||
}
|
||||
|
||||
// Delims sets template left and right delims and returns a Engine instance.
|
||||
func (engine *Engine) Delims(left, right string) *Engine {
|
||||
engine.delims = render.Delims{Left: left, Right: right}
|
||||
return engine
|
||||
|
|
13
mode.go
13
mode.go
|
@ -11,12 +11,16 @@ import (
|
|||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// ENV_GIN_MODE indicates environment name for gin mode.
|
||||
const ENV_GIN_MODE = "GIN_MODE"
|
||||
|
||||
const (
|
||||
DebugMode = "debug"
|
||||
// DebugMode indicates gin mode is debug.
|
||||
DebugMode = "debug"
|
||||
// ReleaseMode indicates gin mode is relase.
|
||||
ReleaseMode = "release"
|
||||
TestMode = "test"
|
||||
// TestMode indicates gin mode is test.
|
||||
TestMode = "test"
|
||||
)
|
||||
const (
|
||||
debugCode = iota
|
||||
|
@ -42,6 +46,7 @@ func init() {
|
|||
SetMode(mode)
|
||||
}
|
||||
|
||||
// SetMode sets gin mode according to input string.
|
||||
func SetMode(value string) {
|
||||
switch value {
|
||||
case DebugMode, "":
|
||||
|
@ -59,14 +64,18 @@ func SetMode(value string) {
|
|||
modeName = value
|
||||
}
|
||||
|
||||
// DisableBindValidation closes the default validator.
|
||||
func DisableBindValidation() {
|
||||
binding.Validator = nil
|
||||
}
|
||||
|
||||
// EnableJsonDecoderUseNumber sets true for binding.EnableDecoderUseNumberto to
|
||||
// call the UseNumber method on the JSON Decoder instance.
|
||||
func EnableJsonDecoderUseNumber() {
|
||||
binding.EnableDecoderUseNumber = true
|
||||
}
|
||||
|
||||
// Mode returns currently gin mode.
|
||||
func Mode() string {
|
||||
return modeName
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ type HTML struct {
|
|||
|
||||
var htmlContentType = []string{"text/html; charset=utf-8"}
|
||||
|
||||
// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface..
|
||||
// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.
|
||||
func (r HTMLProduction) Instance(name string, data interface{}) Render {
|
||||
return HTML{
|
||||
Template: r.Template,
|
||||
|
@ -55,7 +55,7 @@ func (r HTMLProduction) Instance(name string, data interface{}) Render {
|
|||
}
|
||||
}
|
||||
|
||||
// Instance (HTMLDebug) returns an HTML instance which it realizes Render interface..
|
||||
// Instance (HTMLDebug) returns an HTML instance which it realizes Render interface.
|
||||
func (r HTMLDebug) Instance(name string, data interface{}) Render {
|
||||
return HTML{
|
||||
Template: r.loadTemplate(),
|
||||
|
|
|
@ -11,11 +11,13 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// IRouter defines all router handle interface includes single and group router.
|
||||
type IRouter interface {
|
||||
IRoutes
|
||||
Group(string, ...HandlerFunc) *RouterGroup
|
||||
}
|
||||
|
||||
// Iroutes defins all router handle interface.
|
||||
type IRoutes interface {
|
||||
Use(...HandlerFunc) IRoutes
|
||||
|
||||
|
@ -34,8 +36,8 @@ type IRoutes interface {
|
|||
StaticFS(string, http.FileSystem) IRoutes
|
||||
}
|
||||
|
||||
// RouterGroup is used internally to configure router, a RouterGroup is associated with a prefix
|
||||
// and an array of handlers (middleware).
|
||||
// RouterGroup is used internally to configure router, a RouterGroup is associated with
|
||||
// a prefix and an array of handlers (middleware).
|
||||
type RouterGroup struct {
|
||||
Handlers HandlersChain
|
||||
basePath string
|
||||
|
@ -61,6 +63,8 @@ func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *R
|
|||
}
|
||||
}
|
||||
|
||||
// BasePath returns the base path of router group.
|
||||
// For example, if v := router.Group("/rest/n/v1/api"), v.BasePath() is "/rest/n/v1/api".
|
||||
func (group *RouterGroup) BasePath() string {
|
||||
return group.basePath
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue