102 lines
2.2 KiB
Go
102 lines
2.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
App AppConfig `yaml:"app"`
|
|
Database DatabaseConfig `yaml:"database"`
|
|
Metadata MetadataConfig `yaml:"metadata"`
|
|
Indexers []IndexerConfig `yaml:"indexers"`
|
|
Torrent TorrentConfig `yaml:"torrent"`
|
|
Storage StorageConfig `yaml:"storage"`
|
|
}
|
|
|
|
type StorageConfig struct {
|
|
BasePath string `yaml:"base_path"`
|
|
}
|
|
|
|
type AppConfig struct {
|
|
Port int `yaml:"port"`
|
|
}
|
|
|
|
type DatabaseConfig struct {
|
|
URL string `yaml:"url"`
|
|
}
|
|
|
|
type MetadataConfig struct {
|
|
Endpoint string `yaml:"endpoint"`
|
|
}
|
|
|
|
type IndexerType string
|
|
|
|
const (
|
|
IndexerTypeJackett IndexerType = "jackett"
|
|
IndexerTypeProwlarr IndexerType = "prowlarr"
|
|
IndexerTypeTorznab IndexerType = "torznab"
|
|
)
|
|
|
|
type IndexerConfig struct {
|
|
Name string `yaml:"name"`
|
|
IndexerType IndexerType `yaml:"indexer_type"`
|
|
URL string `yaml:"url"`
|
|
APIKey string `yaml:"api_key"`
|
|
}
|
|
|
|
type TorrentClientType string
|
|
|
|
const (
|
|
TorrentClientQBittorrent TorrentClientType = "qbittorrent"
|
|
TorrentClientStub TorrentClientType = "stub"
|
|
TorrentClientNone TorrentClientType = "none"
|
|
)
|
|
|
|
type TorrentConfig struct {
|
|
ClientType TorrentClientType `yaml:"client_type"`
|
|
URL string `yaml:"url,omitempty"`
|
|
Username string `yaml:"username,omitempty"`
|
|
Password string `yaml:"password,omitempty"`
|
|
LogPath string `yaml:"log_path,omitempty"`
|
|
SavePath string `yaml:"save_path,omitempty"`
|
|
}
|
|
|
|
func Load(path string) (*Config, error) {
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read config file: %w", err)
|
|
}
|
|
|
|
var cfg Config
|
|
if err := yaml.Unmarshal(data, &cfg); err != nil {
|
|
return nil, fmt.Errorf("failed to parse config: %w", err)
|
|
}
|
|
|
|
if cfg.App.Port == 0 {
|
|
cfg.App.Port = 3000
|
|
}
|
|
|
|
for i := range cfg.Indexers {
|
|
if cfg.Indexers[i].IndexerType == "" {
|
|
cfg.Indexers[i].IndexerType = IndexerTypeJackett
|
|
}
|
|
}
|
|
|
|
if cfg.Torrent.ClientType == "" {
|
|
cfg.Torrent.ClientType = TorrentClientNone
|
|
}
|
|
|
|
if cfg.Torrent.SavePath == "" {
|
|
cfg.Torrent.SavePath = "/tmp/downloads"
|
|
}
|
|
|
|
if cfg.Storage.BasePath == "" {
|
|
cfg.Storage.BasePath = "/music"
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|