about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db.go20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/db.go b/src/db.go
index e9b127c..3ec9b71 100644
--- a/src/db.go
+++ b/src/db.go
@@ -9,14 +9,28 @@ import (
 )
 
 // setup the Database
-func setupDatabase() *sql.DB {
-	connStr := "user=postgres dbname=postgres sslmode=disable"
+func setupDatabase() (*sql.DB, error) {
+	connStr := "host=localhost port=5432 user=postgres dbname=postgres sslmode=disable"
+
+	// open a connection to the database
 	db, err := sql.Open("postgres", connStr)
 	if err != nil {
 		log.Fatal(err)
 	}
 
-	return db
+	// ping the database
+	err = db.Ping()
+	if err != nil {
+		return nil, err
+	}
+
+	// create a challenges table if it does not exist yet
+	err = dbCreateTableIfNotExist(db)
+	if err != nil {
+		log.Println(err)
+	}
+
+	return db, nil
 }
 
 // getNames gets the name of all the challenges