Gin框架
Gin框架
gin框架中的路由
gin框架中的路由使用的是httprouter这个库
- 简单路由
- 动态路由
函数:(输出数据的四种格式)
- ctx.String():返回一个字符串
r.GET("/", func(ctx *gin.Context) {
// 200为状态码
ctx.String(200, "string")
}) - ctx.JSON()
- ctx.XML()
- c.HTML()
- c.YAML()
1. gin遵循RESTful风格的api
2. API例子
3. HTTP常见请求方法:GET(查) POST(增) PUT(改) DELETE(删)
func main(){ |
4. 模板与渲染
Gin框架中使用ctx.HTML可以渲染模板,渲染模板前需要解析模板,使用LoadHTMLGlob()或者LoadHTMLFiles()方法加载模板
例:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X_UA_Compatible" content="IE=edge">
<meta name="viewPort" content="width=device-width, initial-scale=1.0">
<title> Document </title>
</head>
<body>
<h1>这是一个html模板</h1>
<h3>{{.title}}</h3>
</body>
</html>{{define "template/hello.tmpl"}}
<html lang="zh-CN">
<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>Hello</title>
</head>
<body>
<p>Hello {{.}}</p>
</body>
</html>
{{end}}r.LoadHTMLGlob("template/*")
//r.LoadHTMLFiles("template/hello.tmpl", "template/index.html")
r.GET("/hello", func(ctx *gin.Context) {
ctx.HTML(
http.StatusOK, "template/hello.tmpl",
gin.H{"title" : "首页"})
})
r.GET("/index", func(ctx *gin.Context) {
ctx.HTML(
http.StatusOK, "index.html",
gin.H{"title" : "首页"})
})
```
1. gin 自定义模板方法
```go
r.SetFuncMap(template.FuncMap{
"safe" : func(str string) template.HTML {
//将str进行html解析
return template.HTML(str)
},
})
//自定义函数需要在解析之前- 使用:修改index.html
<h3>{{.title | safe}}</h3>
模板调用safe函数
- 使用:修改index.html
静态文件:html页面中用到的样式文件,如.css, .js, 图片, 音乐等
router.Static("模板中引用的目录", "实际的目录(本机)")
5. 获取参数 url?xxx=xxx&xxx=xxx
- 获取请求参数
querystring
指的是URL中?
后面携带的参数- 函数
- context.DefaultQuery(“需要查询的字符串”, “默认值”)
- context.Query(“需要查询的字符串”)
- 动态获取
- context.Param()
router.GET("/xxx/:value1/:value2", func(context *gin.Context){
//获取动态传入的值
v1 := context.Param("value1")
v2 := context.Param("value2")
})
- context.Param()
- 表单处理
6. 路由
- 重定向
- url重定向,成功状态码:301
context.Redirect(301, "url")
- 路由重定向
context.Request.URL.Path = "路由"
router.HandleContext(context)
- url重定向,成功状态码:301
- NoRoute
//代表没有路由的页面(一般为404页面)
router.NoRouter(func()...) - 路由组
userGroup := router.Group("/user")
{
//代表:/user/add
userGroup.GET("/add", .....)
.........
}
7. 文件上传
- 单文件上传
- 多文件上传
- 同名多文件上传
- 上传文件的存储
- (例:按日期)
8. 自定义控制器
* 控制器处理
* 添加结构体
* 继承方法,控制器继承
9. 中间件(拦截器)
context.Set() //设置全局变量 |
10. Cookie
HTTP是无状态协议,对同一个网站的多次访问是没有任何关系的.
如果要实现多个页面的共享数据,可以使用Cookie或Session实现
cookie是存储在访问者的计算机的浏览器中的.
- 设置和获取cookie
ctx.SetCookie(name, value string, maxAge int, path domain string, secure, httpOnly bool)
- 删除cookie:设置maxAge为-1
11. Session
Session是另一种记录客户状态的机制,与Cookie不同,session是保存在服务器上的
- gin中没有提供session相关的文档,我们可以使用第三方的session中间件来实现
12. 服务器端口
//开启服务默认为8080端口,可自定义 |
13.重启或停止
使用http.Server内置的Shutdown()方法优雅的关机
14.写入日志文件
//LoggerWithFormatter 中间件会写入日志到gin.DefaultWriter |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 XiaoHao's Blog!