From bf4ee1aed6192dd28260792969442abf95f7628b Mon Sep 17 00:00:00 2001 From: Marshall Lee Date: Sun, 17 Apr 2022 11:50:22 +0900 Subject: [PATCH] add Gin framework --- cmd/root.go | 8 ++++++++ go.mod | 2 ++ request/request.go | 18 ++++++++++++++++++ route/route.go | 14 ++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 request/request.go create mode 100644 route/route.go diff --git a/cmd/root.go b/cmd/root.go index 68a0d87..12dd67b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -4,6 +4,7 @@ import ( "fmt" cnf "github.com/project-ginza/notifications/config" logging "github.com/project-ginza/notifications/log" + router "github.com/project-ginza/notifications/route" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "os" @@ -26,6 +27,13 @@ func run(cmd *cobra.Command, args []string) { logLevel := cnf.GlobalConfig.LogLevel setLogConfig(logLevel) + r := router.GetRouter() + // The application runs with port 7000. + if err := r.Run(":7000"); err != nil { + errMsg := fmt.Sprintf("Failed to run Gin Framework. %v", err) + loggingCh.PushErrorMessageToChannel(errMsg) + } + <-sigs } diff --git a/go.mod b/go.mod index 7db076c..0bf6727 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,11 @@ module github.com/project-ginza/notifications go 1.16 require ( + github.com/gin-gonic/gin v1.7.7 github.com/lib/pq v1.10.4 github.com/sirupsen/logrus v1.4.2 github.com/spf13/cobra v1.4.0 + github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.10.1 gorm.io/driver/postgres v1.3.1 gorm.io/gorm v1.23.3 diff --git a/request/request.go b/request/request.go new file mode 100644 index 0000000..efc4e6e --- /dev/null +++ b/request/request.go @@ -0,0 +1,18 @@ +package request + +import ( + "github.com/gin-gonic/gin" + logging "github.com/project-ginza/notifications/log" + "net/http" +) + +var code string = "code" +var message string = "message" +var loggingCh = logging.LoggingCh + +func Home(c *gin.Context) { + loggingCh.PushDebugMessageToChannel("home") + c.JSON(http.StatusOK, gin.H{ + code: http.StatusOK, + }) +} diff --git a/route/route.go b/route/route.go new file mode 100644 index 0000000..076921b --- /dev/null +++ b/route/route.go @@ -0,0 +1,14 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "github.com/project-ginza/notifications/request" +) + +func GetRouter() *gin.Engine { + router := gin.Default() + + router.GET("/", request.Home) + + return router +}