gin框架学习

在这里插入图片描述

文章目录

gin框架学习

一、使用net包搭建web服务器

main.go

package main

import (
	"fmt"
	"net/http"
	"os"
)

// 编写一个方法使之在浏览器输入相应的url便可得到浏览器上响应的页面
func sayHello(w http.ResponseWriter, r *http.Request) {
	//将响应在网页的东西写在一个文件里面,通过读取文件里的内容进行响应
	b, _ := os.ReadFile("./hello.txt") //读hello.txt这个文件
	_, _ = fmt.Fprintln(w, string(b))  //进行输出操作
}
func main() {
	fmt.Println("hello world")
	//如果你向浏览器输入/hello我就给你执行sayHello这个函数
	http.HandleFunc("/hello", sayHello)
	//
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		fmt.Println("http Server failed,err:%v\n", err)
		return
	}

}

hello.txt

<h1 style='color:orange'> hello golang!<h1>
<h1>How are you golang! <h1>
<img id='i1' src='https://tse3-mm.cn.bing.net/th/id/OIP-C.xczgV5hOfPJlurtsgWHoqgAAAA?rs=1&pid=ImgDetMain'>
<button id='b1'>点我</button>
<script>
document.getElementById('b1').onclick = function(){
    document.getElementById("i1").src='https://ts1.cn.mm.bing.net/th/id/R-C.4a1c9198b9a745cce043a25b32b629b7?rik=q1h5vuqxdKsqkA&riu=http%3a%2f%2fn.sinaimg.cn%2fsinakd20123%2f83%2fw803h880%2f20200813%2f9bd2-ixreehp6472485.jpg&ehk=0pNKO48qg7f%2bBbs6LMqiPPsgzVMxavgL0vAcAJIVvzc%3d&risl=&pid=ImgRaw&r=0'
}
</script>

二、gin的初体验

1.什么是gin?

一个go语言的web框架

2.如何得到gin?(要保证你的go版本可以使用go mod)

go get -u github.com/gin-gonic/gin

3.案例初体验

package main

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

func sayHello(c *gin.Context) {//返回一个json格式的字符串
	c.JSON(200, gin.H{
		"message": "hello golang",
	})
}
func main() {
	r := gin.Default() //返回默认的路由引擎

	//指定用户使用GET请求访问/hello时,执行sayHello这个函数
	r.GET("/hello", sayHello)
	//不采用restful风格的写法
	//r.GET("/book",...)
	//r.GET("/create_book",...)
	//r.GET("/update_book",...)
	//r.GET("/delete_book",...)

    //restful风格:推荐使用,行业规范这里要打开postman用get,post delete....等方法去请求http://127.0.0.1:9090/book来查看是否可以获取json字符串
	r.GET("/book", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"method": "GET",
		})
	})

	r.POST("/book", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"method": "POST",
		})
	})

	r.PUT("/book", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "PUT",
		})
	})

	r.DELETE("/book", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "DELETE",
		})
	})

	//启动服务
	r.Run(":9090")
}

三、模板引擎

1.基于http的模板引擎内容

首先创建一个tmpl模板文件其实就是一个html文件

hello.tmpl

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <title>hello</title>
</head>
<body>
  <h1>Hello{{.}}</h1>
</body>
</html>

注意:{{.}}表示要传入的对象

然后编写一个main.go开始进行操作

package main

import (
	"fmt"
	"html/template"
	"net/http"
)

// 遇事不决,写注释
func sayHello(w http.ResponseWriter, r *http.Request) {
	//2.解析模板
	t, err := template.ParseFiles("./hello.tmpl")
	if err != nil {
		fmt.Println("Parse template fail err: %v", err)
		return
	}
	//3.渲染模板
	name := "小王子"
	err = t.Execute(w, name)
	if err != nil {
		fmt.Println("execution failed err: %v", err)
		return
	}

}
func main() {
	//通过http的方式
	http.HandleFunc("/", sayHello)
	err := http.ListenAndServe(":9000", nil)
	if err != nil {
		fmt.Println("http server start fail err: %v", err)
		return
	}
}

使用gin框架对于tmpl进行渲染

main.go

package main

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

