config.go (3952B)
1 package config 2 3 import ( 4 "github.com/spf13/viper" 5 "log" 6 ) 7 8 type Config struct { 9 HiveDavHost string `mapstructure:"HIVEDAV_HOST"` 10 ListenAddress string `mapstructure:"HIVEDAV_LISTEN_ADDRESS"` 11 ListenPort int `mapstructure:"HIVEDAV_LISTEN_PORT"` 12 CaldavUri string `mapstructure:"HIVEDAV_CALDAV_URI"` 13 CaldavHost string `mapstructure:"HIVEDAV_CALDAV_HOST"` 14 Calendar int `mapstructure:"HIVEDAV_CALENDAR"` 15 Horizon int `mapstructure:"HIVEDAV_HORIZON"` 16 CaldavUser string `mapstructure:"HIVEDAV_CALDAV_USER"` 17 CaldavPassword string `mapstructure:"HIVEDAV_CALDAV_PASSWORD"` 18 RefreshInterval int `mapstructure:"HIVEDAV_REFRESH_INTERVAL"` 19 SmtpHost string `mapstructure:"HIVEDAV_SMTP_HOST"` 20 SmtpPort int `mapstructure:"HIVEDAV_SMTP_PORT"` 21 SmtpStartTls bool `mapstructure:"HIVEDAV_SMTP_STARTTLS"` 22 SmtpUser string `mapstructure:"HIVEDAV_SMTP_USER"` 23 SmtpPassword string `mapstructure:"HIVEDAV_SMTP_PASSWORD"` 24 BookingSummary string `mapstructure:"HIVEDAV_BOOKING_SUMMARY"` 25 BookingLocation string `mapstructure:"HIVEDAV_BOOKING_LOCATION"` 26 BookingReminder int `mapstructure:"HIVEDAV_BOOKING_REMINDER"` 27 } 28 29 func (c *Config) LoadConfig(path string) (*Config, error) { 30 viper.AddConfigPath(path) 31 // look for config file with name "app" 32 viper.SetConfigName("app") 33 // look for config file with name "app.env" 34 viper.SetConfigType("env") 35 36 // define some defaults 37 viper.SetDefault("HIVEDAV_HOST", "hivedav.example.com") 38 viper.SetDefault("HIVEDAV_LISTEN_PORT", 3737) 39 viper.SetDefault("HIVEDAV_LISTEN_ADDRESS", "[::]") 40 viper.SetDefault("HIVEDAV_CALENDAR", 0) 41 viper.SetDefault("HIVEDAV_HORIZON", 1) 42 viper.SetDefault("HIVEDAV_REFRESH_INTERVAL", 30) 43 viper.SetDefault("HIVEDAV_BOOKING_SUMMARY", "HiveDAV Booking") 44 viper.SetDefault("HIVEDAV_BOOKING_LOCATION", "https://meet.jit.si") 45 viper.SetDefault("HIVEDAV_BOOKING_REMINDER", 15) 46 viper.SetDefault("HIVEDAV_SMTP_PORT", 587) 47 viper.SetDefault("HIVEDAV_SMTP_STARTTLS", true) 48 49 err := viper.ReadInConfig() 50 if err != nil { 51 log.Println("app.env config file not found") 52 return nil, err 53 } 54 55 err = viper.Unmarshal(c) 56 if err != nil { 57 log.Println("Error unmarshalling config") 58 return nil, err 59 } 60 61 viper.SetEnvPrefix("HIVEDAV") 62 // we either need AutomaticEnv() or BindEnv() on each var 63 viper.AutomaticEnv() 64 65 // override config file vars with vars from environment 66 if viper.IsSet("HOST") { 67 c.HiveDavHost = viper.GetString("HOST") 68 } 69 if viper.IsSet("LISTEN_ADDRESS") { 70 c.ListenAddress = viper.GetString("LISTEN_ADDRESS") 71 } 72 if viper.IsSet("LISTEN_PORT") { 73 c.ListenPort = viper.GetInt("LISTEN_PORT") 74 } 75 if viper.IsSet("CALDAV_URI") { 76 c.CaldavUri = viper.GetString("CALDAV_URI") 77 } 78 if viper.IsSet("CALDAV_HOST") { 79 c.CaldavHost = viper.GetString("CALDAV_HOST") 80 } 81 if viper.IsSet("CALENDAR") { 82 c.Calendar = viper.GetInt("CALENDAR") 83 } 84 if viper.IsSet("HORIZON") { 85 c.Horizon = viper.GetInt("HORIZON") 86 } 87 if viper.IsSet("CALDAV_USER") { 88 c.CaldavUser = viper.GetString("CALDAV_USER") 89 } 90 if viper.IsSet("CALDAV_PASSWORD") { 91 c.CaldavPassword = viper.GetString("CALDAV_PASSWORD") 92 } 93 if viper.IsSet("REFRESH_INTERVAL") { 94 c.RefreshInterval = viper.GetInt("REFRESH_INTERVAL") 95 } 96 if viper.IsSet("SMTP_HOST") { 97 c.SmtpHost = viper.GetString("SMTP_HOST") 98 } 99 if viper.IsSet("SMTP_PORT") { 100 c.SmtpPort = viper.GetInt("SMTP_PORT") 101 } 102 if viper.IsSet("SMTP_STARTTLS") { 103 c.SmtpStartTls = viper.GetBool("SMTP_STARTTLS") 104 } 105 if viper.IsSet("SMTP_USER") { 106 c.SmtpUser = viper.GetString("SMTP_USER") 107 } 108 if viper.IsSet("SMTP_PASSWORD") { 109 c.SmtpPassword = viper.GetString("SMTP_PASSWORD") 110 } 111 if viper.IsSet("BOOKING_SUMMARY") { 112 c.BookingSummary = viper.GetString("BOOKING_SUMMARY") 113 } 114 if viper.IsSet("BOOKING_LOCATION") { 115 c.BookingLocation = viper.GetString("BOOKING_LOCATION") 116 } 117 if viper.IsSet("BOOKING_REMINDER") { 118 c.BookingReminder = viper.GetInt("BOOKING_REMINDER") 119 } 120 121 return c, nil 122 }