目录
学习地址
gin 路由
routes group
gin 中间件
Cookie
重定向
同步异步
日志文件
学习地址
https://www.topgoer.com/gin%E6%A1%86%E6%9E%B6/
gin 路由
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// 1.创建路由
// 默认使用了2个中间件Logger(),Recovery()
// Default()和New()基本上一样,区别就是调用了Logger(),Recovery()中间件
r := gin.Default()
// 2.绑定路由规则,执行的函数
// gin.Context,封装了request和response
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "hello World!")
})
//API参数
//输入http://127.0.0.1:8000/user/我/哈哈哈
//输出我是哈哈哈
r.GET("/user/:name/*action", func(c *gin.Context) {
name := c.Param("name")
action := c.Param("action")
//截取/
action = strings.Trim(action, "/")
c.String(http.StatusOK, name+" is "+action)
})
//URL参数
//输入http://localhost:8080/user
//输出默认值哈哈哈
//输入http://127.0.0.1:8000/user?name=哈哈哈
//输出hello 哈哈哈
r.GET("/user", func(c *gin.Context) {
//指定默认值
//http://localhost:8080/user 才会打印出来默认的值
name := c.DefaultQuery("name", "哈哈哈")
c.String(http.StatusOK, fmt.Sprintf("hello %s", name))
})
// 3.监听端口,默认在8080
// Run("里面不指定端口号默认为8080")
r.Run(":8000")
}
routes group
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
// gin的helloWorld
func main() {
r := gin.Default()
// 路由组1 ,处理GET请求
v1 := r.Group("/v1")
// {} 是书写规范
{
v1.GET("/login", login)
v1.GET("submit", submit)
}
// 路由组2,处理POST请求
v2 := r.Group("/v2")
{
v2.POST("/login", login)
v2.POST("/submit", submit)
}
r.Run(":8000")
}
func login(c *gin.Context) {
name := c.DefaultQuery("name", "jack")
c.String(200, fmt.Sprintf("hello %s\n", name))
}
func submit(c *gin.Context) {
name := c.DefaultQuery("name", "lily")
c.JSON(http.StatusOK, gin.H{
"message": "Hello www.topgoer.com!",
})
c.String(200, fmt.Sprintf("hello %s\n", name))
}
效果演示:
gin 中间件
package main
import (
"fmt"
"time"
"github.com/gin-gonic/gin"
)
// 定义中间
func MiddleWare() gin.HandlerFunc {
return func(c *gin.Context) {
t := time.Now()
fmt.Println("中间件开始执行了")
// 设置变量到Context的key中,可以通过Get()取
c.Set("request", "中间件")
//调用该请求的剩余处理程序
//,指的是GET(relativePath string, handlers ...HandlerFunc)里的HandlerFunc
c.Next()
//c.Abort()//阻止调用后续的处理函数
// 中间件执行完后续的一些事情
status := c.Writer.Status()
fmt.Println("中间件执行完毕", status)
t2 := time.Since(t)
fmt.Println("time:", t2)
}
}
// 定义中间
func myTime(c *gin.Context) {
start := time.Now()
c.Next()
// 统计时间
since := time.Since(start)
fmt.Println("程序用时:", since)
}
func main() {
// 1.创建路由
// 默认使用了2个中间件Logger(), Recovery()
r := gin.Default()
// 注册中间件,全局中间件
r.Use(MiddleWare())
//也可以这样,注册全局中间件
//r.Use(myTime)
// {}为了代码规范
{
r.GET("/ce", func(c *gin.Context) {
// 取值,取c.Set()里的值
req, _ := c.Get("request")
fmt.Println("request:", req)
// 页面接收
c.JSON(200, gin.H{"request": req})
})
}
{
//A设置MiddleWare()为局部中间件,注意myTime可以换成MiddleWare()
r.POST("/ce", myTime, func(c *gin.Context) {
// 取值
req, _ := c.Get("request")
//服务器端打印
fmt.Println("request:", req)
// 页面接收
c.JSON(200, gin.H{"request": req})
})
}
shoppingGroup := r.Group("/shopping")
{
shoppingGroup.GET("/index", shopIndexHandler)
shoppingGroup.GET("/home", shopHomeHandler)
}
r.Run()
}
func shopIndexHandler(c *gin.Context) {
time.Sleep(5 * time.Second)
}
func shopHomeHandler(c *gin.Context) {
time.Sleep(3 * time.Second)
}
Cookie
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"fmt"
)
func main() {
r := gin.Default()
// 服务端要给客户端cookie
r.GET("cookie", func(c *gin.Context) {
// 获取客户端是否携带cookie
cookie, err := c.Cookie("key_cookie")
if err != nil {
cookie = "NotSet"
// 给客户端设置cookie
// maxAge int, 单位为秒
// path,cookie所在目录
// domain string,域名
// secure 是否智能通过https访问
// httpOnly bool 是否允许别人通过js获取自己的cookie
c.SetCookie("key_cookie", "value_cookie", 60, "/",
"localhost", false, true)
}
fmt.Printf("cookie的值是: %s\n", cookie)
})
r.GET("/login", func(c *gin.Context) {
// 设置cookie
//这里domain设置的localhost,访问的地址必须是localhost,不能使用127.0.0.1
c.SetCookie("abc", "123", 60, "/",
"localhost", false, true)
// 返回信息
c.String(200, "Login success!")
})
r.GET("/home", AuthMiddleWare(), func(c *gin.Context) {
c.JSON(200, gin.H{"data": "home"})
})
r.Run(":8000")
}
func AuthMiddleWare() gin.HandlerFunc {
return func(c *gin.Context) {
// 获取客户端cookie并校验
if cookie, err := c.Cookie("abc"); err == nil {
if cookie == "123" {
c.Next()
return
}
}
// 返回错误
c.JSON(http.StatusUnauthorized, gin.H{"error": "err"})
// 若验证不通过,不再调用后续的函数处理
c.Abort()
return
}
}
重定向
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/index", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "http://www.5lmh.com")
})
r.Run()
}
同步异步
package main
import (
"log"
"time"
"github.com/gin-gonic/gin"
)
func main() {
// 1.创建路由
// 默认使用了2个中间件Logger(), Recovery()
r := gin.Default()
// 1.异步
r.GET("/long_async", func(c *gin.Context) {
// 需要搞一个副本
copyContext := c.Copy()
// 异步处理
go func() {
time.Sleep(5 * time.Second)
log.Println("异步执行:" + copyContext.Request.URL.Path)
}()
})
// 2.同步
r.GET("/long_sync", func(c *gin.Context) {
time.Sleep(5 * time.Second)
log.Println("同步执行:" + c.Request.URL.Path)
})
r.Run(":8000")
}
日志文件
package main
import (
"io"
"os"
"github.com/gin-gonic/gin"
)
func main() {
gin.DisableConsoleColor()
// Logging to a file.
f, _ := os.Create("gin.log")
gin.DefaultWriter = io.MultiWriter(f)
// 如果需要同时将日志写入文件和控制台,请使用以下代码。
// gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
r.Run()
}