🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Refactor and clean up
Deluana65c9bbb1636
diff --git a/cmd/root.go b/cmd/root.goindex 6bb69661d..153fba658 100644--- a/cmd/root.go+++ b/cmd/root.go@@ -6,6 +6,7 @@ import ("github.com/deluan/navidrome/conf""github.com/deluan/navidrome/consts"+ "github.com/deluan/navidrome/db""github.com/spf13/cobra""github.com/spf13/viper")@@ -19,7 +20,7 @@ var (Long: `Navidrome is a self-hosted music server and streamer.Complete documentation is available at https://www.navidrome.org/docs`,Run: func(cmd *cobra.Command, args []string) {- start()+ startServer()},Version: consts.Version(),}@@ -33,6 +34,22 @@ func Execute() {}}+func startServer() {+ println(consts.Banner())++ conf.Load()+ db.EnsureLatestVersion()++ subsonic, err := CreateSubsonicAPIRouter()+ if err != nil {+ panic(fmt.Sprintf("Could not create the Subsonic API router. Aborting! err=%v", err))+ }+ a := CreateServer(conf.Server.MusicFolder)+ a.MountRouter(consts.URLPathSubsonicAPI, subsonic)+ a.MountRouter(consts.URLPathUI, CreateAppRouter())+ a.Run(fmt.Sprintf(":%d", conf.Server.Port))+}+// TODO: Implemement some struct tags to map flags to viperfunc init() {cobra.OnInitialize(initConfig)@@ -46,9 +63,9 @@ func init() {viper.BindPFlag("datafolder", rootCmd.PersistentFlags().Lookup("datafolder"))viper.BindPFlag("loglevel", rootCmd.PersistentFlags().Lookup("loglevel"))- rootCmd.Flags().StringP("port", "p", viper.GetString("port"), "HTTP port Navidrome will use")- rootCmd.Flags().String("sessiontimeout", viper.GetString("sessiontimeout"), "how long Navidrome will wait before closing web ui idle sessions")- rootCmd.Flags().String("scaninterval", viper.GetString("scaninterval"), "how frequently to scan for changes in your music library")+ rootCmd.Flags().IntP("port", "p", viper.GetInt("port"), "HTTP port Navidrome will use")+ rootCmd.Flags().Duration("sessiontimeout", viper.GetDuration("sessiontimeout"), "how long Navidrome will wait before closing web ui idle sessions")+ rootCmd.Flags().Duration("scaninterval", viper.GetDuration("scaninterval"), "how frequently to scan for changes in your music library")rootCmd.Flags().String("baseurl", viper.GetString("baseurl"), "base URL (only the path part) to configure Navidrome behind a proxy (ex: /music)")rootCmd.Flags().String("uiloginbackgroundurl", viper.GetString("uiloginbackgroundurl"), "URL to a backaground image used in the Login page")rootCmd.Flags().Bool("enabletranscodingconfig", viper.GetBool("enabletranscodingconfig"), "enables transcoding configuration in the UI")@@ -66,22 +83,7 @@ func init() {}func initConfig() {- conf.SetDefaults()-- 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")- }-- viper.BindEnv("port")- viper.SetEnvPrefix("ND")- viper.AutomaticEnv()-- if err := viper.ReadInConfig(); err != nil {+ if err := conf.InitConfig(cfgFile); err != nil {fmt.Printf("Error loading config file '%s'. Error: %s\n", viper.ConfigFileUsed(), err)os.Exit(1)}diff --git a/cmd/start.go b/cmd/start.godeleted file mode 100644index 3d195db54..000000000--- a/cmd/start.go+++ /dev/null@@ -1,25 +0,0 @@-package cmd--import (- "fmt"-- "github.com/deluan/navidrome/conf"- "github.com/deluan/navidrome/consts"- "github.com/deluan/navidrome/db"-)--func start() {- println(consts.Banner())-- conf.Load()- db.EnsureLatestVersion()-- subsonic, err := CreateSubsonicAPIRouter()- if err != nil {- panic(fmt.Sprintf("Could not create the Subsonic API router. Aborting! err=%v", err))- }- a := CreateServer(conf.Server.MusicFolder)- a.MountRouter(consts.URLPathSubsonicAPI, subsonic)- a.MountRouter(consts.URLPathUI, CreateAppRouter())- a.Run(fmt.Sprintf(":%d", conf.Server.Port))-}diff --git a/cmd/version.go b/cmd/version.goindex dab6d4034..2b75ba618 100644--- a/cmd/version.go+++ b/cmd/version.go@@ -13,7 +13,7 @@ func init() {var versionCmd = &cobra.Command{Use: "version",- Short: "Print the version number of Navidrome",+ Short: "Print Navidrome's version",Long: `All software has versions. This is Navidrome's`,Run: func(cmd *cobra.Command, args []string) {fmt.Println(consts.Version())diff --git a/conf/configuration.go b/conf/configuration.goindex 69a57471b..f2d6ebb0a 100644--- a/conf/configuration.go+++ b/conf/configuration.go@@ -11,7 +11,7 @@ import ("github.com/spf13/viper")-type nd struct {+type configOptions struct {ConfigFile stringPort intMusicFolder string@@ -36,11 +36,9 @@ type nd struct {DevAutoCreateAdminPassword string}-var Server = &nd{}+var Server = &configOptions{}func LoadFromFile(confFile string) {- // Use config file from the flag.- SetDefaults()viper.SetConfigFile(confFile)Load()}@@ -61,7 +59,7 @@ func Load() {log.Debug("Loaded configuration", "file", Server.ConfigFile, "config", fmt.Sprintf("%#v", Server))}-func SetDefaults() {+func init() {viper.SetDefault("musicfolder", "./music")viper.SetDefault("datafolder", "./")viper.SetDefault("loglevel", "info")@@ -85,3 +83,20 @@ func SetDefaults() {viper.SetDefault("devlogsourceline", false)viper.SetDefault("devautocreateadminpassword", "")}++func InitConfig(cfgFile string) error {+ 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")+ }++ viper.BindEnv("port")+ viper.SetEnvPrefix("ND")+ viper.AutomaticEnv()++ return viper.ReadInConfig()+}diff --git a/tests/navidrome-test.toml b/tests/navidrome-test.tomlindex e6b5b4f73..7196de9a4 100644--- a/tests/navidrome-test.toml+++ b/tests/navidrome-test.toml@@ -5,3 +5,4 @@ DbPath = "file::memory:?cache=shared"MusicFolder = "./tests/fixtures"DataFolder = "data/tests"DownsampleCommand = "ffmpeg -i %s -b:a %bk mp3 -"+ScanInterval=0\ No newline at end of file