109 lines
2.3 KiB
Go
109 lines
2.3 KiB
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
|
||
|
"github.com/spf13/viper"
|
||
|
)
|
||
|
|
||
|
type Config struct {
|
||
|
Env string `mapstructure:"env"`
|
||
|
HTTP ConfigHTTP `mapstructure:"http"`
|
||
|
Ytdlp ConfigYtdlp `mapstructure:"ytdlp"`
|
||
|
Cookies ConfigCookies `mapstructure:"cookies"`
|
||
|
}
|
||
|
|
||
|
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"`
|
||
|
}
|
||
|
|
||
|
type ConfigYtdlp struct {
|
||
|
BinaryPath string `mapstructure:"binaryPath"`
|
||
|
Cache ConfigYtdlpCache `mapstructure:"cache"`
|
||
|
}
|
||
|
|
||
|
type ConfigYtdlpCache struct {
|
||
|
TTL time.Duration `mapstructure:"ttl"`
|
||
|
}
|
||
|
|
||
|
type ConfigCookies struct {
|
||
|
Enabled bool `mapstructure:"enabled"`
|
||
|
FilePath string `mapstructure:"filePath"`
|
||
|
FromBrowser *ConfigCookiesFromBrowser `mapstructure:"fromBrowser"`
|
||
|
}
|
||
|
|
||
|
type ConfigCookiesFromBrowser struct {
|
||
|
Browser string `mapstructure:"browser"`
|
||
|
Keyring string `mapstructure:"keyring"`
|
||
|
Profile string `mapstructure:"profile"`
|
||
|
Container string `mapstructure:"container"`
|
||
|
}
|
||
|
|
||
|
func DefaultConfig() *Config {
|
||
|
return &Config{
|
||
|
HTTP: ConfigHTTP{
|
||
|
Port: 8080,
|
||
|
Listen: "127.0.0.1",
|
||
|
BasePath: "/",
|
||
|
},
|
||
|
Ytdlp: ConfigYtdlp{
|
||
|
BinaryPath: "yt-dlp",
|
||
|
Cache: ConfigYtdlpCache{
|
||
|
TTL: time.Hour,
|
||
|
},
|
||
|
},
|
||
|
Cookies: ConfigCookies{
|
||
|
Enabled: false,
|
||
|
FilePath: "/tmp/ytdl-web.cookies",
|
||
|
FromBrowser: nil,
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func LoadConfig(paths ...string) (*Config, error) {
|
||
|
v := viper.New()
|
||
|
|
||
|
v.SetEnvPrefix("YTDL")
|
||
|
v.AutomaticEnv()
|
||
|
|
||
|
v.SetConfigName("config")
|
||
|
v.SetConfigType("yaml")
|
||
|
|
||
|
if len(paths) > 0 {
|
||
|
for _, path := range paths {
|
||
|
v.AddConfigPath(path)
|
||
|
}
|
||
|
} else {
|
||
|
v.AddConfigPath(".")
|
||
|
v.AddConfigPath("/etc/ytdl-web")
|
||
|
v.AddConfigPath("$HOME/.config/ytdl-web")
|
||
|
v.AddConfigPath("$XDG_CONFIG_HOME/ytdl-web")
|
||
|
}
|
||
|
|
||
|
if err := v.ReadInConfig(); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
config := DefaultConfig()
|
||
|
if err := v.Unmarshal(config); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return config, nil
|
||
|
}
|