From 7af896fce4eac0579076aa15a3e987345dc9f9e8 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 24 Jun 2026 01:52:52 +0200 Subject: feat: switch config to JSON; add go.mod and config.json.example - Replace Viper-based config with encoding/json (config.go) - Add config.json with sensible defaults (gitignored) - Add config.json.example with empty values as reference - Initialize go module (go.mod) - Update main.go to use LoadConfig() --- src/config.go | 116 ++++++++++++++++------------------------------------------ 1 file changed, 32 insertions(+), 84 deletions(-) (limited to 'src/config.go') diff --git a/src/config.go b/src/config.go index ff7722d..76d1eca 100644 --- a/src/config.go +++ b/src/config.go @@ -1,98 +1,46 @@ package main import ( + "encoding/json" + "fmt" "os" - - log "github.com/sirupsen/logrus" - "github.com/spf13/viper" ) type Config struct { - DBDriver string - DBDBName string - DBHost string - DBPort string - DBUser string - DBPassword string - DBOptions string - - UserAgent string - Delay int - - AccessToken string - RefreshToken string - - Debug bool // sets log level to debug + // database + DBDriver string `json:"db_driver"` + DBDBName string `json:"db_dbname"` + DBHost string `json:"db_host"` + DBPort string `json:"db_port"` + DBUser string `json:"db_user"` + DBPassword string `json:"db_password"` + DBOptions string `json:"db_options"` + + // http client + UserAgent string `json:"user_agent"` + Delay int `json:"delay"` + + // auth + AccessToken string `json:"access_token"` + RefreshToken string `json:"refresh_token"` + + // misc + Debug bool `json:"debug"` } -// Parses the configuration and sets the configuration struct. -func (c *Config) parseConfig(configFile string) { - - viper.SetDefault("DB_Driver", "mysql") - viper.SetDefault("DB_DBName", "hncrawler") - viper.SetDefault("DB_Host", "localhost") - viper.SetDefault("DB_Port", "3306") - - viper.SetDefault("Debug", false) - viper.SetDefault("Delay", 0) - - viper.SetDefault("UserAgent", "pure cinema - mostdiscussed.com") - - // Name of the configuration file - viper.SetConfigName("config") - - // Where to find the config file - if configFile == "" { - viper.AddConfigPath(".") - } else { - stat, err := os.Stat(configFile) - if os.IsNotExist(err) { - // provided config file does not exist, so we add the path instead - viper.AddConfigPath(configFile) - } else if err == nil && stat.IsDir() { - // adds the path to look for the config file - viper.AddConfigPath(configFile) - } else if err == nil { - // directly sets the config file - viper.SetConfigFile(configFile) - } else { - // if we are here something went wrong - log.Warn(err, "config.go: os.Stat("+configFile+") error") - // adding the path nonetheless because it's not hurting - viper.AddConfigPath(configFile) - } - } - - // Env variables need to be prefixed with "ALKOBOTE_" - viper.SetEnvPrefix("DISCUSS_") - - // Parses automatic the matching env variables - viper.AutomaticEnv() +var _conf = Config{} - // Reads the config - err := viper.ReadInConfig() +// LoadConfig reads a JSON config file and returns a populated Config. +func LoadConfig(path string) (*Config, error) { + f, err := os.Open(path) if err != nil { - log.Fatal(err, "Config: Error parsing config file.") + return nil, fmt.Errorf("config: open %s: %w", path, err) } - log.Debug("Config: Config file used: " + viper.ConfigFileUsed()) - - c.setsConfig() -} + defer f.Close() -// Actually sets the config struct -func (c *Config) setsConfig() { - c.DBDriver = viper.GetString("DB_Driver") - c.DBHost = viper.GetString("DB_Host") - c.DBPort = viper.GetString("DB_Port") - c.DBUser = viper.GetString("DB_User") - c.DBPassword = viper.GetString("DB_Password") - c.DBDBName = viper.GetString("DB_DBName") - c.DBOptions = viper.GetString("DB_Options") - - c.UserAgent = viper.GetString("UserAgent") - c.Delay = viper.GetInt("Delay") - - c.RefreshToken= viper.GetString("RefreshToken") - - c.Debug = viper.GetBool("Debug") + c := &Config{} + if err := json.NewDecoder(f).Decode(c); err != nil { + return nil, fmt.Errorf("config: decode %s: %w", path, err) + } + return c, nil } -- cgit v1.2.3