2016-03-30 00:05:57 -04:00
package conf
import (
2020-01-26 16:42:56 -05:00
"fmt"
2023-02-15 21:13:38 -05:00
"net/url"
2016-03-30 00:05:57 -04:00
"os"
2020-01-26 16:42:56 -05:00
"path/filepath"
2023-01-13 18:10:32 -05:00
"runtime"
2020-09-05 20:28:27 -04:00
"strings"
2020-07-02 16:41:54 -04:00
"time"
2016-03-30 00:05:57 -04:00
2020-10-24 22:55:19 -04:00
"github.com/kr/pretty"
2020-01-23 19:44:08 -05:00
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
2023-01-13 18:10:32 -05:00
"github.com/navidrome/navidrome/utils/number"
2021-05-06 17:56:10 -04:00
"github.com/robfig/cron/v3"
2020-07-02 16:34:21 -04:00
"github.com/spf13/viper"
2016-03-30 00:05:57 -04:00
)
2020-07-02 17:21:25 -04:00
type configOptions struct {
2023-01-18 14:20:06 -05:00
ConfigFile string
Address string
Port int
MusicFolder string
DataFolder string
DbPath string
LogLevel string
ScanInterval time . Duration
ScanSchedule string
SessionTimeout time . Duration
BaseURL string
2023-02-15 21:13:38 -05:00
BasePath string
BaseHost string
BaseScheme string
2023-03-17 16:07:01 -04:00
TLSCert string
TLSKey string
2023-01-18 14:20:06 -05:00
UILoginBackgroundURL string
2023-02-01 10:21:30 -05:00
UIWelcomeMessage string
MaxSidebarPlaylists int
2023-01-18 14:20:06 -05:00
EnableTranscodingConfig bool
EnableDownloads bool
EnableExternalServices bool
EnableMediaFileCoverArt bool
TranscodingCacheSize string
ImageCacheSize string
2023-02-06 09:50:04 -05:00
EnableArtworkPrecache bool
2023-01-18 14:20:06 -05:00
AutoImportPlaylists bool
PlaylistsPath string
AutoTranscodeDownload bool
DefaultDownsamplingFormat string
SearchFullString bool
RecentlyAddedByModTime bool
IgnoredArticles string
IndexGroups string
SubsonicArtistParticipations bool
2023-02-07 13:08:25 -05:00
FFmpegPath string
2023-01-18 14:20:06 -05:00
CoverArtPriority string
CoverJpegQuality int
2023-03-31 00:28:05 +02:00
ArtistArtPriority string
2023-01-18 14:20:06 -05:00
EnableGravatar bool
EnableFavourites bool
EnableStarRating bool
EnableUserEditing bool
2023-01-29 20:33:10 -05:00
EnableSharing bool
2023-03-10 23:01:03 -05:00
DefaultDownloadableShare bool
2023-01-18 14:20:06 -05:00
DefaultTheme string
DefaultLanguage string
DefaultUIVolume int
EnableReplayGain bool
EnableCoverAnimation bool
GATrackingID string
EnableLogRedacting bool
AuthRequestLimit int
AuthWindowLength time . Duration
PasswordEncryptionKey string
ReverseProxyUserHeader string
ReverseProxyWhitelist string
Prometheus prometheusOptions
Scanner scannerOptions
2021-02-07 16:46:15 -05:00
2021-11-17 21:11:53 -05:00
Agents string
LastFM lastfmOptions
Spotify spotifyOptions
ListenBrainz listenBrainzOptions
2020-09-05 20:28:27 -04:00
2020-01-22 08:32:31 -05:00
// DevFlags. These are used to enable/disable debugging and incomplete features
2023-01-13 18:10:32 -05:00
DevLogSourceLine bool
DevLogLevels map [ string ] string
2023-03-15 10:56:13 -04:00
DevEnableProfiler bool
2023-01-13 18:10:32 -05:00
DevAutoCreateAdminPassword string
DevAutoLoginUsername string
DevActivityPanel bool
DevSidebarPlaylists bool
DevEnableBufferedScrobble bool
DevShowArtistPage bool
DevArtworkMaxRequests int
DevArtworkThrottleBacklogLimit int
DevArtworkThrottleBacklogTimeout time . Duration
2023-02-02 16:55:12 -05:00
DevArtistInfoTimeToLive time . Duration
DevAlbumInfoTimeToLive time . Duration
2016-03-30 00:05:57 -04:00
}
2020-09-05 20:28:27 -04:00
type scannerOptions struct {
2021-07-15 19:53:40 -04:00
Extractor string
GenreSeparators string
2020-09-05 20:28:27 -04:00
}
2020-10-17 23:59:09 -04:00
type lastfmOptions struct {
2021-05-27 16:14:24 -04:00
Enabled bool
2020-10-17 23:59:09 -04:00
ApiKey string
Secret string
Language string
}
2020-10-18 13:23:02 -04:00
type spotifyOptions struct {
ID string
Secret string
}
2021-11-17 21:11:53 -05:00
type listenBrainzOptions struct {
Enabled bool
2022-09-27 21:06:28 -04:00
BaseURL string
2021-11-17 21:11:53 -05:00
}
2022-10-03 02:59:53 +03:00
type prometheusOptions struct {
Enabled bool
MetricsPath string
}
2021-02-07 16:46:15 -05:00
var (
Server = & configOptions { }
hooks [ ] func ( )
)
2020-01-08 10:25:23 -05:00
2020-07-02 16:34:21 -04:00
func LoadFromFile ( confFile string ) {
viper . SetConfigFile ( confFile )
Load ( )
2020-01-26 17:09:08 -05:00
}
2020-07-02 16:34:21 -04:00
func Load ( ) {
err := viper . Unmarshal ( & Server )
2020-01-26 17:09:08 -05:00
if err != nil {
2022-12-21 14:37:08 -05:00
_ , _ = fmt . Fprintln ( os . Stderr , "FATAL: Error parsing config:" , err )
2020-07-02 16:34:21 -04:00
os . Exit ( 1 )
2020-01-26 17:09:08 -05:00
}
2020-07-10 21:43:09 -04:00
err = os . MkdirAll ( Server . DataFolder , os . ModePerm )
if err != nil {
2022-12-21 14:37:08 -05:00
_ , _ = fmt . Fprintln ( os . Stderr , "FATAL: Error creating data path:" , "path" , Server . DataFolder , err )
2020-07-10 21:43:09 -04:00
os . Exit ( 1 )
}
2020-07-02 16:34:21 -04:00
Server . ConfigFile = viper . GetViper ( ) . ConfigFileUsed ( )
2020-01-26 16:42:56 -05:00
if Server . DbPath == "" {
2020-02-28 14:35:32 -05:00
Server . DbPath = filepath . Join ( Server . DataFolder , consts . DefaultDbPath )
2020-01-26 16:42:56 -05:00
}
2020-07-02 16:34:21 -04:00
2020-01-31 08:35:33 -05:00
log . SetLevelString ( Server . LogLevel )
2021-07-14 18:44:14 -04:00
log . SetLogLevels ( Server . DevLogLevels )
2020-01-31 08:35:33 -05:00
log . SetLogSourceLine ( Server . DevLogSourceLine )
2021-05-02 16:39:25 -04:00
log . SetRedacting ( Server . EnableLogRedacting )
2021-05-06 17:56:10 -04:00
if err := validateScanSchedule ( ) ; err != nil {
os . Exit ( 1 )
}
2023-02-15 21:13:38 -05:00
if Server . BaseURL != "" {
u , err := url . Parse ( Server . BaseURL )
if err != nil {
_ , _ = fmt . Fprintf ( os . Stderr , "FATAL: Invalid BaseURL %s: %s\n" , Server . BaseURL , err . Error ( ) )
os . Exit ( 1 )
}
Server . BasePath = u . Path
u . Path = ""
u . RawQuery = ""
Server . BaseHost = u . Host
Server . BaseScheme = u . Scheme
}
2021-05-30 16:01:36 -04:00
// Print current configuration if log level is Debug
2021-05-12 14:43:09 -04:00
if log . CurrentLevel ( ) >= log . LevelDebug {
2021-05-30 16:01:36 -04:00
prettyConf := pretty . Sprintf ( "Loaded configuration from '%s': %# v" , Server . ConfigFile , Server )
if Server . EnableLogRedacting {
prettyConf = log . Redact ( prettyConf )
}
2022-12-21 14:37:08 -05:00
_ , _ = fmt . Fprintln ( os . Stderr , prettyConf )
2021-05-12 14:43:09 -04:00
}
2021-02-07 16:46:15 -05:00
2021-10-30 18:00:32 -04:00
if ! Server . EnableExternalServices {
disableExternalServices ( )
}
2021-02-07 16:46:15 -05:00
// Call init hooks
for _ , hook := range hooks {
hook ( )
}
}
2021-10-30 18:00:32 -04:00
func disableExternalServices ( ) {
log . Info ( "All external integrations are DISABLED!" )
Server . LastFM . Enabled = false
Server . Spotify . ID = ""
2021-11-17 21:11:53 -05:00
Server . ListenBrainz . Enabled = false
2021-11-25 15:48:32 -05:00
Server . Agents = ""
2021-11-01 09:11:32 -04:00
if Server . UILoginBackgroundURL == consts . DefaultUILoginBackgroundURL {
Server . UILoginBackgroundURL = consts . DefaultUILoginBackgroundURLOffline
}
2021-10-30 18:00:32 -04:00
}
2021-05-06 17:56:10 -04:00
func validateScanSchedule ( ) error {
2021-05-12 14:08:38 -04:00
if Server . ScanInterval != - 1 {
2021-05-06 17:56:10 -04:00
log . Warn ( "ScanInterval is DEPRECATED. Please use ScanSchedule. See docs at https://navidrome.org/docs/usage/configuration-options/" )
if Server . ScanSchedule != "@every 1m" {
log . Error ( "You cannot specify both ScanInterval and ScanSchedule, ignoring ScanInterval" )
} else {
2021-05-12 14:08:38 -04:00
if Server . ScanInterval == 0 {
Server . ScanSchedule = ""
} else {
Server . ScanSchedule = fmt . Sprintf ( "@every %s" , Server . ScanInterval )
}
2021-05-06 17:56:10 -04:00
log . Warn ( "Setting ScanSchedule" , "schedule" , Server . ScanSchedule )
}
}
2021-08-03 19:37:36 -04:00
if Server . ScanSchedule == "0" || Server . ScanSchedule == "" {
2021-09-26 15:57:27 -04:00
Server . ScanSchedule = ""
2021-05-06 20:49:26 -04:00
return nil
}
if _ , err := time . ParseDuration ( Server . ScanSchedule ) ; err == nil {
Server . ScanSchedule = "@every " + Server . ScanSchedule
}
c := cron . New ( )
_ , err := c . AddFunc ( Server . ScanSchedule , func ( ) { } )
if err != nil {
log . Error ( "Invalid ScanSchedule. Please read format spec at https://pkg.go.dev/github.com/robfig/cron#hdr-CRON_Expression_Format" , "schedule" , Server . ScanSchedule , err )
2021-05-06 17:56:10 -04:00
}
2021-05-06 20:49:26 -04:00
return err
2021-05-06 17:56:10 -04:00
}
2021-02-07 16:46:15 -05:00
// 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 00:05:57 -04:00
}
2020-07-02 17:21:25 -04:00
func init ( ) {
2020-07-28 08:49:07 -04:00
viper . SetDefault ( "musicfolder" , filepath . Join ( "." , "music" ) )
viper . SetDefault ( "datafolder" , "." )
2020-07-02 16:34:21 -04:00
viper . SetDefault ( "loglevel" , "info" )
2020-07-08 20:54:56 -04:00
viper . SetDefault ( "address" , "0.0.0.0" )
2020-07-02 16:34:21 -04:00
viper . SetDefault ( "port" , 4533 )
2020-07-02 16:41:54 -04:00
viper . SetDefault ( "sessiontimeout" , consts . DefaultSessionTimeout )
2021-05-12 14:08:38 -04:00
viper . SetDefault ( "scaninterval" , - 1 )
2021-05-06 17:56:10 -04:00
viper . SetDefault ( "scanschedule" , "@every 1m" )
2020-07-02 16:34:21 -04:00
viper . SetDefault ( "baseurl" , "" )
2023-03-17 16:07:01 -04:00
viper . SetDefault ( "tlscert" , "" )
viper . SetDefault ( "tlskey" , "" )
2021-03-24 10:21:31 -04:00
viper . SetDefault ( "uiloginbackgroundurl" , consts . DefaultUILoginBackgroundURL )
2023-02-01 10:21:30 -05:00
viper . SetDefault ( "uiwelcomemessage" , "" )
viper . SetDefault ( "maxsidebarplaylists" , consts . DefaultMaxSidebarPlaylists )
2020-07-02 16:34:21 -04:00
viper . SetDefault ( "enabletranscodingconfig" , false )
viper . SetDefault ( "transcodingcachesize" , "100MB" )
2021-02-01 00:31:02 -05:00
viper . SetDefault ( "imagecachesize" , "100MB" )
2023-02-06 09:50:04 -05:00
viper . SetDefault ( "enableartworkprecache" , true )
2020-08-02 23:17:13 -04:00
viper . SetDefault ( "autoimportplaylists" , true )
2021-09-12 21:06:03 -04:00
viper . SetDefault ( "playlistspath" , consts . DefaultPlaylistsPath )
2020-11-10 16:14:43 -05:00
viper . SetDefault ( "enabledownloads" , true )
2021-10-30 18:00:32 -04:00
viper . SetDefault ( "enableexternalservices" , true )
2022-12-27 14:30:00 -05:00
viper . SetDefault ( "enableMediaFileCoverArt" , true )
2022-12-18 12:12:37 -05:00
viper . SetDefault ( "autotranscodedownload" , false )
2023-01-18 14:20:06 -05:00
viper . SetDefault ( "defaultdownsamplingformat" , consts . DefaultDownsamplingFormat )
2020-08-30 13:08:10 -04:00
viper . SetDefault ( "searchfullstring" , false )
2021-03-12 17:49:47 -05:00
viper . SetDefault ( "recentlyaddedbymodtime" , false )
2020-07-02 16:34:21 -04: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]([)" )
2023-01-18 14:20:06 -05:00
viper . SetDefault ( "subsonicartistparticipations" , false )
2023-02-07 13:08:25 -05:00
viper . SetDefault ( "ffmpegpath" , "" )
2023-01-18 01:22:54 +00:00
viper . SetDefault ( "coverartpriority" , "cover.*, folder.*, front.*, embedded, external" )
2020-07-02 16:34:21 -04:00
viper . SetDefault ( "coverjpegquality" , 75 )
2023-03-31 00:28:05 +02:00
viper . SetDefault ( "artistartpriority" , "artist.*, album/artist.*, external" )
2020-11-13 00:40:01 -05:00
viper . SetDefault ( "enablegravatar" , false )
2021-03-29 06:05:49 +05:30
viper . SetDefault ( "enablefavourites" , true )
2021-04-08 01:32:52 +05:30
viper . SetDefault ( "enablestarrating" , true )
2021-05-02 17:10:56 -04:00
viper . SetDefault ( "enableuserediting" , true )
2021-04-18 13:51:00 -04:00
viper . SetDefault ( "defaulttheme" , "Dark" )
2022-09-27 19:16:10 -04:00
viper . SetDefault ( "defaultlanguage" , "" )
2022-11-11 16:31:28 -05:00
viper . SetDefault ( "defaultuivolume" , consts . DefaultUIVolume )
2023-02-09 17:45:38 -05:00
viper . SetDefault ( "enablereplaygain" , true )
2021-06-28 17:10:58 -04:00
viper . SetDefault ( "enablecoveranimation" , true )
2020-07-03 12:53:35 -04:00
viper . SetDefault ( "gatrackingid" , "" )
2021-05-02 16:49:20 -04:00
viper . SetDefault ( "enablelogredacting" , true )
2020-07-19 14:45:05 -04:00
viper . SetDefault ( "authrequestlimit" , 5 )
viper . SetDefault ( "authwindowlength" , 20 * time . Second )
2021-06-18 18:38:38 -04:00
viper . SetDefault ( "passwordencryptionkey" , "" )
2020-07-02 16:34:21 -04:00
2021-06-12 03:17:21 +00:00
viper . SetDefault ( "reverseproxyuserheader" , "Remote-User" )
2021-06-13 12:46:36 -04:00
viper . SetDefault ( "reverseproxywhitelist" , "" )
2021-06-12 03:17:21 +00:00
2022-10-03 02:59:53 +03:00
viper . SetDefault ( "prometheus.enabled" , false )
viper . SetDefault ( "prometheus.metricspath" , "/metrics" )
2021-08-28 19:35:54 -04:00
viper . SetDefault ( "scanner.extractor" , consts . DefaultScannerExtractor )
2021-07-16 11:03:28 -04:00
viper . SetDefault ( "scanner.genreseparators" , ";/," )
2021-07-15 19:53:40 -04:00
2021-02-07 16:46:15 -05:00
viper . SetDefault ( "agents" , "lastfm,spotify" )
2021-05-27 16:14:24 -04:00
viper . SetDefault ( "lastfm.enabled" , true )
2020-10-17 23:59:09 -04:00
viper . SetDefault ( "lastfm.language" , "en" )
2021-06-21 17:09:34 -04:00
viper . SetDefault ( "lastfm.apikey" , consts . LastFMAPIKey )
viper . SetDefault ( "lastfm.secret" , consts . LastFMAPISecret )
2020-10-22 08:31:47 -04:00
viper . SetDefault ( "spotify.id" , "" )
viper . SetDefault ( "spotify.secret" , "" )
2021-11-17 21:11:53 -05:00
viper . SetDefault ( "listenbrainz.enabled" , true )
2022-09-27 21:06:28 -04:00
viper . SetDefault ( "listenbrainz.baseurl" , "https://api.listenbrainz.org/1/" )
2020-09-05 20:28:27 -04:00
2020-07-02 16:34:21 -04:00
// DevFlags. These are used to enable/disable debugging and incomplete features
viper . SetDefault ( "devlogsourceline" , false )
2023-03-15 10:56:13 -04:00
viper . SetDefault ( "devenableprofiler" , false )
2020-07-02 16:34:21 -04:00
viper . SetDefault ( "devautocreateadminpassword" , "" )
2021-06-19 10:56:39 -04:00
viper . SetDefault ( "devautologinusername" , "" )
2021-02-01 00:31:02 -05:00
viper . SetDefault ( "devactivitypanel" , true )
2023-01-29 20:33:10 -05:00
viper . SetDefault ( "enablesharing" , false )
2023-03-10 23:01:03 -05:00
viper . SetDefault ( "defaultdownloadableshare" , false )
2021-07-19 16:10:37 -04:00
viper . SetDefault ( "devenablebufferedscrobble" , true )
2021-10-02 13:56:42 -04:00
viper . SetDefault ( "devsidebarplaylists" , true )
2021-10-17 11:07:59 -04:00
viper . SetDefault ( "devshowartistpage" , true )
2023-02-02 13:59:04 -05:00
viper . SetDefault ( "devartworkmaxrequests" , number . Max ( 2 , runtime . NumCPU ( ) / 3 ) )
2023-01-13 18:10:32 -05:00
viper . SetDefault ( "devartworkthrottlebackloglimit" , consts . RequestThrottleBacklogLimit )
viper . SetDefault ( "devartworkthrottlebacklogtimeout" , consts . RequestThrottleBacklogTimeout )
2023-02-02 16:55:12 -05:00
viper . SetDefault ( "devartistinfotimetolive" , consts . ArtistInfoTimeToLive )
viper . SetDefault ( "devalbuminfotimetolive" , consts . AlbumInfoTimeToLive )
2016-03-30 00:05:57 -04:00
}
2020-07-02 17:21:25 -04:00
2020-07-03 09:39:28 -04:00
func InitConfig ( cfgFile string ) {
2020-07-20 18:36:12 -04:00
cfgFile = getConfigFile ( cfgFile )
2020-07-02 17:21:25 -04: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-02 18:17:31 -04:00
_ = viper . BindEnv ( "port" )
2020-07-02 17:21:25 -04:00
viper . SetEnvPrefix ( "ND" )
2020-09-05 20:28:27 -04:00
replacer := strings . NewReplacer ( "." , "_" )
viper . SetEnvKeyReplacer ( replacer )
2020-07-02 17:21:25 -04:00
viper . AutomaticEnv ( )
2020-07-03 10:19:44 -04:00
err := viper . ReadInConfig ( )
2021-07-20 22:32:36 -04:00
if viper . ConfigFileUsed ( ) != "" && err != nil {
2022-12-21 14:37:08 -05:00
_ , _ = fmt . Fprintln ( os . Stderr , "FATAL: Navidrome could not open config file: " , err )
2023-02-06 13:00:07 -05:00
os . Exit ( 1 )
2020-07-03 10:19:44 -04:00
}
2020-07-02 17:21:25 -04:00
}
2020-07-20 18:36:12 -04:00
func getConfigFile ( cfgFile string ) string {
if cfgFile != "" {
return cfgFile
}
return os . Getenv ( "ND_CONFIGFILE" )
}