Refactor and clean up

This commit is contained in:
Deluan 2020-07-02 17:21:25 -04:00
parent d2e4cade62
commit a65c9bbb16
5 changed files with 44 additions and 51 deletions

View File

@ -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 viper
func 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)
}

View File

@ -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))
}

View File

@ -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())

View File

@ -11,7 +11,7 @@ import (
"github.com/spf13/viper"
)
type nd struct {
type configOptions struct {
ConfigFile string
Port int
MusicFolder 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()
}

View File

@ -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