diff options
author | Emile <hanemile@protonmail.com> | 2019-10-21 12:47:16 +0200 |
---|---|---|
committer | Emile <hanemile@protonmail.com> | 2019-10-21 12:47:16 +0200 |
commit | 397ff3989efed6ba52c762291a3fcc1f1a306ca2 (patch) | |
tree | bf37f7eade3aa99cfdc3bce8de14f3dbf18b471a | |
parent | 1d4c70d2ecb7150f07aa7dfd805ffe1e6b1ed448 (diff) |
database connection
-rw-r--r-- | src/db.go | 20 |
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 |