summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config.go97
-rw-r--r--src/main.go46
2 files changed, 143 insertions, 0 deletions
diff --git a/src/config.go b/src/config.go
new file mode 100644
index 0000000..41ea55a
--- /dev/null
+++ b/src/config.go
@@ -0,0 +1,97 @@
+package main
+
+import (
+ "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
+
+ Debug bool // sets log level to 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()
+
+ // Reads the config
+ err := viper.ReadInConfig()
+ if err != nil {
+ log.Fatal(err, "Config: Error parsing config file.")
+ }
+ log.Debug("Config: Config file used: " + viper.ConfigFileUsed())
+
+ c.setsConfig()
+}
+
+// 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.AccessToken= viper.GetString("AccessToken")
+
+ c.Debug = viper.GetBool("Debug")
+}
diff --git a/src/main.go b/src/main.go
new file mode 100644
index 0000000..8596cd1
--- /dev/null
+++ b/src/main.go
@@ -0,0 +1,46 @@
+package main
+
+import (
+ "encoding/json"
+ "fmt"
+ "html"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+ "regexp"
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/PuerkitoBio/goquery"
+ "github.com/anikhasibul/queue"
+ "github.com/jmoiron/sqlx"
+ log "github.com/sirupsen/logrus"
+)
+
+type App struct {
+ Config *Config
+ DB *sqlx.DB
+ Now time.Time
+}
+
+func main() {
+ var err error
+ _own_conf := _conf
+ app := App{Config: &_own_conf}
+ _conf = Config{}
+
+ 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))
+
+ app.DB, err = sqlx.Connect(app.Config.DBDriver, app.Config.DBUser+":"+app.Config.DBPassword+"@tcp("+app.Config.DBHost+":"+app.Config.DBPort+")/"+app.Config.DBDBName+"?"+app.Config.DBOptions)
+ if err != nil {
+ log.Fatal(err, "Cannot connect to database")
+ }
+
+ if err = app.DB.Ping(); err != nil {
+ log.Fatal(err, "No connection to database")
+ }
+ defer app.DB.Close()
+}