Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context keys get set #702

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648
(3): Heap Memory (B/op)
(4): Average Allocations per Repetition (allocs/op)

##Gin v1. stable
## Gin v1. stable

- [x] Zero allocation router.
- [x] Still the fastest http router and framework. From routing to writing.
Expand All @@ -84,6 +84,7 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648


## Start using it

1. Download and install it:

```sh
Expand All @@ -102,7 +103,7 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648
import "net/http"
```

##API Examples
## API Examples

#### Using GET, POST, PUT, PATCH, DELETE and OPTIONS

Expand Down Expand Up @@ -412,7 +413,7 @@ $ curl -v --form user=user --form password=password http://localhost:8080/login
```


#### XML and JSON rendering
#### XML, JSON and YAML rendering

```go
func main() {
Expand Down Expand Up @@ -442,6 +443,10 @@ func main() {
c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
})

r.GET("/someYAML", func(c *gin.Context) {
c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
})

// Listen and server on 0.0.0.0:8080
r.Run(":8080")
}
Expand Down Expand Up @@ -708,3 +713,10 @@ endless.ListenAndServe(":4242", router)
An alternative to endless:

* [manners](https://github.com/braintree/manners): A polite Go HTTP server that shuts down gracefully.

## Example

Awesome project lists using [Gin](https://github.com/gin-gonic/gin) web framework.

* [drone](https://github.com/drone/drone): Drone is a Continuous Delivery platform built on Docker, written in Go
* [gorush](https://github.com/appleboy/gorush): A push notification server written in Go.
14 changes: 10 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"

"github.com/gin-gonic/gin/binding"
Expand Down Expand Up @@ -44,10 +45,11 @@ type Context struct {
handlers HandlersChain
index int8

engine *Engine
Keys map[string]interface{}
Errors errorMsgs
Accepted []string
engine *Engine
Keys map[string]interface{}
KeysLocker sync.RWMutex
Errors errorMsgs
Accepted []string
}

var _ context.Context = &Context{}
Expand Down Expand Up @@ -157,17 +159,21 @@ func (c *Context) Error(err error) *Error {
// Set is used to store a new key/value pair exclusivelly for this context.
// It also lazy initializes c.Keys if it was not used previously.
func (c *Context) Set(key string, value interface{}) {
c.KeysLocker.Lock()
if c.Keys == nil {
c.Keys = make(map[string]interface{})
}
c.Keys[key] = value
c.KeysLocker.Unlock()
}

// Get returns the value for the given key, ie: (value, true).
// If the value does not exists it returns (nil, false)
func (c *Context) Get(key string) (value interface{}, exists bool) {
if c.Keys != nil {
c.KeysLocker.RLock()
value, exists = c.Keys[key]
c.KeysLocker.RUnlock()
}
return
}
Expand Down
2 changes: 1 addition & 1 deletion ginS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func main() {
ginS.GET("/", func(c *gin.Context) { c.String("Hello World") })
ginS.GET("/", func(c *gin.Context) { c.String(200, "Hello World") })
ginS.Run()
}
```
5 changes: 3 additions & 2 deletions mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gin

import (
"io"
"os"

"github.com/gin-gonic/gin/binding"
Expand All @@ -30,8 +31,8 @@ const (
// To support coloring in Windows use:
// import "github.com/mattn/go-colorable"
// gin.DefaultWriter = colorable.NewColorableStdout()
var DefaultWriter = os.Stdout
var DefaultErrorWriter = os.Stderr
var DefaultWriter io.Writer = os.Stdout
var DefaultErrorWriter io.Writer = os.Stderr

var ginMode = debugCode
var modeName = DebugMode
Expand Down