Changed the init to Setup() for better control over DB creation

pull/24/head
Jordan 2014-03-24 22:38:59 -05:00
parent 584d7dbc23
commit 92af237258
3 changed files with 9 additions and 7 deletions

View File

@ -37,10 +37,11 @@ import (
func main() { func main() {
//Setup the global variables and settings //Setup the global variables and settings
err := models.Setup()
defer models.DB.Close() defer models.DB.Close()
/* if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
}*/ }
fmt.Printf("Gophish server started at http://%s\n", config.Conf.URL) fmt.Printf("Gophish server started at http://%s\n", config.Conf.URL)
http.Handle("/", controllers.Use(controllers.CreateRouter().ServeHTTP, middleware.GetContext)) http.Handle("/", controllers.Use(controllers.CreateRouter().ServeHTTP, middleware.GetContext))
http.ListenAndServe(config.Conf.URL, nil) http.ListenAndServe(config.Conf.URL, nil)

View File

@ -1,12 +1,13 @@
package main package main
import ( import (
"github.com/jordan-wright/gophish/db"
"testing" "testing"
"github.com/jordan-wright/gophish/models"
) )
func TestDBSetup(t *testing.T) { func TestDBSetup(t *testing.T) {
err := db.Setup() err := models.Setup()
if err != nil { if err != nil {
t.Fatalf("Failed creating database: %v", err) t.Fatalf("Failed creating database: %v", err)
} }

View File

@ -19,7 +19,7 @@ var Logger = log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
// Setup initializes the Conn object // Setup initializes the Conn object
// It also populates the Gophish Config object // It also populates the Gophish Config object
func init() { func Setup() error {
DB, err := sql.Open("sqlite3", config.Conf.DBPath) DB, err := sql.Open("sqlite3", config.Conf.DBPath)
Conn = &gorp.DbMap{Db: DB, Dialect: gorp.SqliteDialect{}} Conn = &gorp.DbMap{Db: DB, Dialect: gorp.SqliteDialect{}}
//If the file already exists, delete it and recreate it //If the file already exists, delete it and recreate it
@ -50,7 +50,7 @@ func init() {
for _, stmt := range createTablesSQL { for _, stmt := range createTablesSQL {
_, err = DB.Exec(stmt) _, err = DB.Exec(stmt)
if err != nil { if err != nil {
/* return nil, err*/ return err
} }
} }
//Create the default user //Create the default user
@ -64,7 +64,7 @@ func init() {
Logger.Println(err) Logger.Println(err)
} }
} }
/* return Conn, nil*/ return nil
} }
// Flash is used to hold flash information for use in templates. // Flash is used to hold flash information for use in templates.