mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 05:16:35 +08:00
Merge 13565e1bf53792becc94fd4f0e775f5b2b0a639e into 3d002e382355cafc15d706b92899b1961d5b79e9
This commit is contained in:
commit
0ecfa89da2
@ -2,7 +2,8 @@ language: go
|
|||||||
sudo: false
|
sudo: false
|
||||||
go:
|
go:
|
||||||
- 1.4
|
- 1.4
|
||||||
- 1.4.2
|
- 1.5
|
||||||
|
- 1.6
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
script:
|
script:
|
||||||
|
15
README.md
15
README.md
@ -6,7 +6,7 @@
|
|||||||
[](https://godoc.org/github.com/gin-gonic/gin)
|
[](https://godoc.org/github.com/gin-gonic/gin)
|
||||||
[](https://gitter.im/gin-gonic/gin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[](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 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 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.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ func main() {
|
|||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.GET("/ping", func(c *gin.Context) {
|
r.GET("/ping", func(c *gin.Context) {
|
||||||
c.JSON(200, gin.H{
|
c.JSON(200, gin.H{
|
||||||
"message": "hello world",
|
"message": "pong",
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
r.Run() // listen and server on 0.0.0.0:8080
|
r.Run() // listen and server on 0.0.0.0:8080
|
||||||
@ -88,12 +88,19 @@ BenchmarkZeus_GithubAll | 2000 | 944234 | 300688 | 2648
|
|||||||
```sh
|
```sh
|
||||||
$ go get github.com/gin-gonic/gin
|
$ go get github.com/gin-gonic/gin
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Import it in your code:
|
2. Import it in your code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
3. (Optional) Import `net/http`. This is required for example if using constants such as `http.StatusOK`.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "net/http"
|
||||||
|
```
|
||||||
|
|
||||||
##API Examples
|
##API Examples
|
||||||
|
|
||||||
#### Using GET, POST, PUT, PATCH, DELETE and OPTIONS
|
#### Using GET, POST, PUT, PATCH, DELETE and OPTIONS
|
||||||
@ -115,7 +122,7 @@ func main() {
|
|||||||
// By default it serves on :8080 unless a
|
// By default it serves on :8080 unless a
|
||||||
// PORT environment variable was defined.
|
// PORT environment variable was defined.
|
||||||
router.Run()
|
router.Run()
|
||||||
// router.Run.Run(":3000") for a hard coded port
|
// router.Run(":3000") for a hard coded port
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -583,7 +590,7 @@ func main() {
|
|||||||
// /admin/secrets endpoint
|
// /admin/secrets endpoint
|
||||||
// hit "localhost:8080/admin/secrets
|
// hit "localhost:8080/admin/secrets
|
||||||
authorized.GET("/secrets", func(c *gin.Context) {
|
authorized.GET("/secrets", func(c *gin.Context) {
|
||||||
// get user, it was setted by the BasicAuth middleware
|
// get user, it was set by the BasicAuth middleware
|
||||||
user := c.MustGet(gin.AuthUserKey).(string)
|
user := c.MustGet(gin.AuthUserKey).(string)
|
||||||
if secret, ok := secrets[user]; ok {
|
if secret, ok := secrets[user]; ok {
|
||||||
c.JSON(http.StatusOK, gin.H{"user": user, "secret": secret})
|
c.JSON(http.StatusOK, gin.H{"user": user, "secret": secret})
|
||||||
|
@ -139,6 +139,28 @@ func TestValidationDisabled(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExistsSucceeds(t *testing.T) {
|
||||||
|
type HogeStruct struct {
|
||||||
|
Hoge *int `json:"hoge" binding:"exists"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var obj HogeStruct
|
||||||
|
req := requestWithBody("POST", "/", `{"hoge": 0}`)
|
||||||
|
err := JSON.Bind(req, &obj)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExistsFails(t *testing.T) {
|
||||||
|
type HogeStruct struct {
|
||||||
|
Hoge *int `json:"foo" binding:"exists"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var obj HogeStruct
|
||||||
|
req := requestWithBody("POST", "/", `{"boen": 0}`)
|
||||||
|
err := JSON.Bind(req, &obj)
|
||||||
|
assert.Error(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
|
func testFormBinding(t *testing.T, method, path, badPath, body, badBody string) {
|
||||||
b := Form
|
b := Form
|
||||||
assert.Equal(t, b.Name(), "form")
|
assert.Equal(t, b.Name(), "form")
|
||||||
|
@ -77,7 +77,7 @@ func (c *Context) Copy() *Context {
|
|||||||
return &cp
|
return &cp
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandlerName returns the main handle's name. For example if the handler is "handleGetUsers()", this
|
// HandlerName returns the main handler's name. For example if the handler is "handleGetUsers()", this
|
||||||
// function will return "main.handleGetUsers"
|
// function will return "main.handleGetUsers"
|
||||||
func (c *Context) HandlerName() string {
|
func (c *Context) HandlerName() string {
|
||||||
return nameOfFunction(c.handlers.Last())
|
return nameOfFunction(c.handlers.Last())
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package hello
|
package hello
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This function's name is a must. App Engine uses it to drive the requests properly.
|
// This function's name is a must. App Engine uses it to drive the requests properly.
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"net/http/httptest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testRequest(t *testing.T, url string) {
|
func testRequest(t *testing.T, url string) {
|
||||||
@ -103,3 +104,28 @@ func TestBadUnixSocket(t *testing.T) {
|
|||||||
router := New()
|
router := New()
|
||||||
assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
|
assert.Error(t, router.RunUnix("#/tmp/unix_unit_test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWithHttptestWithAutoSelectedPort(t *testing.T) {
|
||||||
|
router := New()
|
||||||
|
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||||
|
|
||||||
|
ts := httptest.NewServer(router)
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
testRequest(t, ts.URL+"/example")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWithHttptestWithSpecifiedPort(t *testing.T) {
|
||||||
|
router := New()
|
||||||
|
router.GET("/example", func(c *Context) { c.String(http.StatusOK, "it worked") })
|
||||||
|
|
||||||
|
l, _ := net.Listen("tcp", ":8033")
|
||||||
|
ts := httptest.Server{
|
||||||
|
Listener: l,
|
||||||
|
Config: &http.Server{Handler: router},
|
||||||
|
}
|
||||||
|
ts.Start()
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
testRequest(t, "http://localhost:8033/example")
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user