forked from mirror/gin
Fix #252 typo (middlewares -> middleware)
This commit is contained in:
parent
d6425f1692
commit
7c0c427b2d
16
README.md
16
README.md
|
@ -88,8 +88,8 @@ import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
// Creates a gin router with default middlewares:
|
// Creates a gin router with default middleware:
|
||||||
// logger and recovery (crash-free) middlewares
|
// logger and recovery (crash-free) middleware
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
router.GET("/someGet", getting)
|
router.GET("/someGet", getting)
|
||||||
|
@ -192,7 +192,7 @@ func main() {
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
#### Blank Gin without middlewares by default
|
#### Blank Gin without middleware by default
|
||||||
|
|
||||||
Use
|
Use
|
||||||
|
|
||||||
|
@ -206,24 +206,24 @@ r := gin.Default()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
#### Using middlewares
|
#### Using middleware
|
||||||
```go
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
// Creates a router without any middleware by default
|
// Creates a router without any middleware by default
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
|
|
||||||
// Global middlewares
|
// Global middleware
|
||||||
r.Use(gin.Logger())
|
r.Use(gin.Logger())
|
||||||
r.Use(gin.Recovery())
|
r.Use(gin.Recovery())
|
||||||
|
|
||||||
// Per route middlewares, you can add as many as you desire.
|
// Per route middleware, you can add as many as you desire.
|
||||||
r.GET("/benchmark", MyBenchLogger(), benchEndpoint)
|
r.GET("/benchmark", MyBenchLogger(), benchEndpoint)
|
||||||
|
|
||||||
// Authorization group
|
// Authorization group
|
||||||
// authorized := r.Group("/", AuthRequired())
|
// authorized := r.Group("/", AuthRequired())
|
||||||
// exactly the same than:
|
// exactly the same than:
|
||||||
authorized := r.Group("/")
|
authorized := r.Group("/")
|
||||||
// per group middlewares! in this case we use the custom created
|
// per group middleware! in this case we use the custom created
|
||||||
// AuthRequired() middleware just in the "authorized" group.
|
// AuthRequired() middleware just in the "authorized" group.
|
||||||
authorized.Use(AuthRequired())
|
authorized.Use(AuthRequired())
|
||||||
{
|
{
|
||||||
|
@ -439,7 +439,7 @@ r.GET("/test", func(c *gin.Context) {
|
||||||
Both internal and external locations are supported.
|
Both internal and external locations are supported.
|
||||||
|
|
||||||
|
|
||||||
#### Custom Middlewares
|
#### Custom Middleware
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Logger() gin.HandlerFunc {
|
func Logger() gin.HandlerFunc {
|
||||||
|
|
|
@ -76,7 +76,7 @@ func (c *Context) Copy() *Context {
|
||||||
/*********** FLOW CONTROL ***********/
|
/*********** FLOW CONTROL ***********/
|
||||||
/************************************/
|
/************************************/
|
||||||
|
|
||||||
// Next should be used only in the middlewares.
|
// Next should be used only inside middleware.
|
||||||
// It executes the pending handlers in the chain inside the calling handler.
|
// It executes the pending handlers in the chain inside the calling handler.
|
||||||
// See example in github.
|
// See example in github.
|
||||||
func (c *Context) Next() {
|
func (c *Context) Next() {
|
||||||
|
|
8
gin.go
8
gin.go
|
@ -23,7 +23,7 @@ type (
|
||||||
HandlerFunc func(*Context)
|
HandlerFunc func(*Context)
|
||||||
HandlersChain []HandlerFunc
|
HandlersChain []HandlerFunc
|
||||||
|
|
||||||
// Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares.
|
// Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middleware.
|
||||||
Engine struct {
|
Engine struct {
|
||||||
RouterGroup
|
RouterGroup
|
||||||
HTMLRender render.HTMLRender
|
HTMLRender render.HTMLRender
|
||||||
|
@ -139,11 +139,11 @@ func (engine *Engine) NoMethod(handlers ...HandlerFunc) {
|
||||||
engine.rebuild405Handlers()
|
engine.rebuild405Handlers()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attachs a global middleware to the router. ie. the middlewares attached though Use() will be
|
// Attachs a global middleware to the router. ie. the middleware attached though Use() will be
|
||||||
// included in the handlers chain for every single request. Even 404, 405, static files...
|
// included in the handlers chain for every single request. Even 404, 405, static files...
|
||||||
// For example, this is the right place for a logger or error management middleware.
|
// For example, this is the right place for a logger or error management middleware.
|
||||||
func (engine *Engine) Use(middlewares ...HandlerFunc) routesInterface {
|
func (engine *Engine) Use(middleware ...HandlerFunc) routesInterface {
|
||||||
engine.RouterGroup.Use(middlewares...)
|
engine.RouterGroup.Use(middleware...)
|
||||||
engine.rebuild404Handlers()
|
engine.rebuild404Handlers()
|
||||||
engine.rebuild405Handlers()
|
engine.rebuild405Handlers()
|
||||||
return engine
|
return engine
|
||||||
|
|
|
@ -199,7 +199,7 @@ func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
||||||
assert.Equal(t, signature, "ACB")
|
assert.Equal(t, signature, "ACB")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as
|
// TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
|
||||||
// as well as Abort
|
// as well as Abort
|
||||||
func TestMiddlewareFailHandlersChain(t *testing.T) {
|
func TestMiddlewareFailHandlersChain(t *testing.T) {
|
||||||
// SETUP
|
// SETUP
|
||||||
|
|
|
@ -30,7 +30,7 @@ type routesInterface interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Used internally to configure router, a RouterGroup is associated with a prefix
|
// Used internally to configure router, a RouterGroup is associated with a prefix
|
||||||
// and an array of handlers (middlewares)
|
// and an array of handlers (middleware)
|
||||||
type RouterGroup struct {
|
type RouterGroup struct {
|
||||||
Handlers HandlersChain
|
Handlers HandlersChain
|
||||||
BasePath string
|
BasePath string
|
||||||
|
@ -38,9 +38,9 @@ type RouterGroup struct {
|
||||||
root bool
|
root bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds middlewares to the group, see example code in github.
|
// Adds middleware to the group, see example code in github.
|
||||||
func (group *RouterGroup) Use(middlewares ...HandlerFunc) routesInterface {
|
func (group *RouterGroup) Use(middleware ...HandlerFunc) routesInterface {
|
||||||
group.Handlers = append(group.Handlers, middlewares...)
|
group.Handlers = append(group.Handlers, middleware...)
|
||||||
return group.returnObj()
|
return group.returnObj()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,8 +54,8 @@ func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *R
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle registers a new request handle and middlewares with the given path and method.
|
// Handle registers a new request handle and middleware 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.
|
// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.
|
||||||
// See the example code in github.
|
// See the example code in github.
|
||||||
//
|
//
|
||||||
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
|
// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut
|
||||||
|
|
Loading…
Reference in New Issue