add gin2
This commit is contained in:
parent
de0fde98c0
commit
d2fae8858e
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue