Loads our config files.
()
| 49 | |
| 50 | // Loads our config files. |
| 51 | func LoadConfig() (*Config, error) { |
| 52 | viper.SetConfigName("config") // Name of the config file (without extension) |
| 53 | viper.SetConfigType("yaml") // File type |
| 54 | viper.AddConfigPath(".") // Look for config in the current directory |
| 55 | viper.AddConfigPath("/config/") // Path for Docker setups |
| 56 | |
| 57 | // Read environment variables with a specific prefix |
| 58 | viper.SetEnvPrefix("TWITTER_BRIDGE") |
| 59 | |
| 60 | // Set default values |
| 61 | viper.SetDefault("VERSION", "1.0.6") // wait till i forget to update this |
| 62 | viper.SetDefault("SERVER_PORT", "3000") |
| 63 | viper.SetDefault("DEVELOPER_MODE", false) |
| 64 | viper.SetDefault("DATABASE_TYPE", "sqlite") |
| 65 | viper.SetDefault("DATABASE_PATH", "./db/twitterbridge.db") |
| 66 | viper.SetDefault("TRACK_ANALYTICS", true) |
| 67 | viper.SetDefault("CDN_URL", "http://127.0.0.1:3000") |
| 68 | viper.SetDefault("USE_X_FORWARDED_FOR", false) |
| 69 | viper.SetDefault("IMG_DISPLAY_TEXT", "pic.twitter.com/{shortblob}") |
| 70 | viper.SetDefault("VID_DISPLAY_TEXT", "pic.twitter.com/{shortblob}") |
| 71 | viper.SetDefault("IMG_URL_TEXT", "http://127.0.0.1:3000/img/{shortblob}") |
| 72 | viper.SetDefault("VID_URL_TEXT", "http://127.0.0.1:3000/img/{shortblob}") |
| 73 | viper.SetDefault("SECRET_KEY", "") |
| 74 | viper.SetDefault("MIN_TOKEN_VERSION", 1) |
| 75 | |
| 76 | // Read config file |
| 77 | if err := viper.ReadInConfig(); err != nil { |
| 78 | fmt.Println("No config file found, relying on environment variables") |
| 79 | } |
| 80 | |
| 81 | // Bind config to struct |
| 82 | var config Config |
| 83 | if err := viper.Unmarshal(&config); err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | |
| 87 | config.SecretKeyBytes = []byte(config.SecretKey) |
| 88 | |
| 89 | return &config, nil |
| 90 | } |