-
Notifications
You must be signed in to change notification settings - Fork 0
/
envious.go
177 lines (151 loc) · 3.77 KB
/
envious.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
package main
import (
"context"
"embed"
"os"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/linux"
"github.com/wailsapp/wails/v2/pkg/options/windows"
"github.com/wailsapp/wails/v2/pkg/runtime"
"github.com/tywil04/tubed/internal/invidious"
"github.com/tywil04/tubed/internal/kv"
"github.com/tywil04/tubed/internal/proxy"
)
type Envious struct {
ctx context.Context
kv *kv.DB
invidious *invidious.Session
}
//go:embed all:frontend/dist
var frontend embed.FS
func main() {
envious := &Envious{}
err := wails.Run(&options.App{
Title: "Envious",
Width: 1024,
Height: 768,
AssetServer: &assetserver.Options{
Assets: frontend,
Middleware: proxy.Middleware,
},
BackgroundColour: &options.RGBA{R: 24, G: 24, B: 27, A: 1},
OnStartup: envious.onStartup,
OnShutdown: envious.onShutdown,
Frameless: true,
Windows: &windows.Options{
WebviewGpuIsDisabled: false,
Theme: windows.Dark,
},
Linux: &linux.Options{
WebviewGpuPolicy: linux.WebviewGpuPolicyOnDemand,
},
Bind: []interface{}{
envious,
},
})
if err != nil {
panic(err)
}
}
func (e *Envious) onStartup(ctx context.Context) {
e.ctx = ctx
db, err := kv.NewDB("Envious" + string(os.PathSeparator) + "envious.json")
if err != nil {
runtime.LogFatal(ctx, err.Error())
}
e.kv = db
instance, err := e.GetInvidiousInstance()
if err != nil {
runtime.LogFatal(ctx, err.Error())
}
e.invidious = invidious.NewSession(instance)
}
func (e *Envious) onShutdown(ctx context.Context) {
err := e.kv.Close()
if err != nil {
runtime.LogFatal(ctx, err.Error())
}
}
func (e *Envious) RestartApp() {
// as close as we can get to a full backend restart
e.onShutdown(e.ctx)
e.onStartup(e.ctx)
runtime.WindowReloadApp(e.ctx)
}
func (e *Envious) KVGet(key string) (string, error) {
return e.kv.Get("frontend." + key)
}
func (e *Envious) KVSet(key string, value string) error {
return e.kv.Set(kv.KV{K: "frontend." + key, V: value})
}
func (e *Envious) KVGetMultiple(keys []string) ([]string, error) {
values := make([]string, len(keys))
for i, key := range keys {
value, err := e.KVGet(key)
if err != nil {
return nil, err
}
values[i] = value
}
return values, nil
}
func (e *Envious) KVSetMultiple(kvs []kv.KV) error {
for _, kv := range kvs {
err := e.KVSet(kv.K, kv.V)
if err != nil {
return err
}
}
return nil
}
func (e *Envious) KVIsSet(key string) bool {
return e.kv.IsSet("frontend." + key)
}
func (e *Envious) GetInvidiousInstances() ([]*invidious.Instance, error) {
instances, err := invidious.GetInstances()
if err != nil {
return nil, err
}
return instances, nil
}
func (e *Envious) GetInvidiousInstance() (*invidious.Instance, error) {
values, err := e.kv.GetMultiple([]string{
"invidiousInstance.ApiUrl",
"invidiousInstance.Region",
})
if err != nil {
return nil, err
}
instance := &invidious.Instance{
ApiUrl: values[0],
Region: values[1],
}
return instance, nil
}
func (e *Envious) SetInvidiousInstance(instance *invidious.Instance) error {
err := e.kv.SetMultiple([]kv.KV{
{
K: "invidiousInstance.ApiUrl",
V: instance.ApiUrl,
},
{
K: "invidiousInstance.Region",
V: instance.Region,
},
})
if err != nil {
return err
}
return nil
}
func (e *Envious) GetTrendingVideos(trendingOptions invidious.TrendingOption) ([]invidious.Video, error) {
return e.invidious.GetTrendingVideos(trendingOptions)
}
func (e *Envious) GetVideo(videoId string) (invidious.Video, error) {
return e.invidious.GetVideo(videoId)
}
func (e *Envious) Search(query string, searchOptions invidious.SearchOption) ([]invidious.SearchItem, error) {
return e.invidious.Search(query, searchOptions)
}