summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.go116
-rw-r--r--src/config.json.example17
-rw-r--r--src/go.mod3
-rw-r--r--src/main.go9
4 files changed, 57 insertions, 88 deletions
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
}
diff --git a/src/config.json.example b/src/config.json.example
new file mode 100644
index 0000000..125484b
--- /dev/null
+++ b/src/config.json.example
@@ -0,0 +1,17 @@
+{
+ "db_driver": "",
+ "db_dbname": "",
+ "db_host": "",
+ "db_port": "",
+ "db_user": "",
+ "db_password": "",
+ "db_options": "",
+
+ "user_agent": "",
+ "delay": 0,
+
+ "access_token": "",
+ "refresh_token": "",
+
+ "debug": false
+}
diff --git a/src/go.mod b/src/go.mod
new file mode 100644
index 0000000..4a23ad9
--- /dev/null
+++ b/src/go.mod
@@ -0,0 +1,3 @@
+module hnimdbbot
+
+go 1.26.4
diff --git a/src/main.go b/src/main.go
index 8596cd1..ebfbd98 100644
--- a/src/main.go
+++ b/src/main.go
@@ -25,11 +25,12 @@ type App struct {
}
func main() {
- var err error
- _own_conf := _conf
- app := App{Config: &_own_conf}
- _conf = Config{}
+ cfg, err := LoadConfig("config.json")
+ if err != nil {
+ log.Fatalf("failed to load config: %v", err)
+ }
+ app := App{Config: cfg}
app.Now = time.Now()
log.Debug(fmt.Sprintf(`Connecting to "%s" database "%s" as user "%s" on host "%s:%s" with extra options "%s".`, app.Config.DBDriver, app.Config.DBDBName, app.Config.DBUser, app.Config.DBHost, app.Config.DBPort, app.Config.DBOptions))