forked from mirror/gin
Fix #252 typo (middlewares -> middleware)
This commit is contained in:
parent
d6425f1692
commit
7c0c427b2d
34
README.md
34
README.md
|
@ -2,14 +2,14 @@
|
|||
|
||||
[![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.svg)](https://godoc.org/github.com/gin-gonic/gin) [![Join the chat at https://gitter.im/gin-gonic/gin](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/gin-gonic/gin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||
|
||||
Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster thanks to [httprouter](https://github.com/julienschmidt/httprouter). If you need performance and good productivity, you will love Gin.
|
||||
Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster thanks to [httprouter](https://github.com/julienschmidt/httprouter). If you need performance and good productivity, you will love Gin.
|
||||
|
||||
![Gin console logger](https://gin-gonic.github.io/gin/other/console.png)
|
||||
|
||||
```
|
||||
$ cat test.go
|
||||
```
|
||||
```go
|
||||
```go
|
||||
package main
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
@ -88,8 +88,8 @@ import "github.com/gin-gonic/gin"
|
|||
|
||||
```go
|
||||
func main() {
|
||||
// Creates a gin router with default middlewares:
|
||||
// logger and recovery (crash-free) middlewares
|
||||
// Creates a gin router with default middleware:
|
||||
// logger and recovery (crash-free) middleware
|
||||
router := gin.Default()
|
||||
|
||||
router.GET("/someGet", getting)
|
||||
|
@ -110,7 +110,7 @@ func main() {
|
|||
```go
|
||||
func main() {
|
||||
router := gin.Default()
|
||||
|
||||
|
||||
// This handler will match /user/john but will not match neither /user/ or /user
|
||||
router.GET("/user/:name", func(c *gin.Context) {
|
||||
name := c.Param("name")
|
||||
|
@ -125,7 +125,7 @@ func main() {
|
|||
message := name + " is " + action
|
||||
c.String(http.StatusOK, message)
|
||||
})
|
||||
|
||||
|
||||
router.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
@ -147,7 +147,7 @@ func main() {
|
|||
}
|
||||
```
|
||||
|
||||
### Multipart/Urlencoded Form
|
||||
### Multipart/Urlencoded Form
|
||||
|
||||
```go
|
||||
func main() {
|
||||
|
@ -156,7 +156,7 @@ func main() {
|
|||
router.POST("/form_post", func(c *gin.Context) {
|
||||
message := c.PostForm("message")
|
||||
nick := c.DefaultPostForm("nick", "anonymous")
|
||||
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"status": "posted",
|
||||
"message": message,
|
||||
|
@ -192,7 +192,7 @@ func main() {
|
|||
```
|
||||
|
||||
|
||||
#### Blank Gin without middlewares by default
|
||||
#### Blank Gin without middleware by default
|
||||
|
||||
Use
|
||||
|
||||
|
@ -206,24 +206,24 @@ r := gin.Default()
|
|||
```
|
||||
|
||||
|
||||
#### Using middlewares
|
||||
#### Using middleware
|
||||
```go
|
||||
func main() {
|
||||
// Creates a router without any middleware by default
|
||||
r := gin.New()
|
||||
|
||||
// Global middlewares
|
||||
// Global middleware
|
||||
r.Use(gin.Logger())
|
||||
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)
|
||||
|
||||
// Authorization group
|
||||
// authorized := r.Group("/", AuthRequired())
|
||||
// exactly the same than:
|
||||
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.
|
||||
authorized.Use(AuthRequired())
|
||||
{
|
||||
|
@ -247,7 +247,7 @@ To bind a request body into a type, use model binding. We currently support bind
|
|||
|
||||
Note that you need to set the corresponding binding tag on all fields you want to bind. For example, when binding from JSON, set `json:"fieldname"`.
|
||||
|
||||
When using the Bind-method, Gin tries to infer the binder depending on the Content-Type header. If you are sure what you are binding, you can use BindWith.
|
||||
When using the Bind-method, Gin tries to infer the binder depending on the Content-Type header. If you are sure what you are binding, you can use BindWith.
|
||||
|
||||
You can also specify that specific fields are required. If a field is decorated with `binding:"required"` and has a empty value when binding, the current request will fail with an error.
|
||||
|
||||
|
@ -261,7 +261,7 @@ type LoginJSON struct {
|
|||
// Binding from form values
|
||||
type LoginForm struct {
|
||||
User string `form:"user" binding:"required"`
|
||||
Password string `form:"password" binding:"required"`
|
||||
Password string `form:"password" binding:"required"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
@ -297,7 +297,7 @@ func main() {
|
|||
```
|
||||
|
||||
|
||||
###Multipart/Urlencoded binding
|
||||
###Multipart/Urlencoded binding
|
||||
```go
|
||||
package main
|
||||
|
||||
|
@ -439,7 +439,7 @@ r.GET("/test", func(c *gin.Context) {
|
|||
Both internal and external locations are supported.
|
||||
|
||||
|
||||
#### Custom Middlewares
|
||||
#### Custom Middleware
|
||||
|
||||
```go
|
||||
func Logger() gin.HandlerFunc {
|
||||
|
|
|
@ -76,7 +76,7 @@ func (c *Context) Copy() *Context {
|
|||
/*********** 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.
|
||||
// See example in github.
|
||||
func (c *Context) Next() {
|
||||
|
|
8
gin.go
8
gin.go
|
@ -23,7 +23,7 @@ type (
|
|||
HandlerFunc func(*Context)
|
||||
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 {
|
||||
RouterGroup
|
||||
HTMLRender render.HTMLRender
|
||||
|
@ -139,11 +139,11 @@ func (engine *Engine) NoMethod(handlers ...HandlerFunc) {
|
|||
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...
|
||||
// For example, this is the right place for a logger or error management middleware.
|
||||
func (engine *Engine) Use(middlewares ...HandlerFunc) routesInterface {
|
||||
engine.RouterGroup.Use(middlewares...)
|
||||
func (engine *Engine) Use(middleware ...HandlerFunc) routesInterface {
|
||||
engine.RouterGroup.Use(middleware...)
|
||||
engine.rebuild404Handlers()
|
||||
engine.rebuild405Handlers()
|
||||
return engine
|
||||
|
|
|
@ -199,7 +199,7 @@ func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
|||
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
|
||||
func TestMiddlewareFailHandlersChain(t *testing.T) {
|
||||
// SETUP
|
||||
|
|
|
@ -30,7 +30,7 @@ type routesInterface interface {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
Handlers HandlersChain
|
||||
BasePath string
|
||||
|
@ -38,9 +38,9 @@ type RouterGroup struct {
|
|||
root bool
|
||||
}
|
||||
|
||||
// Adds middlewares to the group, see example code in github.
|
||||
func (group *RouterGroup) Use(middlewares ...HandlerFunc) routesInterface {
|
||||
group.Handlers = append(group.Handlers, middlewares...)
|
||||
// Adds middleware to the group, see example code in github.
|
||||
func (group *RouterGroup) Use(middleware ...HandlerFunc) routesInterface {
|
||||
group.Handlers = append(group.Handlers, middleware...)
|
||||
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.
|
||||
// The last handler should be the real handler, the other ones should be middlewares that can and should be shared among different routes.
|
||||
// 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 middleware 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
|
||||
|
|
Loading…
Reference in New Issue