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

golangWeb框架---github.com/gin-gonic/gin学习七(重定向、中间件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")
}

浏览器输入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")
}

上效果图:
在这里插入图片描述

中间件

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)
	}
}

浏览器输入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

中间件内部的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")
}

浏览器发起请求,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

HTTP自定义配置

    func main() {
    	router := gin.Default()
    	http.ListenAndServe(":8080", router)
    }

或者

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/article/details/82805113
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