Skip to content

Commit 97f7e33

Browse files
committed
feat: 优化代码 & 增加 sqlite3
1 parent 3b30560 commit 97f7e33

29 files changed

+513
-611
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ data/
1818
docker-compose.yml
1919
static/*.go
2020
.idea/
21+
*.db

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ BUILD_FLAGS = "-v"
2424

2525
default: build
2626

27-
build: clean bindata govet
28-
CGO_ENABLED=0 GOOS= GOARCH= go build ${BUILD_FLAGS} -ldflags '${LDFLAGS}' -tags '${BUILD_TAGS}' -o ${APP}
27+
build: clean govet
28+
CGO_ENABLED=1 GOOS= GOARCH= go build ${BUILD_FLAGS} -ldflags '${LDFLAGS}' -tags '${BUILD_TAGS}' -o ${APP}
2929

3030

31-
build-linux: clean bindata govet
32-
CGO_ENABLED=0 GOOS=${GOOS} GOARCH=${GOARCH} go build ${BUILD_FLAGS} -ldflags '${LDFLAGS}' -tags '${BUILD_TAGS}' -o ${APP}
31+
build-linux: clean govet
32+
CGO_ENABLED=1 GOOS=${GOOS} GOARCH=${GOARCH} go build ${BUILD_FLAGS} -ldflags '${LDFLAGS}' -tags '${BUILD_TAGS}' -o ${APP}
3333

3434
bindata: clean
3535
@ export GOPROXY=https://goproxy.cn && go get github.com/jteeuwen/go-bindata/...

app/app.go

Lines changed: 38 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,38 @@ import (
1111
"syscall"
1212
"time"
1313

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-
2114
"github.com/gorilla/handlers"
2215
"github.com/gorilla/mux"
23-
"github.com/haozibi/zlog"
24-
"github.com/pkg/errors"
16+
"github.com/rs/zerolog/log"
2517
"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"
2624
)
2725

2826
type APP struct {
29-
debug bool
27+
config Config
3028

31-
config *Config
32-
cache cache.Cache
33-
store storage.Storage
34-
group *singleflight.Group
29+
cache cache.Cache
30+
store storage.Storage
3531

32+
group *singleflight.Group
3633
mu sync.Mutex
3734
userMap map[string]time.Time
3835
recordMap map[string]time.Time
3936

40-
wg WaitGroupWrapper
37+
wg tools.WaitGroupWrapper
4138
shutdown chan struct{}
4239
shutdownComplete chan struct{}
4340
}
4441

45-
func New(c Config) *APP {
42+
func New(cfg Config) *APP {
4643

4744
a := &APP{
48-
debug: c.Debug,
49-
config: &c,
45+
config: cfg,
5046
group: new(singleflight.Group),
5147
userMap: make(map[string]time.Time),
5248
recordMap: make(map[string]time.Time),
@@ -59,13 +55,15 @@ func New(c Config) *APP {
5955

6056
func (a *APP) Run() error {
6157

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+
)
6563

66-
err := static.RestoreAssets("./", "static")
64+
a.cache = memory.New()
65+
a.store, err = sqlite.New(path)
6766
if err != nil {
68-
zlog.ZError().AnErr("Static", err).Msg("[Init]")
6967
return err
7068
}
7169

@@ -80,16 +78,14 @@ func (a *APP) Run() error {
8078
})
8179
}
8280

83-
go a.quit(3 * time.Second)
84-
a.Cron(a.config.CronSpec)
85-
81+
go a.quit()
8682
a.wg.Wrap(func() {
8783
exitFunc(a.runHTTP())
8884
})
8985

90-
a.wg.Wrap(func() {
91-
exitFunc(a.runMonitor())
92-
})
86+
if enable {
87+
a.Cron(CronSpec)
88+
}
9389

9490
select {
9591
case <-a.shutdownComplete:
@@ -100,11 +96,16 @@ func (a *APP) Run() error {
10096
}
10197

10298
func (a *APP) runHTTP() error {
99+
100+
var (
101+
address = a.config.Address
102+
)
103+
103104
r := mux.NewRouter()
104-
setRouter(r, a, ioutil.Discard)
105+
Router(r, a, ioutil.Discard)
105106

106107
srv := &http.Server{
107-
Addr: a.config.Address,
108+
Addr: address,
108109
WriteTimeout: 120 * time.Second,
109110
ReadTimeout: 120 * time.Second,
110111
Handler: handlers.RecoveryHandler()(r),
@@ -117,16 +118,16 @@ func (a *APP) runHTTP() error {
117118
defer cancel()
118119
err := srv.Shutdown(ctx)
119120
if err != nil {
120-
zlog.ZError().Msgf("[http] Shutdown %+v", err)
121+
log.Err(err).Msg("shutdown error")
121122
}
122123
select {
123124
case <-ctx.Done():
124-
zlog.ZDebug().Msg("[http] timeout of 3 seconds.")
125+
log.Debug().Msg("[http] timeout of 3 seconds.")
125126
}
126127
}
127128
}()
128129

129-
zlog.ZInfo().Str("Addr", a.config.Address).Msg("[http]")
130+
log.Info().Str("Address", address).Msg("http listen")
130131
if err := srv.ListenAndServe(); err != nil &&
131132
err != http.ErrServerClosed {
132133
return err
@@ -135,73 +136,18 @@ func (a *APP) runHTTP() error {
135136
return nil
136137
}
137138

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() {
180140

181141
quit := make(chan os.Signal)
182142
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
183143
<-quit
184144

185-
zlog.ZInfo().Msg("[Server] Shutdown Server...")
145+
log.Info().Msg("[Server] Shutdown Server...")
186146
close(a.shutdown)
187147

188148
a.wg.Wait()
189149
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
205151
}
206152

207153
func recordKey(name string, isCN bool) string {

0 commit comments

Comments
 (0)