-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (176 loc) · 4.87 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"context"
"embed"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"path"
"time"
"github.com/apex/log"
"github.com/midgarco/movie_downloader/config"
moviedownloader "github.com/midgarco/movie_downloader/rpc/api/v1"
"github.com/spf13/viper"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/runtime"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
var (
configFile = flag.String("config", os.Getenv("HOME")+"/.pmd/agent.yaml", "The path to the config.yaml file")
)
func init() {
flag.Parse()
}
//go:embed frontend/dist
var assets embed.FS
func main() {
app := App{}
err := wails.Run(&options.App{
Title: "PMD Agent",
Width: 1024,
Height: 768,
Assets: assets,
Bind: []interface{}{
&app,
},
OnStartup: app.startup,
OnDomReady: app.domready,
})
if err != nil {
log.WithError(err).Error("failed to start")
}
}
type App struct {
ctx context.Context
endpoint string
conn *grpc.ClientConn
downloads map[int32]*moviedownloader.Progress
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// load the configuration
viper.SetConfigName("agent")
viper.SetConfigType("yaml")
viper.AddConfigPath(path.Dir(*configFile))
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found so create one
if err := config.Create(*configFile); err != nil {
runtime.LogError(a.ctx, fmt.Sprintf("Failed to create config file: %v", err))
}
} else {
runtime.LogFatal(a.ctx, fmt.Sprintf("Could not read in the config file: %v", err))
}
}
// update the configuration file
if err := viper.WriteConfig(); err != nil {
runtime.LogError(a.ctx, fmt.Sprintf("Failed to write config file: %v", err))
}
// set the endpoint
a.endpoint = viper.GetString("GRPC_ENDPOINT")
// build out the GRPC connection
if err := a.createConnection(); err != nil {
runtime.LogError(a.ctx, "failed to create grpc connection")
os.Exit(1)
}
}
func (a *App) GetEndpoint() string {
return viper.GetString("GRPC_ENDPOINT")
}
func (a *App) SaveEndpoint(endpoint string) error {
runtime.LogDebugf(a.ctx, "Save endpoint: %s", endpoint)
viper.Set("GRPC_ENDPOINT", endpoint)
// update the configuration file
if err := viper.WriteConfig(); err != nil {
runtime.LogErrorf(a.ctx, "Failed to write config file: %v", err)
return err
}
// set the endpoint
a.endpoint = viper.GetString("GRPC_ENDPOINT")
// build out the GRPC connection
if err := a.createConnection(); err != nil {
return err
}
a.domready(a.ctx)
return nil
}
func (a *App) createConnection() error {
var opts []grpc.DialOption
var kacp = keepalive.ClientParameters{
Time: time.Duration(10) * time.Second,
// Timeout: time.Second,
// PermitWithoutStream: true,
}
opts = append(opts, grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp))
conn, err := grpc.Dial(a.endpoint, opts...)
if err != nil {
runtime.LogError(a.ctx, err.Error())
return err
}
a.conn = conn
return nil
}
func (a *App) Search(query string) (*moviedownloader.SearchResponse, error) {
client := moviedownloader.NewMovieDownloaderServiceClient(a.conn)
req := &moviedownloader.SearchRequest{Query: query}
results, err := client.Search(context.Background(), req)
if err != nil {
return nil, err
}
return results, nil
}
func (a *App) Download(selected string) error {
movie := &moviedownloader.Movie{}
if err := json.Unmarshal([]byte(selected), movie); err != nil {
return err
}
client := moviedownloader.NewMovieDownloaderServiceClient(a.conn)
req := &moviedownloader.DownloadRequest{Movie: movie}
_, err := client.Download(context.Background(), req)
if err != nil {
return err
}
return nil
}
func (a *App) domready(ctx context.Context) {
runtime.LogInfo(a.ctx, "Start monitor active downloads")
client := moviedownloader.NewMovieDownloaderServiceClient(a.conn)
// display download content
go func(client moviedownloader.MovieDownloaderServiceClient) {
stream, err := client.Progress(ctx, &moviedownloader.ProgressRequest{})
if err != nil {
runtime.LogErrorf(a.ctx, "Error connecting to pmd service: %v", err)
return
}
log.Infof("Connected to progress stream %s", viper.GetString("GRPC_ENDPOINT"))
for {
res, err := stream.Recv()
if err == io.EOF {
runtime.LogWarning(a.ctx, "Server connection lost")
break
}
if err != nil {
runtime.LogErrorf(a.ctx, "Failure receiving progress updates: %v", err)
continue
}
if res == nil {
continue
}
a.downloads = res.ActiveDownloads
runtime.EventsEmit(ctx, "progress", a.downloads)
}
}(client)
}
func (a *App) Complete(id int32) error {
client := moviedownloader.NewMovieDownloaderServiceClient(a.conn)
req := &moviedownloader.CompletedRequest{CompletedId: id}
_, err := client.Completed(context.Background(), req)
if err != nil {
return err
}
return nil
}