From a19b70610ef8236edc5661daaa861aeefbf9e56d Mon Sep 17 00:00:00 2001 From: w-mj Date: Wed, 15 Mar 2023 20:04:43 +0800 Subject: [PATCH] restful api --- main.go | 4 +++- src/controller/deployment_controller.go | 21 +++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 5292d00..ecdd825 100644 --- a/main.go +++ b/main.go @@ -60,7 +60,7 @@ func main() { r.POST("/dep", controller.CreateDeleteDeployment) r.DELETE("/dep", controller.CreateDeleteDeployment) - r.GET("/pod", controller.GetPodStatus) + r.GET("/pod/:id", controller.GetPodStatus) r.GET("/login", LoginToken) r.Any("/test_token", TestToken) @@ -71,6 +71,8 @@ func main() { r.POST("/file/:app/:filename", controller.GetPostOrDeleteFile) r.DELETE("/file/:app/:filename", controller.GetPostOrDeleteFile) + r.POST("/fork", controller.ForkApp) + err := r.Run(":8000") if err != nil { log.Fatal(err) diff --git a/src/controller/deployment_controller.go b/src/controller/deployment_controller.go index 1125346..565738f 100644 --- a/src/controller/deployment_controller.go +++ b/src/controller/deployment_controller.go @@ -2,6 +2,8 @@ package controller import ( "github.com/gin-gonic/gin" + log "github.com/sirupsen/logrus" + k8s_manager "k8s-manager/src" "k8s-manager/src/model" "k8s-manager/src/service" "k8s.io/api/apps/v1" @@ -23,17 +25,12 @@ type DeployStatusObject struct { func CreateDeleteDeployment(c *gin.Context) { tokenUsername := c.MustGet("username").(string) - id := c.Query("id") - if strings.TrimSpace(id) == "" { - c.JSON(http.StatusBadRequest, gin.H{"msg": "no app id"}) - return - } - appId, err := strconv.Atoi(id) + app := &model.AppModel{} + err := c.BindJSON(app) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"msg": "wrong id"}) - return + log.WithError(err).Error("CreateDeleteDeployment BindJSON") + k8s_manager.GinError(c, http.StatusBadRequest, err.Error()) } - app := model.AppModel{Id: appId} if app.Fill() != nil { c.JSON(http.StatusBadRequest, gin.H{"msg": "wrong id"}) return @@ -43,9 +40,9 @@ func CreateDeleteDeployment(c *gin.Context) { return } if c.Request.Method == http.MethodPost { - K.CreateApp(&app) + K.CreateApp(app) } else if c.Request.Method == http.MethodDelete { - K.DeleteApp(&app) + K.DeleteApp(app) } else { c.JSON(http.StatusMethodNotAllowed, gin.H{"msg": "unsupported method"}) } @@ -53,7 +50,7 @@ func CreateDeleteDeployment(c *gin.Context) { } func GetPodStatus(c *gin.Context) { - id := c.Query("id") + id := c.Param("id") if strings.TrimSpace(id) == "" { c.JSON(http.StatusBadRequest, gin.H{"msg": "no app id"}) return