Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: Unified global router variable naming #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions content/en/docs/examples/ascii-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Using AsciiJSON to Generates ASCII-only JSON with escaped non-ASCII characters.

```go
func main() {
r := gin.Default()
router := gin.Default()

r.GET("/someJSON", func(c *gin.Context) {
router.GET("/someJSON", func(c *gin.Context) {
data := map[string]interface{}{
"lang": "GO语言",
"tag": "<br>",
Expand All @@ -20,6 +20,6 @@ func main() {
})

// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
router.Run(":8080")
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func GetDataD(c *gin.Context) {
}

func main() {
r := gin.Default()
r.GET("/getb", GetDataB)
r.GET("/getc", GetDataC)
r.GET("/getd", GetDataD)
router := gin.Default()
router.GET("/getb", GetDataB)
router.GET("/getc", GetDataC)
router.GET("/getd", GetDataD)

r.Run()
router.Run()
}
```

Expand Down
6 changes: 3 additions & 3 deletions content/en/docs/examples/bind-single-binary-with-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func main() {
if err != nil {
panic(err)
}
r.SetHTMLTemplate(t)
router.SetHTMLTemplate(t)

r.GET("/", func(c *gin.Context) {
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "/html/index.tmpl", nil)
})
r.Run(":8080")
router.Run(":8080")
}

