-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathapp.go
130 lines (112 loc) · 2.96 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// Copyright (C) 2023 Quan Chen <chenquan_act@163.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"context"
"errors"
"github.com/op/go-logging"
"github.com/skratchdot/open-golang/open"
"github.com/terasum/medict/internal/entry"
"github.com/terasum/medict/internal/utils"
"github.com/terasum/medict/pkg/backserver"
"github.com/terasum/medict/pkg/model"
"go.etcd.io/etcd/client/pkg/v3/fileutil"
)
var log = logging.MustGetLogger("default")
// App struct
type App struct {
ctx context.Context
errorChannel chan error
stopChannel chan int
bs *backserver.BackServer
}
// NewApp creates a new App application struct
func NewApp() *App {
app := &App{
errorChannel: make(chan error),
stopChannel: make(chan int),
bs: &backserver.BackServer{Ready: false},
}
err := app.appInit()
if err != nil {
go func() {
app.errorChannel <- err
}()
}
return app
}
func (b *App) appInit() error {
conf, err := entry.LoadApp()
if err != nil {
return err
}
bs, err := backserver.NewStaticServer(conf)
if err != nil {
return err
}
err = bs.SetUp()
if err != nil {
return err
}
// assign backend server
b.bs = bs
// running bs, this is not blocking
bs.Start()
return nil
}
// startup is called at application startup
func (b *App) startup(ctx context.Context) {
go b.stopChanListen(ctx)
go b.errorChanListen(ctx)
}
// domReady is called after the front-end dom has been loaded
func (b *App) domReady(ctx context.Context) {
// Add your action here
}
// shutdown is called at application termination
func (b *App) shutdown(ctx context.Context) {
// Perform your teardown here
close(b.stopChannel)
close(b.errorChannel)
b.bs.GracefulStop()
}
func (b *App) Dispatch(apiName string, args map[string]interface{}) *model.Resp {
log.Infof("[wails] IPC request dispatch [%s] | args: %v\n", apiName, args)
return b.bs.DispatchIPCReq(apiName, args)
}
func (b *App) ResourceServerAddr() string {
return b.bs.StaticServerBaseUrl()
}
func (b *App) OpenFinder(filepath string) error {
if !fileutil.Exist(filepath) {
return errors.New("file path not exist, cannot open")
}
err := open.Run(filepath)
if err != nil {
return err
}
return nil
}
func (b *App) BaseDictDir() string {
if b.bs == nil {
return "internal error"
}
f, e := utils.ReplaceHome(b.bs.Config.BaseDictDir)
if e != nil {
return "internal error"
}
return f
}