A fork of https://github.com/teal-fm/piper
at main 62 lines 1.9 kB view raw
1package config 2 3import ( 4 "log" 5 "strings" 6 7 "github.com/joho/godotenv" 8 "github.com/spf13/viper" 9) 10 11// Load initializes the configuration with viper 12func Load() { 13 if err := godotenv.Load(); err != nil { 14 log.Println("No .env file found or error loading it. Using default values and environment variables.") 15 } 16 17 viper.SetDefault("server.port", "8080") 18 viper.SetDefault("server.host", "localhost") 19 viper.SetDefault("callback.spotify", "http://localhost:8080/callback/spotify") 20 viper.SetDefault("spotify.auth_url", "https://accounts.spotify.com/authorize") 21 viper.SetDefault("spotify.token_url", "https://accounts.spotify.com/api/token") 22 viper.SetDefault("spotify.scopes", "user-read-currently-playing user-read-email") 23 viper.SetDefault("tracker.interval", 30) 24 viper.SetDefault("db.path", "./data/piper.db") 25 26 // server metadata 27 viper.SetDefault("server.root_url", "http://localhost:8080") 28 viper.SetDefault("atproto.metadata_url", "http://localhost:8080/metadata") 29 viper.SetDefault("atproto.callback_url", "/metadata") 30 31 viper.AutomaticEnv() 32 33 viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) 34 35 viper.SetConfigName("config") 36 viper.SetConfigType("yaml") 37 viper.AddConfigPath("./config") 38 viper.AddConfigPath(".") 39 40 if err := viper.ReadInConfig(); err != nil { 41 if _, ok := err.(viper.ConfigFileNotFoundError); !ok { 42 log.Fatalf("Error reading config file: %v", err) 43 } 44 log.Println("Config file not found, using default values and environment variables") 45 } else { 46 log.Println("Using config file:", viper.ConfigFileUsed()) 47 } 48 49 // check for required settings 50 requiredVars := []string{"spotify.client_id", "spotify.client_secret"} 51 missingVars := []string{} 52 53 for _, v := range requiredVars { 54 if !viper.IsSet(v) { 55 missingVars = append(missingVars, v) 56 } 57 } 58 59 if len(missingVars) > 0 { 60 log.Fatalf("Required configuration variables not set: %s", strings.Join(missingVars, ", ")) 61 } 62}