-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
82 lines (70 loc) · 1.7 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
package main
import (
"context"
"log"
"path/filepath"
egClient "github.com/bonaysoft/engra/apis/client"
"github.com/bonaysoft/relingo-desktop/pkg/config"
"github.com/bonaysoft/relingo-desktop/pkg/dal"
"github.com/bonaysoft/relingo-desktop/pkg/dal/model"
"github.com/bonaysoft/relingo-desktop/pkg/dal/query"
"github.com/bonaysoft/relingo-desktop/pkg/proxy"
"github.com/bonaysoft/relingo-desktop/pkg/relingo"
"github.com/bonaysoft/relingo-desktop/pkg/service"
"github.com/bonaysoft/relingo-desktop/pkg/youdao"
)
// App struct
type App struct {
ctx context.Context
cm *config.Manager
query *query.Query
rProxy *proxy.Proxy
rc *relingo.Client
wordSvc *service.WordService
}
// NewApp creates a new App application struct
func NewApp() *App {
cm := config.NewCM()
cfg, err := cm.Load()
if err != nil {
log.Fatalln(err)
}
q, err := dal.Connect(filepath.Join(cm.GetConfigDir(), cfg.System.DBPath))
if err != nil {
log.Fatalln(err)
}
rc := relingo.NewClient(cfg.Relingo)
egc := egClient.NewClient(cfg.System.EngraURL)
return &App{
cm: cm,
query: q,
rc: rc,
rProxy: proxy.NewProxy(q, rc),
wordSvc: service.NewWordService(q, rc, egc),
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
go a.rProxy.Run()
}
func (a *App) shutdown(ctx context.Context) {
_ = a.rProxy.Stop(ctx)
if err := a.cm.Save(); err != nil {
log.Println(err)
return
}
}
func (a *App) getCtx() context.Context {
return a.ctx
}
func (a *App) getBinds() []interface{} {
return []interface{}{
a.rc,
a.wordSvc,
service.NewSystem(a.getCtx),
&model.Word{},
youdao.NewClient(),
}
}