func main() {
	r := gin.Default() //创建一个默认的路由
	//r.LoadHTMLFiles("./templates/posts/index.tmpl","./templates/users/index.tmpl") //模板的解析
	r.LoadHTMLGlob("./templates/**/*") //表示对templates下面所有的文件进行一个加载

	r.GET("/posts/index", func(c *gin.Context) {
		//http请求
		c.HTML(http.StatusOK, "posts/index.tmpl", gin.H{ //模板渲染
			"title": "posts/index.com",
		})
	})

templates/posts/index.tmpl

{{define "posts/index.tmpl"}}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>posts/index</title>
</head>
<body>
    {{.title}}
</body>
</html>
{{end}}

四、gin框架学习

1、gin处理json的数据

main.go

package main

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

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

	r.GET("/json", func(c *gin.Context) {
		//方法1:使用map,key是string类型的,后面的值是任意类型的数据
		//data := map[string]interface{}{
		//	"name": "小王子",
		//	"msg":  "hello world",
		//	"age":  18,
		//}
		data := gin.H{"name": "小王子", "message": "hello world", "age": 18}
		c.JSON(http.StatusOK, data)
	})

	//方法2:使用结构体,使用tag来定制化操作,让自己返回的json以自己想要的数据字段返回出去
	type msg struct {
		Name    string `json:"name"`
		Age     int    `json:"age"`
		Message string `json:"message"`
	}
	r.GET("/ajson", func(c *gin.Context) {
		data := msg{
			Name:    "小明",
			Age:     23,
			Message: "我是超人",
		}
		c.JSON(http.StatusOK, data)
	})

	r.Run(":9090")

}

2、query参数

main.go

package main

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

// querystring
func main() {

	r := gin.Default() //创建一个默认的路由
	
	//GETi请求url后面的是query参数是querysting
	//key=value格式,多个key-value值用&连接
	//eq:/web/qurey=小王子&age=78

	r.GET("/web", func(c *gin.Context) {
		//获取浏览器那边发请求qurey string参数
		//第一种
		name := c.Query("query") //通过query获取请求中携带的query参数
		age := c.Query("age")    //通过query获取请求中携带的query参数

		//第二种,没有传值的话就是默认sombody了
		//name := c.DefaultQuery("query", "somebody")
		//第三种
		//name, ok := c.GetQuery("query")//根据取不到就返回第二个false值
		//if !ok {
		//	//取不到
		//	name = "somebody"
		//}
		c.JSON(http.StatusOK, gin.H{
			"name": name,
			"age":  age,
		})
	})
	r.Run(":9090")
}

3、form表单提交

package main

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

// 获取form表单提交的参数
func main() {
	r := gin.Default()
	r.LoadHTMLFiles("./login.html", "./index.html")
	r.GET("/login", func(c *gin.Context) {
		c.HTML(http.StatusOK, "login.html", nil)
	})
	//login post的请求,一次请求对应一个响应
	r.POST("/login", func(c *gin.Context) {
		//获取form表单提交的数据
		//username := c.PostForm("username")
		//password := c.PostForm("password") //取到就返回值,取不到就是返回空
		//username := c.DefaultPostForm("username", "somebody")
		//password := c.DefaultPostForm("password", "***")
		username, ok := c.GetPostForm("username")
		if !ok {
			username = "sb"
		}

		password, _ := c.GetPostForm("password")

		c.HTML(http.StatusOK, "index.html", gin.H{
			"Name":     username,
			"Password": password,
		})
	})
	r.Run(":9090")
}

4、gin获取uri参数

package main

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

// 获取请求的URI(path)参数,返回的都是字符类型
//注意url的匹配不要冲突
func main() {
	r := gin.Default()

	r.GET("/:name/:age", func(c *gin.Context) {
		//获取路径参数
		name := c.Param("name")
		age := c.Param("age")
		c.JSON(http.StatusOK, gin.H{
			"name": name,
			"age":  age,
		})

	})

	r.GET("/blog/:year/:month", func(c *gin.Context) {
		year := c.Param("year")
		month := c.Param("month")
		c.JSON(http.StatusOK, gin.H{
			"year":  year,
			"month": month,
		})
	})
	
	r.Run(":9090")
}

5、gin参数绑定

package main

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

type UserInfo struct {
	Username string `form:"username" json:"user"`
	Password string `form:"password" json:"pwd"`
}

