From 8f8e84ef946f48fbbe467eceb4b7efd00ed71e22 Mon Sep 17 00:00:00 2001 From: Han Joker <540090808@qq.com> Date: Fri, 29 Mar 2024 20:56:03 +0800 Subject: [PATCH] tag crud router --- handlers/router.go | 15 +++++++++++++++ handlers/tag/handler.go | 30 ++++++++++++++++++++++++++++++ handlers/tag/router.go | 18 ++++++++++++++++++ main.go | 32 ++------------------------------ 4 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 handlers/router.go create mode 100644 handlers/tag/handler.go create mode 100644 handlers/tag/router.go diff --git a/handlers/router.go b/handlers/router.go new file mode 100644 index 0000000..f53fb53 --- /dev/null +++ b/handlers/router.go @@ -0,0 +1,15 @@ +package handlers + +import ( + "cmsDemo/handlers/tag" + "github.com/gin-gonic/gin" +) + +func Router() *gin.Engine { + // 初始化默认路由 + router := gin.Default() + + tag.Router(router) + + return router +} diff --git a/handlers/tag/handler.go b/handlers/tag/handler.go new file mode 100644 index 0000000..b6e2a9b --- /dev/null +++ b/handlers/tag/handler.go @@ -0,0 +1,30 @@ +package tag + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func Post(ctx *gin.Context) { + ctx.JSON(http.StatusOK, gin.H{ + "message": "tag post", + }) +} + +func Delete(ctx *gin.Context) { + ctx.JSON(http.StatusOK, gin.H{ + "message": "tag delete", + }) +} + +func Put(ctx *gin.Context) { + ctx.JSON(http.StatusOK, gin.H{ + "message": "tag put", + }) +} + +func Get(ctx *gin.Context) { + ctx.JSON(http.StatusOK, gin.H{ + "message": "tag get", + }) +} diff --git a/handlers/tag/router.go b/handlers/tag/router.go new file mode 100644 index 0000000..7ccf5a8 --- /dev/null +++ b/handlers/tag/router.go @@ -0,0 +1,18 @@ +package tag + +import ( + "github.com/gin-gonic/gin" +) + +func Router(router *gin.Engine) { + // 定义路径和处理函数的匹配关系 + group := router.Group("tag") + // 添加tag + group.POST("", Post) + // 删除tag + group.DELETE("", Delete) + // 更新tag + group.PUT("", Put) + // 查询tag + group.GET("", Get) +} diff --git a/main.go b/main.go index 14c7e8c..f084edb 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,11 @@ package main import ( + "cmsDemo/handlers" "cmsDemo/models" "cmsDemo/utils" - "github.com/gin-gonic/gin" "github.com/spf13/viper" "log" - "net/http" ) func main() { @@ -15,35 +14,8 @@ func main() { utils.InitDB() models.Migrate() - // 初始化默认路由 - router := gin.Default() - // 定义路径和处理函数的匹配关系 - // 添加tag - router.POST("tag", func(ctx *gin.Context) { - ctx.JSON(http.StatusOK, gin.H{ - "message": "tag post", - }) - }) - // 删除tag - router.DELETE("tag", func(ctx *gin.Context) { - ctx.JSON(http.StatusOK, gin.H{ - "message": "tag delete", - }) - }) - // 更新tag - router.PUT("tag", func(ctx *gin.Context) { - ctx.JSON(http.StatusOK, gin.H{ - "message": "tag put", - }) - }) - // 查询tag - router.GET("tag", func(ctx *gin.Context) { - ctx.JSON(http.StatusOK, gin.H{ - "message": "tag get", - }) - }) - // 启动监听 + router := handlers.Router() if err := router.Run(viper.GetString("app.addr")); err != nil { log.Fatal(err) }