-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
116 lines (102 loc) · 2.32 KB
/
app.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package types
import (
"net/http"
"time"
"github.com/ZYallers/golib/utils/logger"
"github.com/ZYallers/zgin/consts"
"github.com/ZYallers/zgin/helper/grace"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis"
)
type Session struct {
Key string
KeyPrefix string
Expiration int64
ClientFunc func() *redis.Client
}
type Version struct {
Latest, Key string
}
type Logger struct {
Dir string
LogTimeout time.Duration
SendTimeout time.Duration
}
type Sign struct {
SecretKey string
Key string
TimeKey string
Dev string
Expiration int64
}
type Server struct {
Addr string
ReadTimeout time.Duration
WriteTimeout time.Duration
ShutDownTimeout time.Duration
Http *http.Server
}
type App struct {
Name string
Mode string
Version *Version
Logger *Logger
Server *Server
Sign *Sign
Session *Session
}
var DefaultApp = &App{
Name: "demo",
Mode: consts.DevMode,
Version: &Version{
Latest: "1.0.0",
Key: "app_version",
},
Session: &Session{
Key: "sess_token",
KeyPrefix: "ci_session:",
Expiration: 6 * 30 * 86400,
},
Logger: &Logger{
Dir: "apps/logs/go/demo",
LogTimeout: 10 * time.Second,
SendTimeout: 5 * time.Second,
},
Sign: &Sign{
SecretKey: "1234!@#$",
Key: "sess_token",
TimeKey: "utime",
Dev: "zgin-dev-sign",
Expiration: 60,
},
Server: &Server{
Addr: "0.0.0.0:9999",
ReadTimeout: 10 * time.Second,
WriteTimeout: 15 * time.Second,
ShutDownTimeout: 15 * time.Second,
},
}
func (a *App) SetMode(mode string) *App {
a.Mode = mode
if a.Mode == consts.DevMode {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
return a
}
func (a *App) GetVersion() (version, key string) {
return a.Version.Latest, a.Version.Key
}
func (a *App) GetSign() (secretKey, key, timeKey, dev string, expiration int64) {
return a.Sign.SecretKey, a.Sign.Key, a.Sign.TimeKey, a.Sign.Dev, a.Sign.Expiration
}
func (a *App) GetSession() (clientFunc func() *redis.Client, key, prefix string, expiration int64) {
return a.Session.ClientFunc, a.Session.Key, a.Session.KeyPrefix, a.Session.Expiration
}
func (a *App) Run(options ...func(app *App)) {
for _, opt := range options {
opt(a)
}
grace.Graceful(a.Server.Http, a.Server.ShutDownTimeout, logger.Use(a.Name))
}