From 2945a5d744bcd5a1b07e692a90b1f41f905d73fb Mon Sep 17 00:00:00 2001 From: han-joker Date: Thu, 17 Nov 2022 22:09:28 +0800 Subject: [PATCH] api --- api.go | 25 +++++++++++++++++++++++++ app/content/handler.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 api.go create mode 100644 app/content/handler.go diff --git a/api.go b/api.go new file mode 100644 index 0000000..edcf929 --- /dev/null +++ b/api.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/gin-gonic/gin" + "simpleCms/app/content" +) + +// 声明一个main包level的变量 +var router = gin.Default() + +// init 初始化时执行,用于完成API的定义 +func init() { + // 定义任意的uri,api + router.GET("/content/:id", content.Get) + router.DELETE("/content/:id", content.Delete) + router.PUT("/content/:id", content.Put) + router.POST("/content", content.Post) + + router.GET("/ping", func(c *gin.Context) { // & + //c.Query("")// ?name=xx + c.JSON(200, gin.H{ + "message": "pong", + }) + }) +} diff --git a/app/content/handler.go b/app/content/handler.go new file mode 100644 index 0000000..23f1d09 --- /dev/null +++ b/app/content/handler.go @@ -0,0 +1,31 @@ +package content + +// controller + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +// action +func Get(ctx *gin.Context) { + ctx.JSON(http.StatusOK, gin.H{ + "id": ctx.Param("id"), + "message": "内容查询", + }) +} +func Post(ctx *gin.Context) { + ctx.JSON(http.StatusOK, gin.H{ + "message": "内容添加", + }) +} +func Delete(ctx *gin.Context) { + ctx.JSON(http.StatusOK, gin.H{ + "message": "内容删除", + }) +} +func Put(ctx *gin.Context) { + ctx.JSON(http.StatusOK, gin.H{ + "message": "内容更新", + }) +}