about summary refs log tree commit diff
path: root/db.go
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2019-03-24 14:51:50 +0100
committerEmile <hanemile@protonmail.com>2019-03-24 14:51:50 +0100
commitda201854e9f9f25bad1720d5368648d5bc49c480 (patch)
treeda81375a4a47e35feaedb31b91b181f2a45ae1fc /db.go
parentfd94a3102282effc9fbed8075063b97fe2c11ab6 (diff)
committing untracked files HEAD main
Diffstat (limited to 'db.go')
-rw-r--r--db.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/db.go b/db.go
new file mode 100644
index 0000000..147528b
--- /dev/null
+++ b/db.go
@@ -0,0 +1,28 @@
+package main
+
+import (
+	"database/sql"
+	"fmt"
+	"log"
+)
+
+// connectToDB returns a pointer to an sql database writing to the database
+func connectToDB() *sql.DB {
+	//connStr := fmt.Sprintf("user=%s dbname=%s sslmode=%s", DBUSER, DBNAME, DBSSLMODE)
+	connStr := fmt.Sprintf("host=%s port=%d user=%s "+
+		"password=%s dbname=%s sslmode=disable",
+		DBHOST, DBPORT, DBUSER, DBPASSWD, DBNAME)
+	db := dbConnect(connStr)
+	return db
+}
+
+// dbConnect connects to a PostgreSQL database
+func dbConnect(connStr string) *sql.DB {
+	// connect to the database
+	db, err := sql.Open("postgres", connStr)
+	if err != nil {
+		log.Fatalf("[ E ] connection: %v", err)
+	}
+
+	return db
+}