Skip to content

Commit

Permalink
Refactor and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Jul 2, 2020
1 parent d2e4cad commit a65c9bb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 51 deletions.
42 changes: 22 additions & 20 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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(),
}
Expand All @@ -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 viper
func init() {
cobra.OnInitialize(initConfig)
Expand All @@ -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")
Expand All @@ -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)
}
Expand Down
25 changes: 0 additions & 25 deletions cmd/start.go

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
25 changes: 20 additions & 5 deletions conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/spf13/viper"
)

type nd struct {
type configOptions struct {
ConfigFile string
Port int
MusicFolder string
Expand All @@ -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()
}
Expand All @@ -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")
Expand All @@ -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()
}
1 change: 1 addition & 0 deletions tests/navidrome-test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a65c9bb

Please sign in to comment.