golangWeb框架---github.com/gin-gonic/gin学习七(重定向、中间件Goroutines、http自定义配置)

文章目录
重定向
服务器内部重定向
中间件
中间件内部的Goroutines
HTTP自定义配置
重定向
package main

import (
"github.com/gin-gonic/gin"
"net/http"
)

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

router.GET("/raw", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently,"http://baidu.com")
})
router.Run(":8080")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
浏览器输入http://127.0.0.1:8080/raw,回车,神奇的跳转到百度界面

服务器内部重定向
我们还可以通过如下的写法来实现

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

router.GET("/test", func(c *gin.Context) {
c.Request.URL.Path = "/test2"
router.HandleContext(c)
})
router.GET("/test2", func(c *gin.Context) {
c.JSON(200, gin.H{"hello": "world"})
})

router.Run(":8080")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
上效果图:


中间件
package main

import (
"github.com/gin-gonic/gin"
"log"
"time"
)

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

r.GET("/test", func(c *gin.Context) {
example := c.MustGet("example").(string)
log.Println(example)
})
r.Run(":8080")
}

func Logger() gin.HandlerFunc {
return func(c *gin.Context) {
t := time.Now()
c.Set("example", "12345")
c.Next()

latency := time.Since(t)
log.Print(latency)

status := c.Writer.Status()
log.Println(status)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
浏览器输入http://127.0.0.1:8080/test
log日志如下:

2018/09/21 15:44:24 12345
2018/09/21 15:44:24 131.006µs
2018/09/21 15:44:24 200
1
2
3
中间件内部的Goroutines
在中间件或处理程序中启动新的Goroutine时,不应该使用其中的原始上下文,必须使用只读副本

package main

import (
"github.com/gin-gonic/gin"
"log"
"time"
)

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

r.GET("/long_async", func(c *gin.Context) {
// create copy to be used inside the goroutine
cCp := c.Copy()
go func() {
// simulate a long task with time.Sleep(). 5 seconds
time.Sleep(5 * time.Second)

// note that you are using the copied context "cCp", IMPORTANT
log.Println("Done! in path " + cCp.Request.URL.Path)
}()
})

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

// since we are NOT using a goroutine, we do not have to copy the context
log.Println("Done! in path " + c.Request.URL.Path)
})

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
浏览器发起请求,log日志输出

[GIN] 2018/09/21 - 17:40:20 | 200 | 2.451µs | 127.0.0.1 | GET /long_async
2018/09/21 17:40:25 Done! in path /long_async
[GIN] 2018/09/21 - 17:40:36 | 200 | 5.003324304s | 127.0.0.1 | GET /long_sync
2018/09/21 17:40:36 Done! in path /long_sync

1
2
3
4
5
HTTP自定义配置
func main() {
router := gin.Default()
http.ListenAndServe(":8080", router)
}
1
2
3
4
或者

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

s := &http.Server{
Addr: ":8080",
Handler: router,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}
————————————————
版权声明:本文为CSDN博主「丙申」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u013210620/java/article/details/82805113

golangWeb框架---github.com/gin-gonic/gin学习七(重定向、中间件Goroutines、http自定义配置)

上一篇:poj 2385 树上掉苹果问题 dp算法


下一篇:js练习关闭小广告