@@ -11,42 +11,38 @@ import (
11
11
"syscall"
12
12
"time"
13
13
14
- "github.com/haozibi/leetcode-badge/internal/cache"
15
- "github.com/haozibi/leetcode-badge/internal/cache/memory"
16
- "github.com/haozibi/leetcode-badge/internal/cache/redis"
17
- "github.com/haozibi/leetcode-badge/internal/storage"
18
- "github.com/haozibi/leetcode-badge/internal/storage/mysql"
19
- "github.com/haozibi/leetcode-badge/static"
20
-
21
14
"github.com/gorilla/handlers"
22
15
"github.com/gorilla/mux"
23
- "github.com/haozibi/zlog"
24
- "github.com/pkg/errors"
16
+ "github.com/rs/zerolog/log"
25
17
"golang.org/x/sync/singleflight"
18
+
19
+ "github.com/haozibi/leetcode-badge/internal/cache"
20
+ "github.com/haozibi/leetcode-badge/internal/cache/memory"
21
+ "github.com/haozibi/leetcode-badge/internal/storage"
22
+ "github.com/haozibi/leetcode-badge/internal/storage/sqlite"
23
+ "github.com/haozibi/leetcode-badge/internal/tools"
26
24
)
27
25
28
26
type APP struct {
29
- debug bool
27
+ config Config
30
28
31
- config * Config
32
- cache cache.Cache
33
- store storage.Storage
34
- group * singleflight.Group
29
+ cache cache.Cache
30
+ store storage.Storage
35
31
32
+ group * singleflight.Group
36
33
mu sync.Mutex
37
34
userMap map [string ]time.Time
38
35
recordMap map [string ]time.Time
39
36
40
- wg WaitGroupWrapper
37
+ wg tools. WaitGroupWrapper
41
38
shutdown chan struct {}
42
39
shutdownComplete chan struct {}
43
40
}
44
41
45
- func New (c Config ) * APP {
42
+ func New (cfg Config ) * APP {
46
43
47
44
a := & APP {
48
- debug : c .Debug ,
49
- config : & c ,
45
+ config : cfg ,
50
46
group : new (singleflight.Group ),
51
47
userMap : make (map [string ]time.Time ),
52
48
recordMap : make (map [string ]time.Time ),
@@ -59,13 +55,15 @@ func New(c Config) *APP {
59
55
60
56
func (a * APP ) Run () error {
61
57
62
- if err := a .initConfig (); err != nil {
63
- return err
64
- }
58
+ var (
59
+ path = a .config .SqlitePath
60
+ enable = a .config .EnableCron
61
+ err error
62
+ )
65
63
66
- err := static .RestoreAssets ("./" , "static" )
64
+ a .cache = memory .New ()
65
+ a .store , err = sqlite .New (path )
67
66
if err != nil {
68
- zlog .ZError ().AnErr ("Static" , err ).Msg ("[Init]" )
69
67
return err
70
68
}
71
69
@@ -80,16 +78,14 @@ func (a *APP) Run() error {
80
78
})
81
79
}
82
80
83
- go a .quit (3 * time .Second )
84
- a .Cron (a .config .CronSpec )
85
-
81
+ go a .quit ()
86
82
a .wg .Wrap (func () {
87
83
exitFunc (a .runHTTP ())
88
84
})
89
85
90
- a . wg . Wrap ( func () {
91
- exitFunc ( a . runMonitor () )
92
- })
86
+ if enable {
87
+ a . Cron ( CronSpec )
88
+ }
93
89
94
90
select {
95
91
case <- a .shutdownComplete :
@@ -100,11 +96,16 @@ func (a *APP) Run() error {
100
96
}
101
97
102
98
func (a * APP ) runHTTP () error {
99
+
100
+ var (
101
+ address = a .config .Address
102
+ )
103
+
103
104
r := mux .NewRouter ()
104
- setRouter (r , a , ioutil .Discard )
105
+ Router (r , a , ioutil .Discard )
105
106
106
107
srv := & http.Server {
107
- Addr : a . config . Address ,
108
+ Addr : address ,
108
109
WriteTimeout : 120 * time .Second ,
109
110
ReadTimeout : 120 * time .Second ,
110
111
Handler : handlers .RecoveryHandler ()(r ),
@@ -117,16 +118,16 @@ func (a *APP) runHTTP() error {
117
118
defer cancel ()
118
119
err := srv .Shutdown (ctx )
119
120
if err != nil {
120
- zlog . ZError (). Msgf ( "[http] Shutdown %+v" , err )
121
+ log . Err ( err ). Msg ( "shutdown error" )
121
122
}
122
123
select {
123
124
case <- ctx .Done ():
124
- zlog . ZDebug ().Msg ("[http] timeout of 3 seconds." )
125
+ log . Debug ().Msg ("[http] timeout of 3 seconds." )
125
126
}
126
127
}
127
128
}()
128
129
129
- zlog . ZInfo ().Str ("Addr " , a . config . Address ).Msg ("[ http] " )
130
+ log . Info ().Str ("Address " , address ).Msg ("http listen " )
130
131
if err := srv .ListenAndServe (); err != nil &&
131
132
err != http .ErrServerClosed {
132
133
return err
@@ -135,73 +136,18 @@ func (a *APP) runHTTP() error {
135
136
return nil
136
137
}
137
138
138
- func (a * APP ) initConfig () error {
139
- var err error
140
-
141
- switch a .config .CacheType {
142
- case "redis" :
143
- a .cache , err = redis .New (
144
- a .config .RedisConfig .Address ,
145
- a .config .RedisConfig .Password ,
146
- )
147
- if err != nil {
148
- return err
149
- }
150
- case "memory" :
151
- a .cache = memory .New ()
152
- default :
153
- return errors .New ("not support cache type: " + a .config .CacheType )
154
- }
155
-
156
- zlog .ZInfo ().Msgf ("[cache] type: %s" , a .config .CacheType )
157
-
158
- switch a .config .StorageType {
159
- case "mysql" :
160
- a .store , err = mysql .New (
161
- a .config .MySQLConfig .DBName ,
162
- a .config .MySQLConfig .User ,
163
- a .config .MySQLConfig .Password ,
164
- a .config .MySQLConfig .Host ,
165
- a .config .MySQLConfig .Port ,
166
- )
167
- if err != nil {
168
- return err
169
- }
170
- default :
171
- return errors .New ("not support storage type: " + a .config .StorageType )
172
- }
173
-
174
- zlog .ZInfo ().Msgf ("[storage] type: %s" , a .config .StorageType )
175
-
176
- return nil
177
- }
178
-
179
- func (a * APP ) quit (out time.Duration ) error {
139
+ func (a * APP ) quit () {
180
140
181
141
quit := make (chan os.Signal )
182
142
signal .Notify (quit , syscall .SIGINT , syscall .SIGTERM )
183
143
<- quit
184
144
185
- zlog . ZInfo ().Msg ("[Server] Shutdown Server..." )
145
+ log . Info ().Msg ("[Server] Shutdown Server..." )
186
146
close (a .shutdown )
187
147
188
148
a .wg .Wait ()
189
149
close (a .shutdownComplete )
190
- return nil
191
- }
192
-
193
- // WaitGroupWrapper wrap sync.WaitGroup
194
- type WaitGroupWrapper struct {
195
- sync.WaitGroup
196
- }
197
-
198
- // Wrap wrap
199
- func (w * WaitGroupWrapper ) Wrap (cb func ()) {
200
- w .Add (1 )
201
- go func () {
202
- cb ()
203
- w .Done ()
204
- }()
150
+ return
205
151
}
206
152
207
153
func recordKey (name string , isCN bool ) string {
0 commit comments