From d2fae8858ef26e1b861999c41e7311b6f697a50e Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Tue, 20 Aug 2024 15:45:03 +0800 Subject: [PATCH] add gin2 --- gin2/filter-gin.go | 37 ++++++++++++++++++++ gin2/gin.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++ gin2/router-gin.go | 16 +++++++++ 3 files changed, 139 insertions(+) create mode 100644 gin2/filter-gin.go create mode 100644 gin2/gin.go create mode 100644 gin2/router-gin.go diff --git a/gin2/filter-gin.go b/gin2/filter-gin.go new file mode 100644 index 0000000..844db8e --- /dev/null +++ b/gin2/filter-gin.go @@ -0,0 +1,37 @@ +package gin + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" +) + +func filterUser() gin.HandlerFunc { + return func(c *gin.Context) { + token := c.GetHeader("Authorization") + if token == "" { + token = c.Query("Authorization") + } + + staff, err := redis.UserGet(&token) + if err != nil { + logrus.WithFields(logrus.Fields{ + "func": "authUser", + }).Errorf("redis.UserGet: %s", err.Error()) + c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{ + "data": "无效TOKEN, 请重新登录!", + }) + return + } + + if staff == nil { + c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{ + "data": "无效TOKEN, 请重新登录!", + }) + return + } + c.Set("user", staff) + c.Next() + } +} diff --git a/gin2/gin.go b/gin2/gin.go new file mode 100644 index 0000000..2fae7e4 --- /dev/null +++ b/gin2/gin.go @@ -0,0 +1,86 @@ +package gin + +import ( + "fmt" + "log" + "net/http" + "os" + "strconv" + "time" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + "github.com/unrolled/secure" +) + +var ( + rootPath string + addr string + port int + ssl string + sslPem string + sslKey string +) + +func Service() { + rootPath = os.Getenv("GIN_PATH") + if rootPath == "" { + rootPath = "/" + } + addr = os.Getenv("GIN_ADDR") + if addr == "" { + addr = "0.0.0.0" + } + port, _ = strconv.Atoi(os.Getenv("GIN_PORT")) + if port == 0 { + port = 8080 + } + + ssl = os.Getenv("GIN_SSL") + sslPem = os.Getenv("GIN_SSL_PEM") + sslKey = os.Getenv("GIN_SSL_KEY") + + go func() { + router := gin.New() + routerSetup(router, &rootPath) + + if ssl == "true" { + router.Use(tlsHandler()) + } + + s := &http.Server{ + Addr: fmt.Sprintf("%s:%d", addr, port), + Handler: router, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + MaxHeaderBytes: 1 << 20, + } + + logrus.WithFields(logrus.Fields{ + "func": "Service", + }).Infof("start service on %s:%d", addr, port) + + if ssl == "true" { + log.Fatal(s.ListenAndServeTLS(sslPem, sslKey)) + } else { + log.Fatal(s.ListenAndServe()) + } + }() +} + +func tlsHandler() gin.HandlerFunc { + return func(c *gin.Context) { + secureMiddleware := secure.New(secure.Options{ + SSLRedirect: true, + SSLHost: ":" + strconv.Itoa(port), + }) + err := secureMiddleware.Process(c.Writer, c.Request) + + // If there was an error, do not continue. + if err != nil { + return + } + + c.Next() + } +} diff --git a/gin2/router-gin.go b/gin2/router-gin.go new file mode 100644 index 0000000..6726202 --- /dev/null +++ b/gin2/router-gin.go @@ -0,0 +1,16 @@ +package gin + +import ( + "fmt" + + "github.com/gin-gonic/gin" +) + +// 路由配置 +func routerSetup(router *gin.Engine, rootpath *string) { + router.Use(gin.Recovery()) + + r := router.Group(fmt.Sprintf("/%s", *rootpath)) + { + } +}