func main() {
	r := gin.Default()
	r.LoadHTMLFiles("./index.html")
	r.GET("/user", func(c *gin.Context) {
		//username := c.Query("username")
		//password := c.Query("password")
		//u := UserInfo{
		//	username: username,
		//	password: password,
		//}
		//以下代码就是对上面注释的进行简化替代操作
		var u UserInfo //声明一个UserInfo类型的变量u
		err := c.ShouldBind(&u)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"error": err.Error(),
			})
		} else {
			fmt.Printf("拿到的数据是:%#v\n", u)
			c.JSON(http.StatusOK, gin.H{
				"status": "ok",
			})
		}
	})

	r.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", nil)
	})
	//使用form表单的方式发送post请求
	r.POST("/form", func(c *gin.Context) {
		var u UserInfo //声明一个UserInfo类型的变量u
		err := c.ShouldBind(&u)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"error": err.Error(),
			})
		} else {
			fmt.Printf("拿到的数据是:%#v\n", u)
			c.JSON(http.StatusOK, gin.H{
				"status": "ok",
			})
		}

	})
	//在postman种使用json格式发送post请求
	r.POST("/json", func(c *gin.Context) {
		var u UserInfo //声明一个UserInfo类型的变量u
		err := c.ShouldBind(&u)
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"error": err.Error(),
			})
		} else {
			fmt.Printf("拿到的数据是:%#v\n", u)
			c.JSON(http.StatusOK, gin.H{
				"status": "ok",
			})
		}

	})
	r.Run(":9090")

}

6、文件上传

上传单个文件

main.go

package main
//上传单个文件
import (
	//"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"path"
)

func main() {
	r := gin.Default()
    //加载html文件就是写了一个html用post提交操作
	r.LoadHTMLFiles("./index.html")
	r.GET("/index", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", nil)
	})
	r.POST("/upload", func(c *gin.Context) {
		//从请求中读取文件
		f, err := c.FormFile("f1")
		if err != nil {
			c.JSON(http.StatusBadRequest, gin.H{
				"error": err.Error(),
			})
		} else {
			//将读取到的文件保存在本地
			//dst := fmt.Sprintf("./%s",f.Filename)
			dst := path.Join("./", f.Filename)
			c.SaveUploadedFile(f, dst)
			c.JSON(http.StatusOK, gin.H{
				"status": "ok",
			})
		}
	})

	r.Run(":8080")

}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="f1">
  <input type="submit" value="上传">
</form>
</body>
</html>

上传多个文件

func main() {
	router := gin.Default()
	// 处理multipart forms提交文件时默认的内存限制是32 MiB
	// 可以通过下面的方式修改
	// router.MaxMultipartMemory = 8 << 20  // 8 MiB
	router.POST("/upload", func(c *gin.Context) {
		// Multipart form
		form, _ := c.MultipartForm()
		files := form.File["file"]

		for index, file := range files {
			log.Println(file.Filename)
			dst := fmt.Sprintf("C:/tmp/%s_%d", file.Filename, index)
			// 上传文件到指定的目录
			c.SaveUploadedFile(file, dst)
		}
		c.JSON(http.StatusOK, gin.H{
			"message": fmt.Sprintf("%d files uploaded!", len(files)),
		})
	})
	router.Run()
}

7.请求重定向操作

main.go

package main

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

func main() {
	r := gin.Default()
	//http转发
	r.GET("/index", func(c *gin.Context) {
		//	c.JSON(http.StatusOK, gin.H{
		//		"status": "ok",
		//	})
		//进行重定向操作
		c.Redirect(http.StatusMovedPermanently, "http://www.bing.com")
	})
//gin 路由转发
	r.GET("/a", func(c *gin.Context) {
		//跳转到b的路由处理函数
		c.Request.URL.Path = "/b" //把请求的URI修改
		r.HandleContext(c)        //继续后续的处理

	})

	r.GET("/b", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "b",
		})
	})

	r.Run(":8080")
}

8、gin路由和路由组

main.go

package main

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

func main() {
	r := gin.Default()
	//访问/index的GET请求会走这一条处理逻辑
	//路由
	r.GET("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "GET",
		})
	})
	r.POST("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "POST",
		})

	})
	r.PUT("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "PUT",
		})
	})

	r.DELETE("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "DELETE",
		})
	})
	//Any可以处理我们刚看到的所有请求,当请求很多的时候
	r.Any("/user", func(c *gin.Context) {
		switch c.Request.Method {
		case "GET":
			c.JSON(http.StatusOK, gin.H{"method": "GET"})
		case http.MethodPost:
			c.JSON(http.StatusOK, gin.H{"method": "POST"})
			//.....
		}
	})
	//NO ROUET,访问不存在的页面
	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusNotFound, gin.H{"msg": "没有找到这个页面"})
	})

	时评的首页和详情页
	//r.GET("/video/index", func(c *gin.Context) {
	//	c.JSON(http.StatusOK, gin.H{"msg": "/video/index"})
	//})
	商城的首页和详情页
	//r.GET("/shop/index", func(c *gin.Context) {
	//	c.JSON(http.StatusOK, gin.H{"msg": "/shop/index"})
	//})

	//路由组的组,便于处理多个业务线
	videoGroup := r.Group("video")
	{
		videoGroup.GET("/index", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{"msg": "/video/index"})
		})

		videoGroup.GET("/xx", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{"msg": "/video/xx"})
		})
		videoGroup.GET("/oo", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{"msg": "/video/oo"})
		})
	}

	r.GET("/shop/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"msg": "/shop/index"})
	})

	r.GET("/shop/xx", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"msg": "/shop/index"})
	})

	r.GET("/shop/oo", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{"msg": "/shop/index"})
	})
    //路由组也是支持嵌套的
    shopGroup := r.Group("/shop")
    {
        shopGroup.GET("/index",func(c *gin.Context{...}))
        shopGroup.GET("/cart",func(c *gin.Context{...}))
        shopGroup.POST("/checkout",func(c *gin.Context{...}))
        //嵌套路由组
        xx := shopGroup.Group("xx")
        xx.GET("/OO",func(c *gin.Context){...})
    }
	r.Run(":9090")
}

9、gin中间件

1)介绍

Gin框架允许开发者在处理请求的过程中,加入用户自己的钩子(Hook)函数。这个钩子函数就叫中间件,中间件适合处理一些公共的业务逻辑,比如登录认证、权限校验、数据分页、记录日志、耗时统计等。

2)定义中间件
package main

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

// handlerFuncs
func indexHandler(c *gin.Context) {
	fmt.Println("index")
	name, ok := c.Get("name") //跨中间件取值
	if !ok {
		name = "匿名用户"
	}

	c.JSON(http.StatusOK, gin.H{
		"msg": name,
	})
}

// 定义一个中间件,统计处理函数的耗时
func m1(c *gin.Context) {
	fmt.Println("m1 in ..")
	//计时
	start := time.Now()
	c.Next() //调用后续的处理函数
	//c.Abort()//阻止后续的处理函数
	cost := time.Since(start)
	fmt.Printf("cost:%v\n", cost)
	fmt.Println("m1 out ..")
}

func m2(c *gin.Context) {
	fmt.Println("m2 in ..")
	c.Set("name", "qimi") //设置值
	//c.Abort() //调用后续的处理函数
	//return
	fmt.Println("m2 out ..")
}

func outMiddleware(docheck bool) gin.HandlerFunc {
	//连接数据库
	//或者一些其他的准备工作
	return func(c *gin.Context) {
		if docheck {
			//是否登录的判断
			//if 是登录用户
			c.Next()
			//else
			//c.Abort()
		} else {
			c.Next()
		}
	}
}
func main() {
	r := gin.Default()
	r.Use(m1, m2, outMiddleware(true)) //全局注册中间件函数

	r.GET("/index", indexHandler)
	r.GET("/shop", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "shop",
		})
	})

	r.GET("/user", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "user",
		})
	})

	路由组注册中间件1
	//xxGroup := r.Group("/xx",outMiddleware(true))
	//{
	//	xxGroup.GET("/index", func(c *gin.Context) {
	//		c.JSON(http.StatusOK, gin.H{
	//			"msg":"xxGroup",
	//		})
	//	})
	//
	//}
	//
	//
	路由组注册中间件2
	//xx2Group := r.Group("/xx2")
	//xx2Group.Use(outMiddleware(true))
	//{
	//	xx2Group.GET("/index", func(c *gin.Context) {
	//		c.JSON(http.StatusOK, gin.H{
	//			"msg":"xx2Group",
	//		})
	//	})
	//
	//}
	r.Run(":9090")
}

中间件注意事项

gin默认中间件

gin.Default()默认使用了LoggerRecovery中间件,其中:

  • Logger中间件将日志写入gin.DefaultWriter,即使配置了GIN_MODE=release
  • Recovery中间件会recover任何panic。如果有panic的话,会写入500响应码。

如果不想使用上面两个默认的中间件,可以使用gin.New()新建一个没有任何默认中间件的路由。

gin中间件中使用goroutine

当在中间件或handler中启动新的goroutine时,不能使用原始的上下文(c *gin.Context),必须使用其只读副本(c.Copy()

  • 18
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值