From f90cc43045dc1845a17e17b6b898207717a11bb3 Mon Sep 17 00:00:00 2001 From: Javier Provecho Fernandez Date: Wed, 12 Oct 2016 16:42:56 +0200 Subject: [PATCH 1/5] Add contribution guide --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 8023dc5..c4cbfbb 100644 --- a/README.md +++ b/README.md @@ -714,6 +714,19 @@ An alternative to endless: * [manners](https://github.com/braintree/manners): A polite Go HTTP server that shuts down gracefully. +## Contributing + +- With issues: + - Use the search tool before opening a new issue. + - Please provide source code and commit sha if you found a bug. + - Review existing issues and provide feedback or react to them. +- With pull requests: + - Open your pull request against develop + - Your pull request should have no more than two commits, if not you should squash them. + - It should pass all tests in the available continuous integrations systems such as TravisCI. + - You should add/modify tests to cover your proposed code changes. + - If your pull request contains a new feature, please document it on the README. + ## Example Awesome project lists using [Gin](https://github.com/gin-gonic/gin) web framework. From 3900df04d2a88e22beaf6a2970c63648b9e1b0e1 Mon Sep 17 00:00:00 2001 From: Javier Provecho Fernandez Date: Wed, 12 Oct 2016 16:51:06 +0200 Subject: [PATCH 2/5] Update go get for stable version In the future, github default branch will be develop so running `go get github.com/gin-gonic/gin` will pull latest code from develop. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c4cbfbb..75e7417 100644 --- a/README.md +++ b/README.md @@ -88,13 +88,13 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648 1. Download and install it: ```sh - $ go get github.com/gin-gonic/gin + $ go get gopkg.in/gin-gonic/gin.v1 ``` 2. Import it in your code: ```go - import "github.com/gin-gonic/gin" + import "gopkg.in/gin-gonic/gin.v1" ``` 3. (Optional) Import `net/http`. This is required for example if using constants such as `http.StatusOK`. From e396f2f357fb52126d9c20833b7fe54c1a4af309 Mon Sep 17 00:00:00 2001 From: Pablo Moncada Date: Tue, 8 Nov 2016 18:25:23 +0100 Subject: [PATCH 3/5] Added go 1.7 version to travis.yml --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 2f78c8a..cfa47af 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ go: - 1.4 - 1.5 - 1.6 + - 1.7 - tip script: From 32cab500ecc71d2975f5699c8a65c6debb29cfbe Mon Sep 17 00:00:00 2001 From: Javier Provecho Fernandez Date: Sat, 3 Dec 2016 10:10:40 +0100 Subject: [PATCH 4/5] v1.1 (#751) * Implement QueryArray and PostArray methods * Refactor GetQuery and GetPostForm * Removed additional Iota I think assigning iota to each constant is not required * Add 1.7 test. * Add codecov.io * corrected a typo in README * remove coveralls services. Signed-off-by: Bo-Yi Wu * Update TravisCI to Gitter webhook * Add codecov.yml Gitter webhook * Changed imports to gopkg instead of github in README (#733) * Add contribution guide * Update go get for stable version In the future, github default branch will be develop so running `go get github.com/gin-gonic/gin` will pull latest code from develop. * Changed imports to gopkg instead of github in README * Update README.md * Logger: skip ANSI color commands if output is not a tty --- .travis.yml | 6 ++---- README.md | 26 ++++++++++++-------------- codecov.yml | 5 +++++ context.go | 45 +++++++++++++++++++++++++++++++++++++++------ context_test.go | 32 ++++++++++++++++++++++++++++++++ logger.go | 15 +++++++++++++-- mode.go | 4 ++-- 7 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 codecov.yml diff --git a/.travis.yml b/.travis.yml index cfa47af..3305174 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,17 +8,15 @@ go: - tip script: - - go get golang.org/x/tools/cmd/cover - - go get github.com/mattn/goveralls - go test -v -covermode=count -coverprofile=coverage.out after_success: - - goveralls -coverprofile=coverage.out -service=travis-ci -repotoken yFj7FrCeddvBzUaaCyG33jCLfWXeb93eA + - bash <(curl -s https://codecov.io/bash) notifications: webhooks: urls: - - https://webhooks.gitter.im/e/acc2c57482e94b44f557 + - https://webhooks.gitter.im/e/7f95bf605c4d356372f4 on_success: change # options: [always|never|change] default: always on_failure: always # options: [always|never|change] default: always on_start: false # default: false diff --git a/README.md b/README.md index 75e7417..82ea6a5 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,15 @@ #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) +[![codecov](https://codecov.io/gh/gin-gonic/gin/branch/master/graph/badge.svg)](https://codecov.io/gh/gin-gonic/gin) [![Go Report Card](https://goreportcard.com/badge/github.com/gin-gonic/gin)](https://goreportcard.com/report/github.com/gin-gonic/gin) [![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 Go (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) ```sh @@ -19,7 +18,7 @@ $ cat test.go ```go package main -import "github.com/gin-gonic/gin" +import "gopkg.in/gin-gonic/gin.v1" func main() { r := gin.Default() @@ -28,7 +27,7 @@ func main() { "message": "pong", }) }) - r.Run() // listen and server on 0.0.0.0:8080 + r.Run() // listen and serve on 0.0.0.0:8080 } ``` @@ -317,7 +316,7 @@ func main() { testing.GET("/analytics", analyticsEndpoint) } - // Listen and server on 0.0.0.0:8080 + // Listen and serve on 0.0.0.0:8080 r.Run(":8080") } ``` @@ -367,7 +366,7 @@ func main() { } }) - // Listen and server on 0.0.0.0:8080 + // Listen and serve on 0.0.0.0:8080 router.Run(":8080") } ``` @@ -378,8 +377,7 @@ func main() { package main import ( - "github.com/gin-gonic/gin" - "github.com/gin-gonic/gin/binding" + "gopkg.in/gin-gonic/gin.v1" ) type LoginForm struct { @@ -447,7 +445,7 @@ func main() { c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK}) }) - // Listen and server on 0.0.0.0:8080 + // Listen and serve on 0.0.0.0:8080 r.Run(":8080") } ``` @@ -461,7 +459,7 @@ func main() { router.StaticFS("/more_static", http.Dir("my_file_system")) router.StaticFile("/favicon.ico", "./resources/favicon.ico") - // Listen and server on 0.0.0.0:8080 + // Listen and serve on 0.0.0.0:8080 router.Run(":8080") } ``` @@ -593,7 +591,7 @@ func main() { log.Println(example) }) - // Listen and server on 0.0.0.0:8080 + // Listen and serve on 0.0.0.0:8080 r.Run(":8080") } ``` @@ -631,7 +629,7 @@ func main() { } }) - // Listen and server on 0.0.0.0:8080 + // Listen and serve on 0.0.0.0:8080 r.Run(":8080") } ``` @@ -664,7 +662,7 @@ func main() { log.Println("Done! in path " + c.Request.URL.Path) }) - // Listen and server on 0.0.0.0:8080 + // Listen and serve on 0.0.0.0:8080 r.Run(":8080") } ``` diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..c9c9a52 --- /dev/null +++ b/codecov.yml @@ -0,0 +1,5 @@ +coverage: + notify: + gitter: + default: + url: https://webhooks.gitter.im/e/d90dcdeeab2f1e357165 diff --git a/context.go b/context.go index 5d3b6a4..df001a4 100644 --- a/context.go +++ b/context.go @@ -230,13 +230,29 @@ func (c *Context) DefaultQuery(key, defaultValue string) string { // ("", false) == c.GetQuery("id") // ("", true) == c.GetQuery("lastname") func (c *Context) GetQuery(key string) (string, bool) { - req := c.Request - if values, ok := req.URL.Query()[key]; ok && len(values) > 0 { - return values[0], true + if values, ok := c.GetQueryArray(key); ok { + return values[0], ok } return "", false } +// QueryArray returns a slice of strings for a given query key. +// The length of the slice depends on the number of params with the given key. +func (c *Context) QueryArray(key string) []string { + values, _ := c.GetQueryArray(key) + return values +} + +// GetQueryArray returns a slice of strings for a given query key, plus +// a boolean value whether at least one value exists for the given key. +func (c *Context) GetQueryArray(key string) ([]string, bool) { + req := c.Request + if values, ok := req.URL.Query()[key]; ok && len(values) > 0 { + return values, true + } + return []string{}, false +} + // PostForm returns the specified key from a POST urlencoded form or multipart form // when it exists, otherwise it returns an empty string `("")`. func (c *Context) PostForm(key string) string { @@ -262,17 +278,34 @@ func (c *Context) DefaultPostForm(key, defaultValue string) string { // email= --> ("", true) := GetPostForm("email") // set email to "" // --> ("", false) := GetPostForm("email") // do nothing with email func (c *Context) GetPostForm(key string) (string, bool) { + if values, ok := c.GetPostFormArray(key); ok { + return values[0], ok + } + return "", false +} + +// PostFormArray returns a slice of strings for a given form key. +// The length of the slice depends on the number of params with the given key. +func (c *Context) PostFormArray(key string) []string { + values, _ := c.GetPostFormArray(key) + return values +} + +// GetPostFormArray returns a slice of strings for a given form key, plus +// a boolean value whether at least one value exists for the given key. +func (c *Context) GetPostFormArray(key string) ([]string, bool) { req := c.Request + req.ParseForm() req.ParseMultipartForm(32 << 20) // 32 MB if values := req.PostForm[key]; len(values) > 0 { - return values[0], true + return values, true } if req.MultipartForm != nil && req.MultipartForm.File != nil { if values := req.MultipartForm.Value[key]; len(values) > 0 { - return values[0], true + return values, true } } - return "", false + return []string{}, false } // Bind checks the Content-Type to select a binding engine automatically, diff --git a/context_test.go b/context_test.go index 97d4957..01ee6b8 100644 --- a/context_test.go +++ b/context_test.go @@ -251,6 +251,22 @@ func TestContextQueryAndPostForm(t *testing.T) { assert.Equal(t, obj.Page, 11) assert.Equal(t, obj.Both, "") assert.Equal(t, obj.Array, []string{"first", "second"}) + + values, ok := c.GetQueryArray("array[]") + assert.True(t, ok) + assert.Equal(t, "first", values[0]) + assert.Equal(t, "second", values[1]) + + values = c.QueryArray("array[]") + assert.Equal(t, "first", values[0]) + assert.Equal(t, "second", values[1]) + + values = c.QueryArray("nokey") + assert.Equal(t, 0, len(values)) + + values = c.QueryArray("both") + assert.Equal(t, 1, len(values)) + assert.Equal(t, "GET", values[0]) } func TestContextPostFormMultipart(t *testing.T) { @@ -299,6 +315,22 @@ func TestContextPostFormMultipart(t *testing.T) { assert.False(t, ok) assert.Empty(t, value) assert.Equal(t, c.DefaultPostForm("nokey", "nothing"), "nothing") + + values, ok := c.GetPostFormArray("array") + assert.True(t, ok) + assert.Equal(t, "first", values[0]) + assert.Equal(t, "second", values[1]) + + values = c.PostFormArray("array") + assert.Equal(t, "first", values[0]) + assert.Equal(t, "second", values[1]) + + values = c.PostFormArray("nokey") + assert.Equal(t, 0, len(values)) + + values = c.PostFormArray("foo") + assert.Equal(t, 1, len(values)) + assert.Equal(t, "bar", values[0]) } func TestContextSetCookie(t *testing.T) { diff --git a/logger.go b/logger.go index d56bc62..b4743a7 100644 --- a/logger.go +++ b/logger.go @@ -7,7 +7,10 @@ package gin import ( "fmt" "io" + "os" "time" + + "golang.org/x/crypto/ssh/terminal" ) var ( @@ -44,6 +47,11 @@ func Logger() HandlerFunc { // LoggerWithWriter instance a Logger middleware with the specified writter buffer. // Example: os.Stdout, a file opened in write mode, a socket... func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc { + isTerm := true + if outFile, ok := out.(*os.File); ok { + isTerm = terminal.IsTerminal(int(outFile.Fd())) + } + var skip map[string]struct{} if length := len(notlogged); length > 0 { @@ -71,8 +79,11 @@ func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc { clientIP := c.ClientIP() method := c.Request.Method statusCode := c.Writer.Status() - statusColor := colorForStatus(statusCode) - methodColor := colorForMethod(method) + var statusColor, methodColor string + if isTerm { + statusColor = colorForStatus(statusCode) + methodColor = colorForMethod(method) + } comment := c.Errors.ByType(ErrorTypePrivate).String() fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %s |%s %s %-7s %s\n%s", diff --git a/mode.go b/mode.go index f44b071..c600b7b 100644 --- a/mode.go +++ b/mode.go @@ -20,8 +20,8 @@ const ( ) const ( debugCode = iota - releaseCode = iota - testCode = iota + releaseCode + testCode ) // DefaultWriter is the default io.Writer used the Gin for debug output and From b26d956e07852e31402c41018d5d4dd9807a9f0c Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sun, 4 Dec 2016 00:30:59 +0800 Subject: [PATCH 5/5] fix #752 ignore appengine os. Signed-off-by: Bo-Yi Wu --- logger.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/logger.go b/logger.go index b4743a7..ad442d0 100644 --- a/logger.go +++ b/logger.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "os" + "runtime" "time" "golang.org/x/crypto/ssh/terminal" @@ -48,8 +49,11 @@ func Logger() HandlerFunc { // Example: os.Stdout, a file opened in write mode, a socket... func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc { isTerm := true - if outFile, ok := out.(*os.File); ok { - isTerm = terminal.IsTerminal(int(outFile.Fd())) + + if runtime.GOOS != "appengine" && runtime.GOOS != "netbsd" && runtime.GOOS != "openbsd" { + if outFile, ok := out.(*os.File); ok { + isTerm = terminal.IsTerminal(int(outFile.Fd())) + } } var skip map[string]struct{}