From 054cbb910e29e7a035ae4eebab018b982b0e3fba Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Fri, 3 Dec 2021 11:36:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0gin=E7=BB=84=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/config.go | 8 ++++++++ gin/gin.go | 26 ++++++++++++++++++++++++++ gin/router.go | 26 ++++++++++++++++++++++++++ go.mod | 2 +- 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 gin/config.go create mode 100644 gin/gin.go create mode 100644 gin/router.go diff --git a/gin/config.go b/gin/config.go new file mode 100644 index 0000000..35826b8 --- /dev/null +++ b/gin/config.go @@ -0,0 +1,8 @@ +package gin + +//GIN 配置 +type Config struct { + RootPath string + Addr string + Port int +} diff --git a/gin/gin.go b/gin/gin.go new file mode 100644 index 0000000..ad1b2a4 --- /dev/null +++ b/gin/gin.go @@ -0,0 +1,26 @@ +package gin + +import ( + "fmt" + "log" + "net/http" + "time" + + "github.com/gin-gonic/gin" +) + +func Service(conf *Config) { + go func() { + router := gin.New() + routerSetup(router) + s := &http.Server{ + Addr: fmt.Sprintf("%s:%d", conf.Addr, conf.Port), + Handler: router, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + MaxHeaderBytes: 1 << 20, + } + log.Printf("start service on %s", fmt.Sprintf("%s:%d", conf.Addr, conf.Port)) + log.Fatal(s.ListenAndServe()) + }() +} diff --git a/gin/router.go b/gin/router.go new file mode 100644 index 0000000..5fa320b --- /dev/null +++ b/gin/router.go @@ -0,0 +1,26 @@ +package gin + +import ( + "github.com/gin-gonic/gin" +) + +//路由配置 +func routerSetup(router *gin.Engine) { + router.Use(gin.Recovery()) + router.GET(`/health/check`) + + r := router.Group(`/user`) + { + r.POST(`/register`) + r.GET(`/accountcheck/:accname`) + r.POST(`/login`) + r.POST(`/forgot`) + } + + ug := router.Group(`/user`) + { + ug.GET(`/choose/:orgid`) + ug.GET(`/detail`) + ug.POST(`/update`) + } +} diff --git a/go.mod b/go.mod index b943cf5..9d1ef68 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/gin-gonic/gin v1.7.4 github.com/gomodule/redigo v1.8.5 github.com/hashicorp/consul/api v1.10.1 + go.mongodb.org/mongo-driver v1.7.4 google.golang.org/grpc v1.40.0 gorm.io/driver/mysql v1.1.2 gorm.io/driver/sqlite v1.1.4 @@ -53,7 +54,6 @@ require ( github.com/xdg-go/scram v1.0.2 // indirect github.com/xdg-go/stringprep v1.0.2 // indirect github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect - go.mongodb.org/mongo-driver v1.7.4 // indirect golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect