package main import ( "encoding/json" "fmt" "time" _ "database/sql" _ "github.com/go-sql-driver/mysql" //_ "github.com/mattn/go-sqlite3" log "github.com/Sirupsen/logrus" "github.com/jmoiron/sqlx" ) type App struct { Offers []Angebot Shops []Shop Config *Config DB *sqlx.DB Now int64 Debug bool } type Angebot struct { Id int Name string Abv float32 Volume float32 Age int Shop int Url string Short_url string Original_price int Discounted_price int Base_price int Image_url string Spirit_type string Website string Valid_until int error_msg string error_ctx string } type Shop struct { Id int Name string Url string Short_url string Logo_url string Shipping_costs int Free_shipping string } func main() { var err error // copy global config to avoid woring with globals _own_config := _conf app := App{Config: &_own_config} // overwrite the global _conf = Config{} app.Now = time.Now().Unix() if "sqlite3" == app.Config.DBDriver { app.DB, err = sqlx.Connect(app.Config.DBDriver, app.Config.DBPath) } else { 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 { Fatal(err, "Cannot connect to database") } if err = app.DB.Ping(); err != nil { Fatal(err, "No connection to database") } defer app.DB.Close() err = app.createTables() if err != nil { Fatal(err, "Creating table failed") } err = app.insertShops() if err != nil { Fatal(err, "Inserting shops failed") } shops, err := app.getShops() if err != nil { Fatal(err, "Getting shops failed") } app.ScrapeHTML(shops) // short url err = app.post_process() if err != nil { Fatal(err, "Post processing failed") } }