navidrome/conf/configuration.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

199 lines
5.5 KiB
Go
Raw Normal View History

2016-03-30 06:05:57 +02:00
package conf
import (
"fmt"
2016-03-30 06:05:57 +02:00
"os"
"path/filepath"
"strings"
2020-07-02 22:41:54 +02:00
"time"
2016-03-30 06:05:57 +02:00
2020-10-25 03:55:19 +01:00
"github.com/kr/pretty"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
2020-07-02 22:34:21 +02:00
"github.com/spf13/viper"
2016-03-30 06:05:57 +02:00
)
2020-07-02 23:21:25 +02:00
type configOptions struct {
2020-07-02 22:34:21 +02:00
ConfigFile string
Address string
2020-07-02 22:34:21 +02:00
Port int
MusicFolder string
DataFolder string
DbPath string
LogLevel string
2020-07-02 22:41:54 +02:00
ScanInterval time.Duration
SessionTimeout time.Duration
2020-07-02 22:34:21 +02:00
BaseURL string
UILoginBackgroundURL string
EnableTranscodingConfig bool
EnableDownloads bool
2020-07-02 22:34:21 +02:00
TranscodingCacheSize string
ImageCacheSize string
2020-08-03 05:17:13 +02:00
AutoImportPlaylists bool
SearchFullString bool
RecentlyAddedByModTime bool
IgnoredArticles string
IndexGroups string
ProbeCommand string
CoverArtPriority string
CoverJpegQuality int
UIWelcomeMessage string
EnableGravatar bool
EnableFavourites bool
GATrackingID string
AuthRequestLimit int
AuthWindowLength time.Duration
Scanner scannerOptions
2021-02-07 22:46:15 +01:00
Agents string
LastFM lastfmOptions
Spotify spotifyOptions
// DevFlags. These are used to enable/disable debugging and incomplete features
2020-07-02 22:34:21 +02:00
DevLogSourceLine bool
DevAutoCreateAdminPassword string
DevPreCacheAlbumArtwork bool
DevFastAccessCoverArt bool
2020-11-05 05:25:38 +01:00
DevOldCacheLayout bool
DevActivityPanel bool
2016-03-30 06:05:57 +02:00
}
type scannerOptions struct {
Extractor string
}
type lastfmOptions struct {
ApiKey string
Secret string
Language string
}
type spotifyOptions struct {
ID string
Secret string
}
2021-02-07 22:46:15 +01:00
var (
Server = &configOptions{}
hooks []func()
)
2020-01-08 16:25:23 +01:00
2020-07-02 22:34:21 +02:00
func LoadFromFile(confFile string) {
viper.SetConfigFile(confFile)
Load()
}
2020-07-02 22:34:21 +02:00
func Load() {
err := viper.Unmarshal(&Server)
if err != nil {
2020-07-02 22:34:21 +02:00
fmt.Println("Error parsing config:", err)
os.Exit(1)
}
err = os.MkdirAll(Server.DataFolder, os.ModePerm)
if err != nil {
fmt.Println("Error creating data path:", "path", Server.DataFolder, err)
os.Exit(1)
}
2020-07-02 22:34:21 +02:00
Server.ConfigFile = viper.GetViper().ConfigFileUsed()
if Server.DbPath == "" {
Server.DbPath = filepath.Join(Server.DataFolder, consts.DefaultDbPath)
}
2020-07-02 22:34:21 +02:00
log.SetLevelString(Server.LogLevel)
log.SetLogSourceLine(Server.DevLogSourceLine)
2020-10-25 03:55:19 +01:00
if log.CurrentLevel() >= log.LevelDebug {
pretty.Printf("Loaded configuration from '%s': %# v\n", Server.ConfigFile, Server)
}
2021-02-07 22:46:15 +01:00
// Call init hooks
for _, hook := range hooks {
hook()
}
}
// AddHook is used to register initialization code that should run as soon as the config is loaded
func AddHook(hook func()) {
hooks = append(hooks, hook)
2016-03-30 06:05:57 +02:00
}
2020-07-02 23:21:25 +02:00
func init() {
2020-07-28 14:49:07 +02:00
viper.SetDefault("musicfolder", filepath.Join(".", "music"))
viper.SetDefault("datafolder", ".")
2020-07-02 22:34:21 +02:00
viper.SetDefault("loglevel", "info")
viper.SetDefault("address", "0.0.0.0")
2020-07-02 22:34:21 +02:00
viper.SetDefault("port", 4533)
2020-07-02 22:41:54 +02:00
viper.SetDefault("sessiontimeout", consts.DefaultSessionTimeout)
viper.SetDefault("scaninterval", time.Minute)
2020-07-02 22:34:21 +02:00
viper.SetDefault("baseurl", "")
2021-03-24 15:21:31 +01:00
viper.SetDefault("uiloginbackgroundurl", consts.DefaultUILoginBackgroundURL)
2020-07-02 22:34:21 +02:00
viper.SetDefault("enabletranscodingconfig", false)
viper.SetDefault("transcodingcachesize", "100MB")
viper.SetDefault("imagecachesize", "100MB")
2020-08-03 05:17:13 +02:00
viper.SetDefault("autoimportplaylists", true)
viper.SetDefault("enabledownloads", true)
2020-07-02 22:34:21 +02:00
// Config options only valid for file/env configuration
viper.SetDefault("searchfullstring", false)
viper.SetDefault("recentlyaddedbymodtime", false)
2020-07-02 22:34:21 +02:00
viper.SetDefault("ignoredarticles", "The El La Los Las Le Les Os As O A")
viper.SetDefault("indexgroups", "A B C D E F G H I J K L M N O P Q R S T U V W X-Z(XYZ) [Unknown]([)")
viper.SetDefault("probecommand", "ffmpeg %s -f ffmetadata")
viper.SetDefault("coverartpriority", "embedded, cover.*, folder.*, front.*")
viper.SetDefault("coverjpegquality", 75)
viper.SetDefault("uiwelcomemessage", "")
viper.SetDefault("enablegravatar", false)
viper.SetDefault("enablefavourites", true)
viper.SetDefault("gatrackingid", "")
viper.SetDefault("authrequestlimit", 5)
viper.SetDefault("authwindowlength", 20*time.Second)
2020-07-02 22:34:21 +02:00
viper.SetDefault("scanner.extractor", "taglib")
2021-02-07 22:46:15 +01:00
viper.SetDefault("agents", "lastfm,spotify")
viper.SetDefault("lastfm.language", "en")
viper.SetDefault("lastfm.apikey", "")
viper.SetDefault("lastfm.secret", "")
viper.SetDefault("spotify.id", "")
viper.SetDefault("spotify.secret", "")
2020-07-02 22:34:21 +02:00
// DevFlags. These are used to enable/disable debugging and incomplete features
viper.SetDefault("devlogsourceline", false)
viper.SetDefault("devautocreateadminpassword", "")
viper.SetDefault("devprecachealbumartwork", false)
2020-11-05 05:25:38 +01:00
viper.SetDefault("devoldcachelayout", false)
viper.SetDefault("devFastAccessCoverArt", false)
viper.SetDefault("devactivitypanel", true)
2016-03-30 06:05:57 +02:00
}
2020-07-02 23:21:25 +02:00
2020-07-03 15:39:28 +02:00
func InitConfig(cfgFile string) {
cfgFile = getConfigFile(cfgFile)
2020-07-02 23:21:25 +02:00
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Search config in local directory with name "navidrome" (without extension).
viper.AddConfigPath(".")
viper.SetConfigName("navidrome")
}
2020-07-03 00:17:31 +02:00
_ = viper.BindEnv("port")
2020-07-02 23:21:25 +02:00
viper.SetEnvPrefix("ND")
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
2020-07-02 23:21:25 +02:00
viper.AutomaticEnv()
2020-07-03 16:19:44 +02:00
err := viper.ReadInConfig()
if cfgFile != "" && err != nil {
fmt.Println("Navidrome could not open config file: ", err)
os.Exit(1)
}
2020-07-02 23:21:25 +02:00
}
func getConfigFile(cfgFile string) string {
if cfgFile != "" {
return cfgFile
}
return os.Getenv("ND_CONFIGFILE")
}