Skip to content

Commit

Permalink
Provide custom options of TrustedPlatform for another CDN services (#…
Browse files Browse the repository at this point in the history
…2906)

* refine TrustedPlatform and docs

* refactor for switch

* refactor switch to if statement
  • Loading branch information
Bisstocuz authored and thinkerou committed Nov 20, 2021
1 parent 2207118 commit 2042af7
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Expand Up @@ -77,6 +77,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
- [http2 server push](#http2-server-push)
- [Define format for the log of routes](#define-format-for-the-log-of-routes)
- [Set and get a cookie](#set-and-get-a-cookie)
- [Don't trust all proxies](#don't-trust-all-proxies)
- [Testing](#testing)
- [Users](#users)

Expand Down Expand Up @@ -2164,6 +2165,34 @@ func main() {
}
```

**Notice:** If you are using a CDN service, you can set the `Engine.TrustedPlatform`
to skip TrustedProxies check, it has a higher priority than TrustedProxies.
Look at the example below:
```go
import (
"fmt"

"github.com/gin-gonic/gin"
)

func main() {

router := gin.Default()
// Use predefined header gin.PlatformXXX
router.TrustedPlatform = gin.PlatformGoogleAppEngine
// Or set your own trusted request header for another trusted proxy service
// Don't set it to any suspect request header, it's unsafe
router.TrustedPlatform = "X-CDN-IP"

router.GET("/", func(c *gin.Context) {
// If you set TrustedPlatform, ClientIP() will resolve the
// corresponding header and return IP directly
fmt.Printf("ClientIP: %s\n", c.ClientIP())
})
router.Run()
}
```

## Testing

The `net/http/httptest` package is preferable way for HTTP testing.
Expand Down

0 comments on commit 2042af7

Please sign in to comment.