// loadTemplate loads templates embedded by go-assets-builder
Expand Down
6 changes: 3 additions & 3 deletions content/en/docs/examples/custom-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ func Logger() gin.HandlerFunc {

func main() {
r := gin.New()
r.Use(Logger())
router.Use(Logger())

r.GET("/test", func(c *gin.Context) {
router.GET("/test", func(c *gin.Context) {
example := c.MustGet("example").(string)

// it would print: "12345"
log.Println(example)
})

// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
router.Run(":8080")
}
```

10 changes: 5 additions & 5 deletions content/en/docs/examples/define-format-for-the-log-of-routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ import (
)

func main() {
r := gin.Default()
router := gin.Default()
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)
}

r.POST("/foo", func(c *gin.Context) {
router.POST("/foo", func(c *gin.Context) {
c.JSON(http.StatusOK, "foo")
})

r.GET("/bar", func(c *gin.Context) {
router.GET("/bar", func(c *gin.Context) {
c.JSON(http.StatusOK, "bar")
})

r.GET("/status", func(c *gin.Context) {
router.GET("/status", func(c *gin.Context) {
c.JSON(http.StatusOK, "ok")
})

// Listen and Server in http://0.0.0.0:8080
r.Run()
router.Run()
}
```
8 changes: 4 additions & 4 deletions content/en/docs/examples/goroutines-inside-a-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ When starting new Goroutines inside a middleware or handler, you **SHOULD NOT**

```go
func main() {
r := gin.Default()
router := gin.Default()

r.GET("/long_async", func(c *gin.Context) {
router.GET("/long_async", func(c *gin.Context) {
// create copy to be used inside the goroutine
cCp := c.Copy()
go func() {
Expand All @@ -21,7 +21,7 @@ func main() {
}()
})

r.GET("/long_sync", func(c *gin.Context) {
router.GET("/long_sync", func(c *gin.Context) {
// simulate a long task with time.Sleep(). 5 seconds
time.Sleep(5 * time.Second)

Expand All @@ -30,6 +30,6 @@ func main() {
})

// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
router.Run(":8080")
}
```
6 changes: 3 additions & 3 deletions content/en/docs/examples/html-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ func main() {
You may use custom delims

```go
r := gin.Default()
r.Delims("{[{", "}]}")
r.LoadHTMLGlob("/path/to/templates")
router := gin.Default()
router.Delims("{[{", "}]}")
router.LoadHTMLGlob("/path/to/templates")
```

### Custom Template Funcs
Expand Down
10 changes: 5 additions & 5 deletions content/en/docs/examples/http2-server-push.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ var html = template.Must(template.New("https").Parse(`
`))

func main() {
r := gin.Default()
r.Static("/assets", "./assets")
r.SetHTMLTemplate(html)
router := gin.Default()
router.Static("/assets", "./assets")
router.SetHTMLTemplate(html)

r.GET("/", func(c *gin.Context) {
router.GET("/", func(c *gin.Context) {
if pusher := c.Writer.Pusher(); pusher != nil {
// use pusher.Push() to do server push
if err := pusher.Push("/assets/app.js", nil); err != nil {
Expand All @@ -45,7 +45,7 @@ func main() {
})

// Listen and Serve in https://127.0.0.1:8080
r.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")
router.RunTLS(":8080", "./testdata/server.pem", "./testdata/server.key")
}
```

6 changes: 3 additions & 3 deletions content/en/docs/examples/jsonp.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Using JSONP to request data from a server in a different domain. Add callback t

```go
func main() {
r := gin.Default()
router := gin.Default()

r.GET("/JSONP?callback=x", func(c *gin.Context) {
router.GET("/JSONP?callback=x", func(c *gin.Context) {
data := map[string]interface{}{
"foo": "bar",
}
Expand All @@ -20,6 +20,6 @@ func main() {
})

// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
router.Run(":8080")
}
```
8 changes: 4 additions & 4 deletions content/en/docs/examples/pure-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ This feature is unavailable in Go 1.6 and lower.

```go
func main() {
r := gin.Default()
router := gin.Default()

// Serves unicode entities
r.GET("/json", func(c *gin.Context) {
router.GET("/json", func(c *gin.Context) {
c.JSON(200, gin.H{
"html": "<b>Hello, world!</b>",
})
})

// Serves literal characters
r.GET("/purejson", func(c *gin.Context) {
router.GET("/purejson", func(c *gin.Context) {
c.PureJSON(200, gin.H{
"html": "<b>Hello, world!</b>",
})
})

// listen and serve on 0.0.0.0:8080
r.Run(":8080")
router.Run(":8080")
}
```
10 changes: 5 additions & 5 deletions content/en/docs/examples/redirects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ draft: false
Issuing a HTTP redirect is easy. Both internal and external locations are supported.

```go
r.GET("/test", func(c *gin.Context) {
router.GET("/test", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "http://www.google.com/")
})
```

Issuing a HTTP redirect from POST. Refer to issue: [#444](https://github.com/gin-gonic/gin/issues/444)

```go
r.POST("/test", func(c *gin.Context) {
router.POST("/test", func(c *gin.Context) {
c.Redirect(http.StatusFound, "/foo")
})
```

Issuing a Router redirect, use `HandleContext` like below.

``` go
r.GET("/test", func(c *gin.Context) {
router.GET("/test", func(c *gin.Context) {
c.Request.URL.Path = "/test2"
r.HandleContext(c)
router.HandleContext(c)
})
r.GET("/test2", func(c *gin.Context) {
router.GET("/test2", func(c *gin.Context) {
c.JSON(200, gin.H{"hello": "world"})
})
```
14 changes: 7 additions & 7 deletions content/en/docs/examples/rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ draft: false

```go
func main() {
r := gin.Default()
router := gin.Default()

// gin.H is a shortcut for map[string]interface{}
r.GET("/someJSON", func(c *gin.Context) {
router.GET("/someJSON", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
})

r.GET("/moreJSON", func(c *gin.Context) {
router.GET("/moreJSON", func(c *gin.Context) {
// You also can use a struct
var msg struct {
Name string `json:"user"`
Expand All @@ -27,15 +27,15 @@ func main() {
c.JSON(http.StatusOK, msg)
})

r.GET("/someXML", func(c *gin.Context) {
router.GET("/someXML", func(c *gin.Context) {
c.XML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
})

r.GET("/someYAML", func(c *gin.Context) {
router.GET("/someYAML", func(c *gin.Context) {
c.YAML(http.StatusOK, gin.H{"message": "hey", "status": http.StatusOK})
})

r.GET("/someProtoBuf", func(c *gin.Context) {
router.GET("/someProtoBuf", func(c *gin.Context) {
reps := []int64{int64(1), int64(2)}
label := "test"
// The specific definition of protobuf is written in the testdata/protoexample file.
Expand All @@ -49,6 +49,6 @@ func main() {
})

// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
router.Run(":8080")
}
```
8 changes: 4 additions & 4 deletions content/en/docs/examples/secure-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ Using SecureJSON to prevent json hijacking. Default prepends `"while(1),"` to re

```go
func main() {
r := gin.Default()
router := gin.Default()

// You can also use your own secure json prefix
// r.SecureJsonPrefix(")]}',\n")
// router.SecureJsonPrefix(")]}',\n")

r.GET("/someJSON", func(c *gin.Context) {
router.GET("/someJSON", func(c *gin.Context) {
names := []string{"lena", "austin", "foo"}

// Will output : while(1);["lena","austin","foo"]
c.SecureJSON(http.StatusOK, names)
})

// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
router.Run(":8080")
}
```
8 changes: 4 additions & 4 deletions content/en/docs/examples/support-lets-encrypt.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
)

func main() {
r := gin.Default()
router := gin.Default()

// Ping handler
r.GET("/ping", func(c *gin.Context) {
router.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})

Expand All @@ -41,10 +41,10 @@ import (
)

func main() {
r := gin.Default()
router := gin.Default()

// Ping handler
r.GET("/ping", func(c *gin.Context) {
router.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})

Expand Down
6 changes: 3 additions & 3 deletions content/en/docs/examples/using-basicauth-middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ var secrets = gin.H{
}

func main() {
r := gin.Default()
router := gin.Default()

// Group using gin.BasicAuth() middleware
// gin.Accounts is a shortcut for map[string]string
authorized := r.Group("/admin", gin.BasicAuth(gin.Accounts{
authorized := router.Group("/admin", gin.BasicAuth(gin.Accounts{
"foo": "bar",
"austin": "1234",
"lena": "hello2",
Expand All @@ -36,6 +36,6 @@ func main() {
})

// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
router.Run(":8080")
}
```
Loading