/
starrs.go
97 lines (82 loc) · 2.42 KB
/
starrs.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
package starrs
import (
"context"
"strings"
"time"
"github.com/Notifiarr/toolbarr/pkg/logs"
"github.com/Notifiarr/toolbarr/pkg/mnd"
"golift.io/starr"
)
// Starrs holds the running data and provides the frontend a place
// to interact with starr instances and their databases.
type Starrs struct {
ctx context.Context
app mnd.App
log *logs.Logger
}
// instance allows interacting with the instances via HTTP API using a standard interface.
type instance struct {
*Starrs
config *AppConfig
*starr.Config
}
// Instances is the configured list of instances. The map key is the instance type, e.g. Lidarr.
type Instances map[string][]AppConfig
// AppConfig is the configuration for an instance.
type AppConfig struct {
SSL bool // verify ssl cert?
Form bool // Use form login instead of basic auth?
Timeout time.Duration // How long to wait for the app's API.
App string // Radarr, Sonarr, etc
Name string // Custom name: Radarr2, Radarr4k, etc.
URL string // url to app.
User string // username for app.
Pass string // password for app.
Key string // api key for app.
DBPath string // path to database for app.
}
// Startup runs after wails inializes so we can save the context.
func Startup(ctx context.Context, starrs *Starrs, log *logs.Logger, app mnd.App) {
starrs.ctx = ctx
starrs.app = app
starrs.log = log
}
// Copy the instances list. Using copies provides thread safety at the risk of inconsistency.
func (i Instances) Copy() Instances {
instances := make(Instances)
for k := range i {
instances[k] = make([]AppConfig, len(i[k]))
copy(instances[k], i[k])
}
return instances
}
func (s *Starrs) newInstance(config *AppConfig) *instance {
starrConfig := &starr.Config{
APIKey: config.Key,
URL: strings.TrimSuffix(config.URL, "/") + "/",
Client: starr.Client(config.Timeout, config.SSL),
}
if config.Form {
starrConfig.Username = config.User
starrConfig.Password = config.Pass
} else {
starrConfig.HTTPUser = config.User
starrConfig.HTTPPass = config.Pass
}
return &instance{
Starrs: s,
config: config,
Config: starrConfig,
}
}
func (s *Starrs) newAPIinstance(config *AppConfig) (*instance, error) {
instance := s.newInstance(config)
if instance.APIKey == "" {
data, err := instance.testWithoutKey()
if err != nil {
return nil, err
}
instance.Config.APIKey = data.Key
}
return instance, nil
}