Merge branch 'master' into develop
This commit is contained in:
commit
503b1fadae
39
README.md
39
README.md
@ -1,4 +1,7 @@
|
||||
#Gin Web Framework
|
||||
|
||||
[![GoDoc](https://godoc.org/github.com/gin-gonic/gin?status.png)](https://godoc.org/github.com/gin-gonic/gin)
|
||||
|
||||
Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster. If you need performance and good productivity, you will love Gin.
|
||||
[Check out the official web site](http://gin-gonic.github.io/gin/)
|
||||
|
||||
@ -23,7 +26,7 @@ import "github.com/gin-gonic/gin"
|
||||
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
r.GET("ping", func(c *gin.Context){
|
||||
r.GET("/ping", func(c *gin.Context){
|
||||
c.String(200, "pong")
|
||||
})
|
||||
|
||||
@ -61,6 +64,9 @@ func main() {
|
||||
message := "Hello "+name
|
||||
c.String(200, message)
|
||||
})
|
||||
|
||||
// Listen and server on 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
@ -167,6 +173,9 @@ func main() {
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Listen and server on 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
@ -184,17 +193,24 @@ func main() {
|
||||
r.GET("/moreJSON", func(c *gin.Context) {
|
||||
// You also can use a struct
|
||||
var msg struct {
|
||||
Name string `json:"user"`
|
||||
Message string
|
||||
Status int
|
||||
Number int
|
||||
}
|
||||
msg.Name = "Lena"
|
||||
msg.Message = "hey"
|
||||
msg.Status = 200
|
||||
c.JSON(200, msg.Status)
|
||||
msg.Number = 123
|
||||
// Note that msg.Name becomes "user" in the JSON
|
||||
// Will output : {"user": "Lena", "Message": "hey", "Number": 123}
|
||||
c.JSON(200, msg)
|
||||
})
|
||||
|
||||
r.GET("/someXML", func(c *gin.Context) {
|
||||
c.XML(200, gin.H{"message": "hey", "status": 200})
|
||||
})
|
||||
|
||||
// Listen and server on 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
@ -207,10 +223,13 @@ Using LoadHTMLTemplates()
|
||||
func main() {
|
||||
r := gin.Default()
|
||||
r.LoadHTMLTemplates("templates/*")
|
||||
r.GET("index", func(c *gin.Context) {
|
||||
r.GET("/index", func(c *gin.Context) {
|
||||
obj := gin.H{"title": "Main website"}
|
||||
c.HTML(200, "index.tmpl", obj)
|
||||
})
|
||||
|
||||
// Listen and server on 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
@ -222,6 +241,9 @@ func main() {
|
||||
r := gin.Default()
|
||||
html := template.ParseFiles("file1", "file2")
|
||||
r.HTMLTemplates = html
|
||||
|
||||
// Listen and server on 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
@ -231,7 +253,7 @@ func main() {
|
||||
```go
|
||||
func Logger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
t : time.Now()
|
||||
t := time.Now()
|
||||
|
||||
// Set example variable
|
||||
c.Set("example", "12345")
|
||||
@ -250,12 +272,15 @@ func main() {
|
||||
r := gin.New()
|
||||
r.Use(Logger())
|
||||
|
||||
r.GET("test", func(c *gin.Context){
|
||||
r.GET("/test", func(c *gin.Context){
|
||||
example := r.Get("example").(string)
|
||||
|
||||
// it would print: "12345"
|
||||
log.Println(example)
|
||||
})
|
||||
|
||||
// Listen and server on 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -51,5 +51,5 @@ func main() {
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
r.Run(":8081")
|
||||
r.Run(":8080")
|
||||
}
|
||||
|
6
gin.go
6
gin.go
@ -323,10 +323,10 @@ func (c *Context) ParseBody(item interface{}) error {
|
||||
// Serializes the given struct as a JSON into the response body in a fast and efficient way.
|
||||
// It also sets the Content-Type as "application/json"
|
||||
func (c *Context) JSON(code int, obj interface{}) {
|
||||
c.Writer.Header().Set("Content-Type", "application/json")
|
||||
if code >= 0 {
|
||||
c.Writer.WriteHeader(code)
|
||||
}
|
||||
c.Writer.Header().Set("Content-Type", "application/json")
|
||||
encoder := json.NewEncoder(c.Writer)
|
||||
if err := encoder.Encode(obj); err != nil {
|
||||
c.Error(err, obj)
|
||||
@ -337,10 +337,10 @@ func (c *Context) JSON(code int, obj interface{}) {
|
||||
// Serializes the given struct as a XML into the response body in a fast and efficient way.
|
||||
// It also sets the Content-Type as "application/xml"
|
||||
func (c *Context) XML(code int, obj interface{}) {
|
||||
c.Writer.Header().Set("Content-Type", "application/xml")
|
||||
if code >= 0 {
|
||||
c.Writer.WriteHeader(code)
|
||||
}
|
||||
c.Writer.Header().Set("Content-Type", "application/xml")
|
||||
encoder := xml.NewEncoder(c.Writer)
|
||||
if err := encoder.Encode(obj); err != nil {
|
||||
c.Error(err, obj)
|
||||
@ -352,10 +352,10 @@ func (c *Context) XML(code int, obj interface{}) {
|
||||
// It also update the HTTP code and sets the Content-Type as "text/html".
|
||||
// See http://golang.org/doc/articles/wiki/
|
||||
func (c *Context) HTML(code int, name string, data interface{}) {
|
||||
c.Writer.Header().Set("Content-Type", "text/html")
|
||||
if code >= 0 {
|
||||
c.Writer.WriteHeader(code)
|
||||
}
|
||||
c.Writer.Header().Set("Content-Type", "text/html")
|
||||
if err := c.engine.HTMLTemplates.ExecuteTemplate(c.Writer, name, data); err != nil {
|
||||
c.Error(err, map[string]interface{}{
|
||||
"name": name,
|
||||
|
Loading…
Reference in New Issue
Block a user