From 92af2372588e111c328ef6159cdd3c121b235261 Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 24 Mar 2014 22:38:59 -0500 Subject: [PATCH] Changed the init to Setup() for better control over DB creation --- gophish.go | 5 +++-- gophish_test.go | 5 +++-- models/models.go | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/gophish.go b/gophish.go index 0a69e67c..9433e5ef 100644 --- a/gophish.go +++ b/gophish.go @@ -37,10 +37,11 @@ import ( func main() { //Setup the global variables and settings + err := models.Setup() defer models.DB.Close() - /* if err != nil { + if err != nil { fmt.Println(err) - }*/ + } fmt.Printf("Gophish server started at http://%s\n", config.Conf.URL) http.Handle("/", controllers.Use(controllers.CreateRouter().ServeHTTP, middleware.GetContext)) http.ListenAndServe(config.Conf.URL, nil) diff --git a/gophish_test.go b/gophish_test.go index feb41e3c..97510b9a 100644 --- a/gophish_test.go +++ b/gophish_test.go @@ -1,12 +1,13 @@ package main import ( - "github.com/jordan-wright/gophish/db" "testing" + + "github.com/jordan-wright/gophish/models" ) func TestDBSetup(t *testing.T) { - err := db.Setup() + err := models.Setup() if err != nil { t.Fatalf("Failed creating database: %v", err) } diff --git a/models/models.go b/models/models.go index f1cd123e..daf8343d 100644 --- a/models/models.go +++ b/models/models.go @@ -19,7 +19,7 @@ var Logger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile) // Setup initializes the Conn object // It also populates the Gophish Config object -func init() { +func Setup() error { DB, err := sql.Open("sqlite3", config.Conf.DBPath) Conn = &gorp.DbMap{Db: DB, Dialect: gorp.SqliteDialect{}} //If the file already exists, delete it and recreate it @@ -50,7 +50,7 @@ func init() { for _, stmt := range createTablesSQL { _, err = DB.Exec(stmt) if err != nil { - /* return nil, err*/ + return err } } //Create the default user @@ -64,7 +64,7 @@ func init() { Logger.Println(err) } } - /* return Conn, nil*/ + return nil } // Flash is used to hold flash information for use in templates.