Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add data binding and format rendering
- Loading branch information
Showing
6 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
// gin - 数据解析和绑定: Json | ||
|
||
// 定义接收数据的结构体 | ||
type LoginJson struct { | ||
// binding:"required"修饰的字段,若接收为空值,则报错,是必须字段 | ||
User string `form:"username" json:"user" uri:"user" xml:"user" binding:"required"` | ||
Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"` | ||
} | ||
|
||
func main() { | ||
// 1.创建路由 | ||
// 默认使用了2个中间件Logger(), Recovery() | ||
r := gin.Default() | ||
// JSON绑定 | ||
r.POST("loginJSON", func(c *gin.Context) { | ||
// 声明接收的变量 | ||
var json LoginJson | ||
// 将request的body中的数据,自动按照json格式解析到结构体 | ||
if err := c.ShouldBindJSON(&json); err != nil { | ||
// 返回错误信息 | ||
// gin.H封装了生成json数据的工具 | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
// 判断用户名密码是否正确 | ||
if json.User != "root" || json.Password != "admin" { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "304"}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{"status": "200"}) | ||
}) | ||
r.Run(":8000") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
// gin - 数据解析和绑定: Form | ||
|
||
// 定义接收数据的结构体 | ||
type LoginForm struct { | ||
// binding:"required"修饰的字段,若接收为空值,则报错,是必须字段 | ||
User string `form:"username" json:"user" uri:"user" xml:"user" binding:"required"` | ||
Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"` | ||
} | ||
|
||
func main() { | ||
// 1.创建路由 | ||
// 默认使用了2个中间件Logger(), Recovery() | ||
r := gin.Default() | ||
// JSON绑定 | ||
r.POST("/loginForm", func(c *gin.Context) { | ||
// 声明接收的变量 | ||
var form LoginForm | ||
// Bind()默认解析并绑定form格式 | ||
// 根据请求头中content-type自动推断 | ||
if err := c.Bind(&form); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
// 判断用户名密码是否正确 | ||
if form.User != "root" || form.Password != "admin" { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "304"}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{"status": "200"}) | ||
}) | ||
r.Run(":8000") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
// gin - 数据解析和绑定: URI | ||
|
||
// 定义接收数据的结构体 | ||
type LoginURI struct { | ||
// binding:"required"修饰的字段,若接收为空值,则报错,是必须字段 | ||
User string `form:"username" json:"user" uri:"user" xml:"user" binding:"required"` | ||
Password string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"` | ||
} | ||
|
||
func main() { | ||
// 1.创建路由 | ||
// 默认使用了2个中间件Logger(), Recovery() | ||
r := gin.Default() | ||
// JSON绑定 | ||
r.GET("/:user/:password", func(c *gin.Context) { | ||
// 声明接收的变量 | ||
var login LoginURI | ||
// Bind()默认解析并绑定form格式 | ||
// 根据请求头中content-type自动推断 | ||
if err := c.ShouldBindUri(&login); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
// 判断用户名密码是否正确 | ||
if login.User != "root" || login.Password != "admin" { | ||
c.JSON(http.StatusBadRequest, gin.H{"status": "304"}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, gin.H{"status": "200"}) | ||
}) | ||
r.Run(":8000") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/gin-gonic/gin/testdata/protoexample" | ||
) | ||
|
||
// gin - 各种数据格式的响应 | ||
|
||
// 多种响应方式 | ||
func main() { | ||
// 1.创建路由 | ||
// 默认使用了2个中间件Logger(), Recovery() | ||
router := gin.Default() | ||
// 1.json | ||
router.GET("/json", func(c *gin.Context) { | ||
c.JSON(200, gin.H{"msg": "json rendering", "status": 200}) | ||
}) | ||
// 2. 结构体响应 | ||
router.GET("/struct", func(c *gin.Context) { | ||
var msg struct { | ||
Name string | ||
Message string | ||
Number int | ||
} | ||
msg.Name = "struct" | ||
msg.Message = "message" | ||
msg.Number = 123 | ||
c.JSON(200, msg) | ||
}) | ||
// 3.XML | ||
router.GET("/xml", func(c *gin.Context) { | ||
c.XML(200, gin.H{"msg": "xml rendering"}) | ||
}) | ||
// 4.YAML响应 | ||
router.GET("/yaml", func(c *gin.Context) { | ||
c.YAML(200, gin.H{"msg": "yaml rendering"}) | ||
}) | ||
// 5.protobuf格式,谷歌开发的高效存储读取的工具 | ||
// 数组?切片?如果自己构建一个传输格式,应该是什么格式? | ||
router.GET("/protobuf", func(c *gin.Context) { | ||
reps := []int64{int64(1), int64(2)} | ||
// 定义数据 | ||
label := "protobuf" | ||
// 传protobuf格式数据 | ||
data := &protoexample.Test{ | ||
Label: &label, | ||
Reps: reps, | ||
} | ||
c.ProtoBuf(200, data) | ||
}) | ||
|
||
router.Run(":8000") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!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>Document</title> | ||
</head> | ||
<body> | ||
<!--form表单--> | ||
<form action="http://localhost:8000/loginForm" method="post" enctype="application/x-www-form-urlencoded"> | ||
用户名<input type="text" name="username"><br> | ||
密码<input type="password" name="password"> | ||
<input type="submit" value="提交"> | ||
</form> | ||
</body> | ||
</html> |