From 397ff3989efed6ba52c762291a3fcc1f1a306ca2 Mon Sep 17 00:00:00 2001 From: Emile Date: Mon, 21 Oct 2019 12:47:16 +0200 Subject: database connection --- src/db.go | 20 +++++++++++++++++--- 1 file 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 -- cgit 1.4.1