tag crud router

master
Han Joker 7 months ago
parent 5106717b5e
commit 8f8e84ef94

@ -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
}

@ -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",
})
}

@ -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)
}

@ -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)
}

Loading…
Cancel
Save