-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (89 loc) · 2.64 KB
/
main.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
98
99
package main
import (
"context"
"fmt"
"net/http"
"os"
"time"
"gitee.com/binze/binzekeji/api"
"gitee.com/binze/binzekeji/config"
"gitee.com/binze/binzekeji/pkg/casbinx"
"gitee.com/binze/binzekeji/pkg/engine"
"gitee.com/binze/binzekeji/pkg/httpserver"
"gitee.com/binze/binzekeji/pkg/jwtauth"
"gitee.com/binze/binzekeji/pkg/logger"
"gitee.com/binze/binzekeji/pkg/mongors"
"gitee.com/binze/binzekeji/pkg/router"
"gitee.com/binze/binzekeji/pkg/shutdown"
"gitee.com/binze/binzekeji/service"
"github.com/gin-gonic/gin"
"go.uber.org/ratelimit"
"go.uber.org/zap"
)
func main() {
//环境变量注入后执行:export envflag=dev && go run -race main.go
envflag, ok := os.LookupEnv("envflag")
if !ok {
panic("envflag环境变量缺失,请设置envflag后重试")
}
//构造依赖对象
conf := config.FromYamlFile(envflag)
//log记录器
zlog := logger.NewJSONLogger(&conf.ZapConfig)
//mongo 副本集
mongors := mongors.New(&conf.MongoRSConfig)
//jwt
jwt := jwtauth.New(&conf.JwtConfig)
//casbin
ca := casbinx.NewAdapter(&conf.MongoAdapterConf)
//构造全家engine,注入依赖对象
_engine := engine.New(conf.EngineConfig.DataName)
_engine.Log = zlog
_engine.Ce = ca
_engine.Jwt = jwt
_engine.DBConn = mongors
//初始化服务模块资源对象
service.InitService(_engine, conf)
//全局route
_router := router.New()
//流量限速
limitrate := ratelimit.New(conf.EngineConfig.Rps)
_router.Use(_engine.LeakBucket(limitrate))
// //go运行时指标
// pprof.RouteRegister(_router)
//日志,panic拦截并记录
_router.Use(_engine.LogZap, _engine.RecoveryZap)
//检查mongo js注入攻击
_router.Use(_engine.DenyInjection)
//注册系统所有模块路由
api.RegisterRouter(_router)
//http服务
httpServer := httpserver.New(&conf.HttpServConfig, _router)
fmt.Fprintf(gin.DefaultWriter, "[GIN-debug] RATE"+fmt.Sprintf(" Current Rate Limit: %d requests/s", conf.Rps))
go func() {
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
zlog.Fatal("http server startup err", zap.Error(err))
}
}()
//释放资源
shutdown.New().Close(
func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
//优雅释放h2Server连接资源
if err := httpServer.Shutdown(ctx); err != nil {
zlog.Error("server shutdown fail", zap.Error(err))
} else {
zlog.Info("server shutdown success")
}
},
func() {
//关闭服务的数据库资源,日志缓存flush
if err := _engine.Close(); err != nil {
zlog.Error("svcResources release fail", zap.Error(err))
} else {
zlog.Info("svcResources release success")
}
},
)
}