2023-05-23 18:44:05 -04:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-07-12 19:16:28 -04:00
|
|
|
"os"
|
2023-05-23 21:52:00 -04:00
|
|
|
"strings"
|
2023-05-23 18:44:05 -04:00
|
|
|
"time"
|
|
|
|
|
2023-07-12 19:16:28 -04:00
|
|
|
"github.com/adrg/xdg"
|
2023-05-23 18:44:05 -04:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2023-05-23 20:07:48 -04:00
|
|
|
Env string `mapstructure:"env"`
|
|
|
|
BinaryPath string `mapstructure:"binaryPath"`
|
|
|
|
HTTP ConfigHTTP `mapstructure:"http"`
|
2023-05-23 20:19:25 -04:00
|
|
|
Cache ConfigCache `mapstructure:"cache"`
|
2023-05-23 20:07:48 -04:00
|
|
|
Cookies ConfigCookies `mapstructure:"cookies"`
|
2023-05-23 18:44:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) IsProduction() bool {
|
|
|
|
return c.Env == "Production"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) IsDevelopment() bool {
|
|
|
|
return c.Env == "Development"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) IsStaging() bool {
|
|
|
|
return c.Env == "Staging"
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigHTTP struct {
|
|
|
|
Port int `mapstructure:"port"`
|
|
|
|
Listen string `mapstructure:"listen"`
|
|
|
|
BasePath string `mapstructure:"basePath"`
|
|
|
|
TrustedProxies []string `mapstructure:"trustedProxies"`
|
|
|
|
}
|
|
|
|
|
2023-05-23 20:07:48 -04:00
|
|
|
type ConfigCache struct {
|
|
|
|
TTL time.Duration `mapstructure:"ttl"`
|
|
|
|
DirPath string `mapstructure:"dirPath"`
|
2023-05-23 18:44:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigCookies struct {
|
2023-05-23 21:52:00 -04:00
|
|
|
Enabled bool `mapstructure:"enabled"`
|
|
|
|
FilePath string `mapstructure:"filePath"`
|
|
|
|
FromBrowser ConfigCookiesFromBrowser `mapstructure:"fromBrowser"`
|
2023-05-23 18:44:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigCookiesFromBrowser struct {
|
|
|
|
Browser string `mapstructure:"browser"`
|
|
|
|
Keyring string `mapstructure:"keyring"`
|
|
|
|
Profile string `mapstructure:"profile"`
|
|
|
|
Container string `mapstructure:"container"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func DefaultConfig() *Config {
|
|
|
|
return &Config{
|
2023-07-12 16:51:05 -04:00
|
|
|
Env: "Production",
|
|
|
|
BinaryPath: "yt-dlp",
|
2023-05-23 18:44:05 -04:00
|
|
|
HTTP: ConfigHTTP{
|
|
|
|
Port: 8080,
|
|
|
|
Listen: "127.0.0.1",
|
|
|
|
BasePath: "/",
|
|
|
|
},
|
2023-05-23 20:07:48 -04:00
|
|
|
Cache: ConfigCache{
|
|
|
|
TTL: time.Hour,
|
|
|
|
DirPath: "/tmp/ytdl-web",
|
2023-05-23 18:44:05 -04:00
|
|
|
},
|
|
|
|
Cookies: ConfigCookies{
|
|
|
|
Enabled: false,
|
2023-05-23 20:07:48 -04:00
|
|
|
FilePath: "./cookies.txt",
|
2023-05-23 21:52:00 -04:00
|
|
|
FromBrowser: ConfigCookiesFromBrowser{},
|
2023-05-23 18:44:05 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfig(paths ...string) (*Config, error) {
|
|
|
|
v := viper.New()
|
|
|
|
|
|
|
|
v.SetEnvPrefix("YTDL")
|
2023-05-23 21:52:00 -04:00
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
2023-05-23 18:44:05 -04:00
|
|
|
v.AutomaticEnv()
|
|
|
|
|
|
|
|
v.SetConfigName("config")
|
|
|
|
v.SetConfigType("yaml")
|
|
|
|
|
|
|
|
if len(paths) > 0 {
|
|
|
|
for _, path := range paths {
|
|
|
|
v.AddConfigPath(path)
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-12 19:16:28 -04:00
|
|
|
envDir := os.Getenv("YTDL_CONFIGDIR")
|
|
|
|
if envDir != "" {
|
|
|
|
v.AddConfigPath(envDir)
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:44:05 -04:00
|
|
|
v.AddConfigPath(".")
|
2023-07-12 19:16:28 -04:00
|
|
|
|
|
|
|
homeDir, err := os.UserHomeDir()
|
|
|
|
if err == nil {
|
|
|
|
v.AddConfigPath(homeDir + "/.config/ytdl-web")
|
|
|
|
}
|
|
|
|
|
|
|
|
v.AddConfigPath(xdg.ConfigHome + "/ytdl-web")
|
|
|
|
for _, dir := range xdg.ConfigDirs {
|
|
|
|
v.AddConfigPath(dir + "/ytdl-web")
|
|
|
|
}
|
2023-05-23 18:44:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.ReadInConfig(); err != nil {
|
2023-05-23 21:52:00 -04:00
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-23 18:44:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
config := DefaultConfig()
|
|
|
|
if err := v.Unmarshal(config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|