This commit is contained in:
2024-04-02 14:36:18 +03:00
parent 7a865dcf1d
commit 9aa1216a15
26 changed files with 108 additions and 117 deletions
+53 -57
View File
@@ -513,19 +513,19 @@ Sample Output
```go
func main() {
router := gin.New()
// skip logging for desired paths by setting SkipPaths in LoggerConfig
loggerConfig := gin.LoggerConfig{SkipPaths: []string{"/metrics"}}
// skip logging based on your logic by setting Skip func in LoggerConfig
loggerConfig.Skip = func(c *gin.Context) bool {
// as an example skip non server side errors
return c.Writer.Status() < http.StatusInternalServerError
}
engine.Use(gin.LoggerWithConfig(loggerConfig))
router.Use(gin.Recovery())
// skipped
router.GET("/metrics", func(c *gin.Context) {
c.Status(http.StatusNotImplemented)
@@ -540,7 +540,7 @@ func main() {
router.GET("/data", func(c *gin.Context) {
c.Status(http.StatusNotImplemented)
})
router.Run(":8080")
}
@@ -714,8 +714,8 @@ import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"git.company.lan/gopkg/gin"
"git.company.lan/gopkg/gin/binding"
"github.com/go-playground/validator/v10"
)
@@ -782,7 +782,7 @@ import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
type Person struct {
@@ -820,7 +820,7 @@ import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
type Person struct {
@@ -870,7 +870,7 @@ package main
import (
"net/http"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
type Person struct {
@@ -908,7 +908,7 @@ import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
type testHeader struct {
@@ -967,21 +967,21 @@ form.html
```html
<form action="/" method="POST">
<p>Check some colors</p>
<label for="red">Red</label>
<input type="checkbox" name="colors[]" value="red" id="red">
<label for="green">Green</label>
<input type="checkbox" name="colors[]" value="green" id="green">
<label for="blue">Blue</label>
<input type="checkbox" name="colors[]" value="blue" id="blue">
<input type="submit">
<p>Check some colors</p>
<label for="red">Red</label>
<input type="checkbox" name="colors[]" value="red" id="red" />
<label for="green">Green</label>
<input type="checkbox" name="colors[]" value="green" id="green" />
<label for="blue">Blue</label>
<input type="checkbox" name="colors[]" value="blue" id="blue" />
<input type="submit" />
</form>
```
result:
```json
{"color":["red","green","blue"]}
{ "color": ["red", "green", "blue"] }
```
### Multipart/Urlencoded binding
@@ -1109,7 +1109,7 @@ func main() {
#### JSONP
Using JSONP to request data from a server in a different domain. Add callback to response body if the query parameter callback exists.
Using JSONP to request data from a server in a different domain. Add callback to response body if the query parameter callback exists.
```go
func main() {
@@ -1158,7 +1158,7 @@ func main() {
#### PureJSON
Normally, JSON replaces special HTML characters with their unicode entities, e.g. `<` becomes `\u003c`. If you want to encode such characters literally, you can use PureJSON instead.
Normally, JSON replaces special HTML characters with their unicode entities, e.g. `<` becomes `\u003c`. If you want to encode such characters literally, you can use PureJSON instead.
This feature is unavailable in Go 1.6 and lower.
```go
@@ -1193,7 +1193,7 @@ func main() {
router.StaticFS("/more_static", http.Dir("my_file_system"))
router.StaticFile("/favicon.ico", "./resources/favicon.ico")
router.StaticFileFS("/more_favicon.ico", "more_favicon.ico", http.Dir("my_file_system"))
// Listen and serve on 0.0.0.0:8080
router.Run(":8080")
}
@@ -1266,9 +1266,7 @@ templates/index.tmpl
```html
<html>
<h1>
{{ .title }}
</h1>
<h1>{{ .title }}</h1>
</html>
```
@@ -1296,10 +1294,9 @@ templates/posts/index.tmpl
```html
{{ define "posts/index.tmpl" }}
<html><h1>
{{ .title }}
</h1>
<p>Using posts/index.tmpl</p>
<html>
<h1>{{ .title }}</h1>
<p>Using posts/index.tmpl</p>
</html>
{{ end }}
```
@@ -1308,10 +1305,9 @@ templates/users/index.tmpl
```html
{{ define "users/index.tmpl" }}
<html><h1>
{{ .title }}
</h1>
<p>Using users/index.tmpl</p>
<html>
<h1>{{ .title }}</h1>
<p>Using users/index.tmpl</p>
</html>
{{ end }}
```
@@ -1354,7 +1350,7 @@ import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
func formatAsDate(t time.Time) string {
@@ -1417,7 +1413,7 @@ r.POST("/test", func(c *gin.Context) {
Issuing a Router redirect, use `HandleContext` like below.
``` go
```go
r.GET("/test", func(c *gin.Context) {
c.Request.URL.Path = "/test2"
r.HandleContext(c)
@@ -1579,7 +1575,7 @@ import (
"net/http"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
func main() {
@@ -1604,7 +1600,7 @@ import (
"net/http"
"github.com/gin-gonic/autotls"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
"golang.org/x/crypto/acme/autocert"
)
@@ -1638,7 +1634,7 @@ import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
"golang.org/x/sync/errgroup"
)
@@ -1732,9 +1728,9 @@ endless.ListenAndServe(":4242", router)
Alternatives:
* [grace](https://github.com/facebookgo/grace): Graceful restart & zero downtime deploy for Go servers.
* [graceful](https://github.com/tylerb/graceful): Graceful is a Go package enabling graceful shutdown of an http.Handler server.
* [manners](https://github.com/braintree/manners): A polite Go HTTP server that shuts down gracefully.
- [grace](https://github.com/facebookgo/grace): Graceful restart & zero downtime deploy for Go servers.
- [graceful](https://github.com/tylerb/graceful): Graceful is a Go package enabling graceful shutdown of an http.Handler server.
- [manners](https://github.com/braintree/manners): A polite Go HTTP server that shuts down gracefully.
#### Manually
@@ -1754,7 +1750,7 @@ import (
"syscall"
"time"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
func main() {
@@ -1812,7 +1808,7 @@ import (
"html/template"
"net/http"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
//go:embed assets/* templates/*
@@ -1983,12 +1979,12 @@ func SomeHandler(c *gin.Context) {
```
1. `c.ShouldBindBodyWith` stores body into the context before binding. This has
a slight impact to performance, so you should not use this method if you are
enough to call binding at once.
a slight impact to performance, so you should not use this method if you are
enough to call binding at once.
2. This feature is only needed for some formats -- `JSON`, `XML`, `MsgPack`,
`ProtoBuf`. For other formats, `Query`, `Form`, `FormPost`, `FormMultipart`,
can be called by `c.ShouldBind()` multiple times without any damage to
performance (See [#1341](https://github.com/gin-gonic/gin/pull/1341)).
`ProtoBuf`. For other formats, `Query`, `Form`, `FormPost`, `FormMultipart`,
can be called by `c.ShouldBind()` multiple times without any damage to
performance (See [#1341](https://github.com/gin-gonic/gin/pull/1341)).
### Bind form-data request with custom struct and custom tag
@@ -2057,7 +2053,7 @@ import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
var html = template.Must(template.New("https").Parse(`
@@ -2112,7 +2108,7 @@ import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
func main() {
@@ -2144,7 +2140,7 @@ func main() {
import (
"fmt"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
func main() {
@@ -2177,7 +2173,7 @@ or network CIDRs from where clients which their request headers related to clien
IP can be trusted. They can be IPv4 addresses, IPv4 CIDRs, IPv6 addresses or
IPv6 CIDRs.
**Attention:** Gin trust all proxies by default if you don't specify a trusted
**Attention:** Gin trust all proxies by default if you don't specify a trusted
proxy using the function above, **this is NOT safe**. At the same time, if you don't
use any proxy, you can disable this feature by using `Engine.SetTrustedProxies(nil)`,
then `Context.ClientIP()` will return the remote address directly to avoid some
@@ -2187,7 +2183,7 @@ unnecessary computation.
import (
"fmt"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
func main() {
@@ -2206,14 +2202,14 @@ 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.
to skip TrustedProxies check, it has a higher priority than TrustedProxies.
Look at the example below:
```go
import (
"fmt"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
func main() {
@@ -2249,7 +2245,7 @@ package main
import (
"net/http"
"github.com/gin-gonic/gin"
"git.company.lan/gopkg/gin"
)
func setupRouter() *gin.Engine {