From 1ab24b1efd2e218e01a45e3cd879708ecbf04c31 Mon Sep 17 00:00:00 2001 From: appleboy Date: Sat, 27 Jan 2024 23:02:26 +0800 Subject: [PATCH] feat: add new router and ping route to main application - Remove the import statement for "github.com/gin-gonic/gin" in main.go - Add a new file called router.go in the router directory - Create a new function called New() in router.go that returns a *gin.Engine - Add a GET route for "/ping" in the New() function in router.go Signed-off-by: appleboy --- main.go | 11 ++--------- router/router.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 router/router.go diff --git a/main.go b/main.go index 711202d..2de688f 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,11 @@ package main import ( - "net/http" - - "github.com/gin-gonic/gin" + "example/router" ) func main() { - r := gin.Default() - r.GET("/ping", func(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{ - "message": "pong", - }) - }) + r := router.New() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080") if err := r.Run(); err != nil { panic(err) diff --git a/router/router.go b/router/router.go new file mode 100644 index 0000000..aaa9a70 --- /dev/null +++ b/router/router.go @@ -0,0 +1,11 @@ +package router + +import "github.com/gin-gonic/gin" + +func New() *gin.Engine { + r := gin.Default() + r.GET("/ping", func(c *gin.Context) { + c.String(200, "pong") + }) + return r +}