-
Notifications
You must be signed in to change notification settings - Fork 0
/
gin.go
97 lines (88 loc) · 2.92 KB
/
gin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package ioc
import (
"context"
"strings"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/memstore"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"github.com/xiaoshanjiang/my-geektime/webook/internal/web"
ijwt "github.com/xiaoshanjiang/my-geektime/webook/internal/web/jwt"
"github.com/xiaoshanjiang/my-geektime/webook/internal/web/middleware"
"github.com/xiaoshanjiang/my-geektime/webook/pkg/ginx/middlewares/logger"
logger2 "github.com/xiaoshanjiang/my-geektime/webook/pkg/logger"
)
func InitWebServer(
mdls []gin.HandlerFunc,
userHdl *web.UserHandler,
oauth2WechatHdl *web.OAuth2WechatHandler,
articleHdl *web.ArticleHandler,
) *gin.Engine {
server := gin.Default()
server.Use(mdls...)
userHdl.RegisterRoutes(server)
articleHdl.RegisterRoutes(server)
oauth2WechatHdl.RegisterRoutes(server)
return server
}
func InitMiddlewares(redisClient redis.Cmdable,
l logger2.LoggerV1,
jwtHdl ijwt.Handler) []gin.HandlerFunc {
bd := logger.NewBuilder(func(ctx context.Context, al *logger.AccessLog) {
l.Debug("HTTP请求", logger2.Field{Key: "al", Value: al})
}).AllowReqBody(true).AllowRespBody()
viper.OnConfigChange(func(in fsnotify.Event) {
ok := viper.GetBool("web.logreq")
bd.AllowReqBody(ok)
})
return []gin.HandlerFunc{
corsHandler(),
bd.Build(),
middleware.NewLoginJWTMiddlewareBuilder(jwtHdl).Build(),
// ratelimit.NewBuilder(redisClient, time.Second, 100).Build(),
}
}
func corsHandler() gin.HandlerFunc {
return cors.New(cors.Config{
//AllowOrigins: []string{"*"},
//AllowMethods: []string{"POST", "GET"},
AllowHeaders: []string{"Content-Type", "Authorization"},
// 你不加这个,前端是拿不到的
ExposeHeaders: []string{"x-jwt-token"},
// 是否允许你带 cookie 之类的东西
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
if strings.HasPrefix(origin, "http://localhost") {
// 你的开发环境
return true
}
return strings.Contains(origin, "yourcompany.com")
},
MaxAge: 12 * time.Hour,
})
}
func sessionHandlerFunc() gin.HandlerFunc {
//store := cookie.NewStore([]byte("secret"))
// 这是基于内存的实现,第一个参数是 authentication key,最好是 32 或者 64 位
// 第二个参数是 encryption key
store := memstore.NewStore([]byte("moyn8y9abnd7q4zkq2m73yw8tu9j5ixm"),
[]byte("o6jdlo2cb9f9pb6h46fjmllw481ldebj"))
// 第一个参数是最大空闲连接数量
// 第二个就是 tcp,你不太可能用 udp
// 第三、四个 就是连接信息和密码
// 第五第六就是两个 key
//store, err := redis.NewStore(16, "tcp",
// "localhost:6379", "",
// // authentication key, encryption key
// []byte("moyn8y9abnd7q4zkq2m73yw8tu9j5ixm"),
// []byte("o6jdlo2cb9f9pb6h46fjmllw481ldebj"))
//if err != nil {
// panic(err)
//}
// cookie 的名字叫做ssid
return sessions.Sessions("ssid", store)
}