mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 20:22:20 +08:00
Merge abe076b8f8961378daecca4d8309929302f3c37a into 5eb0e10a78a29c69678d1e6bfca6b1ca8016be95
This commit is contained in:
commit
3bb74a4c66
14
README.md
14
README.md
@ -275,14 +275,14 @@ func main() {
|
|||||||
func Logger() gin.HandlerFunc {
|
func Logger() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
|
|
||||||
// Set example variable
|
// Set example variable
|
||||||
c.Set("example", "12345")
|
c.Set("example", "12345")
|
||||||
|
|
||||||
// before request
|
// before request
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
// after request
|
// after request
|
||||||
latency := time.Since(t)
|
latency := time.Since(t)
|
||||||
log.Print(latency)
|
log.Print(latency)
|
||||||
@ -292,10 +292,10 @@ func Logger() gin.HandlerFunc {
|
|||||||
func main() {
|
func main() {
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
r.Use(Logger())
|
r.Use(Logger())
|
||||||
|
|
||||||
r.GET("/test", func(c *gin.Context){
|
r.GET("/test", func(c *gin.Context){
|
||||||
example := r.Get("example").(string)
|
example := c.MustGet("example").(string)
|
||||||
|
|
||||||
// it would print: "12345"
|
// it would print: "12345"
|
||||||
log.Println(example)
|
log.Println(example)
|
||||||
})
|
})
|
||||||
|
@ -38,14 +38,14 @@ func main() {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
authorized.POST("admin", func(c *gin.Context) {
|
authorized.POST("admin", func(c *gin.Context) {
|
||||||
user := c.Get("user").(string)
|
user, _ := c.Get("user")
|
||||||
|
|
||||||
// Parse JSON
|
// Parse JSON
|
||||||
var json struct {
|
var json struct {
|
||||||
Value string `json:"value" binding:"required"`
|
Value string `json:"value" binding:"required"`
|
||||||
}
|
}
|
||||||
if c.EnsureBody(&json) {
|
if c.EnsureBody(&json) {
|
||||||
DB[user] = json.Value
|
DB[user.(string)] = json.Value
|
||||||
c.JSON(200, gin.H{"status": "ok"})
|
c.JSON(200, gin.H{"status": "ok"})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
24
gin.go
24
gin.go
@ -281,20 +281,24 @@ func (c *Context) Set(key string, item interface{}) {
|
|||||||
c.Keys[key] = item
|
c.Keys[key] = item
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the value for the given key.
|
// Get returns the value for the given key or an error if the key does not exist.
|
||||||
// It panics if the value doesn't exist.
|
func (c *Context) Get(key string) (interface{}, error) {
|
||||||
func (c *Context) Get(key string) interface{} {
|
|
||||||
var ok bool
|
|
||||||
var item interface{}
|
|
||||||
if c.Keys != nil {
|
if c.Keys != nil {
|
||||||
item, ok = c.Keys[key]
|
item, ok := c.Keys[key]
|
||||||
} else {
|
if ok {
|
||||||
item, ok = nil, false
|
return item, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if !ok || item == nil {
|
return nil, errors.New("Key does not exist.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustGet returns the value for the given key or panics if the value doesn't exist.
|
||||||
|
func (c *Context) MustGet(key string) interface{} {
|
||||||
|
value, err := c.Get(key)
|
||||||
|
if err != nil || value == nil {
|
||||||
log.Panicf("Key %s doesn't exist", key)
|
log.Panicf("Key %s doesn't exist", key)
|
||||||
}
|
}
|
||||||
return item
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************/
|
/************************************/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user