Skip to content
Myr

Merge pull request #362 from javierprovecho/fix-middlewares-typo

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix #252 typo (middlewares -> middleware)

authored by Manu Mtz.-Almeida · committed by Manu Mtz.-Almeida · parents d6425f1, 7c0c427 · Browse files · 5 files+29 added−29 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+29 added−29 removed

  • other:+29 added−29 removed

5 files+29 added−29 removed

modified:context.go+1 added-1 removed
74 unmodified lines
75/************************************/76/*********** FLOW CONTROL ***********/77/************************************/7879// Next should be used only in the middlewares.79// Next should be used only inside middleware.80// It executes the pending handlers in the chain inside the calling handler.81// See example in github.82func (c *Context) Next() {83 c.index++
390 unmodified lines
modified:gin.go+4 added-4 removed
21 unmodified lines
22type (23 HandlerFunc func(*Context)24 HandlersChain []HandlerFunc2526 // Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middlewares.26 // Represents the web framework, it wraps the blazing fast httprouter multiplexer and a list of global middleware.27 Engine struct {28 RouterGroup29 HTMLRender render.HTMLRender30 allNoRoute HandlersChain
107 unmodified lines
138 engine.noMethod = handlers139 engine.rebuild405Handlers()140}141142// Attachs a global middleware to the router. ie. the middlewares attached though Use() will be142// Attachs a global middleware to the router. ie. the middleware attached though Use() will be143// included in the handlers chain for every single request. Even 404, 405, static files...144// For example, this is the right place for a logger or error management middleware.145func (engine *Engine) Use(middlewares ...HandlerFunc) routesInterface {146 engine.RouterGroup.Use(middlewares...)145func (engine *Engine) Use(middleware ...HandlerFunc) routesInterface {146 engine.RouterGroup.Use(middleware...)147 engine.rebuild404Handlers()148 engine.rebuild405Handlers()149 return engine150}
185 unmodified lines
modified:middleware_test.go+1 added-1 removed
197 unmodified lines
198 assert.Equal(t, w.Code, 410)199 assert.Equal(t, signature, "ACB")200}201202// TestFailHandlersChain - ensure that Fail interrupt used middlewares in fifo order as202// TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as203// as well as Abort204func TestMiddlewareFailHandlersChain(t *testing.T) {205 // SETUP206 signature := ""
49 unmodified lines
modified:README.md+17 added-17 removed
1#Gin Web Framework [![Build Status](https://travis-ci.org/gin-gonic/gin.svg)](https://travis-ci.org/gin-gonic/gin) [![Coverage Status](https://coveralls.io/repos/gin-gonic/gin/badge.svg?branch=master)](https://coveralls.io/r/gin-gonic/gin?branch=master) 23 [![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)45Gin 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. 5Gin 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.67![Gin console logger](https://gin-gonic.github.io/gin/other/console.png)89```10$ cat test.go11```12```go 12```go13package main1415import "github.com/gin-gonic/gin"16
70 unmodified lines
87#### Using GET, POST, PUT, PATCH, DELETE and OPTIONS8889```go90func main() {91 // Creates a gin router with default middlewares:92 // logger and recovery (crash-free) middlewares91 // Creates a gin router with default middleware:92 // logger and recovery (crash-free) middleware93 router := gin.Default()9495 router.GET("/someGet", getting)96 router.POST("/somePost", posting)
12 unmodified lines
109110```go111func main() {112 router := gin.Default()113 113114 // This handler will match /user/john but will not match neither /user/ or /user115 router.GET("/user/:name", func(c *gin.Context) {116 name := c.Param("name")117 c.String(http.StatusOK, "Hello %s", name)
6 unmodified lines
124 action := c.Param("action")125 message := name + " is " + action126 c.String(http.StatusOK, message)127 })128 128129 router.Run(":8080")130}131```132
13 unmodified lines
146 router.Run(":8080")147}148```149150### Multipart/Urlencoded Form 150### Multipart/Urlencoded Form151152```go153func main() {154 router := gin.Default()155156 router.POST("/form_post", func(c *gin.Context) {157 message := c.PostForm("message")158 nick := c.DefaultPostForm("nick", "anonymous")159 159160 c.JSON(200, gin.H{161 "status": "posted",162 "message": message,163 })
27 unmodified lines
191}192```193194195#### Blank Gin without middlewares by default195#### Blank Gin without middleware by default196197Use198199```go
5 unmodified lines
205r := gin.Default()206```207208209#### Using middlewares209#### Using middleware210```go211func main() {212 // Creates a router without any middleware by default213 r := gin.New()214215 // Global middlewares215 // Global middleware216 r.Use(gin.Logger())217 r.Use(gin.Recovery())218219 // Per route middlewares, you can add as many as you desire.219 // Per route middleware, you can add as many as you desire.220 r.GET("/benchmark", MyBenchLogger(), benchEndpoint)221222 // Authorization group223 // authorized := r.Group("/", AuthRequired())224 // exactly the same than:225 authorized := r.Group("/")226 // per group middlewares! in this case we use the custom created226 // per group middleware! in this case we use the custom created227 // AuthRequired() middleware just in the "authorized" group.228 authorized.Use(AuthRequired())229 {230 authorized.POST("/login", loginEndpoint)
15 unmodified lines
246To bind a request body into a type, use model binding. We currently support binding of JSON, XML and standard form values (foo=bar&boo=baz).247248Note 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"`.249250When 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. 250When 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.251252You 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.253254```go
5 unmodified lines
260261// Binding from form values262type LoginForm struct {263 User string `form:"user" binding:"required"`264 Password string `form:"password" binding:"required"` 264 Password string `form:"password" binding:"required"`265}266267func main() {268 r := gin.Default()
27 unmodified lines
296}297```298299300###Multipart/Urlencoded binding 300###Multipart/Urlencoded binding301```go302package main303304import (
133 unmodified lines
438```439Both internal and external locations are supported.440441442#### Custom Middlewares442#### Custom Middleware443444```go445func Logger() gin.HandlerFunc {446 return func(c *gin.Context) {
133 unmodified lines
modified:routergroup.go+6 added-6 removed
28 unmodified lines
29 StaticFS(string, http.FileSystem) routesInterface30}3132// Used internally to configure router, a RouterGroup is associated with a prefix33// and an array of handlers (middlewares)33// and an array of handlers (middleware)34type RouterGroup struct {35 Handlers HandlersChain36 BasePath string37 engine *Engine38 root bool39}4041// Adds middlewares to the group, see example code in github.42func (group *RouterGroup) Use(middlewares ...HandlerFunc) routesInterface {43 group.Handlers = append(group.Handlers, middlewares...)41// Adds middleware to the group, see example code in github.42func (group *RouterGroup) Use(middleware ...HandlerFunc) routesInterface {43 group.Handlers = append(group.Handlers, middleware...)44 return group.returnObj()45}4647// Creates a new router group. You should add all the routes that have common middlwares or the same path prefix.
5 unmodified lines
53 engine: group.engine,54 }55}5657// Handle registers a new request handle and middlewares with the given path and method.58// The last handler should be the real handler, the other ones should be middlewares that can and should be shared among different routes.57// Handle registers a new request handle and middleware with the given path and method.58// The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes.59// See the example code in github.60//61// For GET, POST, PUT, PATCH and DELETE requests the respective shortcut62// functions can be used.
136 unmodified lines