GO语言gin框架实战-01-基本使用
2022-10-19 本文已影响0人
玄德公笔记
1. 定义路由服务
我们写一个简单的K8S示例来做完整示例:
package router
import (
"github.com/gin-gonic/gin"
)
func Server() {
r := gin.Default()
//创建一个groupV1组,里边放一个检查存活的接口
groupV1 := r.Group("/api/v1")
{
groupV1.GET("/ping", Ping)
}
//在创建一个deployment组,来放置deployment的操作相关接口
deployment := r.Group("/api/v1/deployment")
{
deployment.GET("/list",GetDeploymentList)
deployment.GET("/",GetDeployment)
deployment.DELETE("/",DeleteDeployment)
deployment.POST("/",CreateDeployment)
deployment.PUT("/image",ApplyDeploymentByImage)
deployment.PUT("/replicas",ApplyDeploymentByReplicas)
}
//启动服务,如果不写端口,默认8080
r.Run(":1840")
}
2. 定义headers函数
2.1 Get方法
Query(Params方式)
- 参数接收示例
deploymentName := c.Query("deployment_name")
- 完整代码示例
func GetDeployment(c *gin.Context) {
namespaceName := c.Query("namespace_name")
deploymentName := c.Query("deployment_name")
clientSet,err := service.ConnectK8s()
if err !=nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
deploymentInfo,err := service.GetDeployment(clientSet,namespaceName,deploymentName)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
fmt.Printf("%+v\n",deploymentInfo)
c.JSON(200,deploymentInfo)
}
说明:
(c *gin.Context)
必须要传入
-
postman调用
image.png
2.2 Post方法
BindJSON (Body=>raw=>JSON)
- 定义结构体如下
type DeploymentQueryParams struct {
NamespaceName string `json:"namespace_name"`
DeploymentName string `json:"deployment_name"`
ImageName string `json:"image_name"`
PortNum int32 `json:"port_num"`
Replica int32 `json:"replica"`
}
- 参数接收示例
err := c.BindJSON(&deploymentQueryParams)
- 代码完整示例
func CreateDeployment(c *gin.Context) {
var deploymentQueryParams service.DeploymentQueryParams
//传参绑定结构体
err := c.BindJSON(&deploymentQueryParams)
if err !=nil{
c.JSON(400, gin.H{"error": err.Error()})
return
}
clientSet,err := service.ConnectK8s()
if err !=nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
deploymentInfo,err := service.CreateSimpleDeployment(clientSet,deploymentQueryParams)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
fmt.Printf("%+v\n",deploymentInfo)
c.JSON(200,deploymentInfo)
}
-
postman调用
image.png
PostForm(Body=>form-data)
- 参数接收示例
deploymentName := c.PostForm("deployment_name")
- 完整示例
参见2.3 PUT方法
中"PostForm(Body=>form-data)" 部分。
2.3 PUT方法
BindJSON (Body=>raw=>JSON)
- 参数接收示例
err := BindJSON(&deploymentQueryParams)
- 完整代码示例
func ApplyDeploymentByImage(c *gin.Context) {
var deploymentQueryParams service.DeploymentQueryParams
c.BindJSON(&deploymentQueryParams)
clientSet,err := service.ConnectK8s()
if err !=nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
deploymentInfo,err := service.ApplyDeploymentByImage(clientSet,deploymentQueryParams.NamespaceName,deploymentQueryParams.DeploymentName,deploymentQueryParams.ImageName)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
fmt.Printf("%+v\n",deploymentInfo)
c.JSON(200,deploymentInfo)
}
-
postman 调用
image.png
PostForm(Body=>form-data)
- 参数接收示例
deploymentName := c.PostForm("deployment_name")
- 完整示例
修改一下上例(BindJSON)
func ApplyDeploymentByImageA(c *gin.Context) {
namespaceName := c.PostForm("namespace_name")
deploymentName := c.PostForm("deployment_name")
imageName := c.PostForm("image_name")
clientSet,err := service.ConnectK8s()
if err !=nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
deploymentInfo,err := service.ApplyDeploymentByImage(clientSet,namespaceName,deploymentName,imageName)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
fmt.Printf("%+v\n",deploymentInfo)
c.JSON(200,deploymentInfo)
}
2.4 DELETE方法
Query(Params方式)
- 参数接收示例
deploymentName := c.Query("deployment_name")
- 完整示例
func DeleteDeployment(c *gin.Context) {
namespaceName := c.Query("namespace_name")
deploymentName := c.Query("deployment_name")
clientSet,err := service.ConnectK8s()
if err !=nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
err = service.DeleteDeployment(clientSet,namespaceName,deploymentName)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}else {
c.JSON(200,"删除成功")
}
}
-
postman 调用
image.png
PostForm(Body=>form-data)
- 参数接收示例
deploymentName := c.PostForm("deployment_name")
- 完整示例
func DeleteDeployment(c *gin.Context) {
namespaceName := c.PostForm("namespace_name")
deploymentName := c.PostForm("deployment_name")
clientSet,err := service.ConnectK8s()
if err !=nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
err = service.DeleteDeployment(clientSet,namespaceName,deploymentName)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}else {
c.JSON(200,"删除成功")
}
}
Param (Path)
因为我们在k8s这个场景下不能一下定位到需要操作的资源,因此并不合适使用该方式,所以就不写完整示例了,只写一个样例:
- 路由设置
deployment.PUT("/:uuid",DeleteDeploymentA)
- 参数接收示例
uuid := c.Param("uuid")
3. main函数调用路由服务
func main() {
router.Server()
}
4. 数据返回和错误处理
- 返回数据
c.JSON(200,deploymentInfo)
- 错误处理
if err !=nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
- 示例
见 “2. 定义headers函数”中各示例均可看到:
func GetDeployment(c *gin.Context) {
namespaceName := c.Query("namespace_name")
deploymentName := c.Query("deployment_name")
clientSet,err := service.ConnectK8s()
if err !=nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
deploymentInfo,err := service.GetDeployment(clientSet,namespaceName,deploymentName)
if err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
fmt.Printf("%+v\n",deploymentInfo)
c.JSON(200,deploymentInfo)
}