changeset 31:e5a067051716

merge
author Thomas Junk <thomas.junk@intevation.de>
date Mon, 25 Jun 2018 18:06:21 +0200
parents 7ba0a77fd679 (current diff) a9d3990b083b (diff)
children 0c19dcc352f9
files
diffstat 2 files changed, 44 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/auth/opendb.go	Mon Jun 25 18:05:50 2018 +0200
+++ b/auth/opendb.go	Mon Jun 25 18:06:21 2018 +0200
@@ -2,11 +2,32 @@
 
 import (
 	"database/sql"
-	"errors"
+	"fmt"
+	"strings"
+
+	"gemma.intevation.de/gemma/config"
+
+	_ "github.com/jackc/pgx/stdlib"
 )
 
-var ErrNotImplementedYet = errors.New("Not implemented, yet!")
+const driver = "pgx"
+
+// dbQuote quotes strings to be able to contain whitespace
+// and backslashes in database DSN strings.
+var dbQuote = strings.NewReplacer(`\`, `\\`, `'`, `\'`).Replace
+
+// dbDSN creates a data source name suitable for sql.Open on
+// PostgreSQL databases.
+func dbDSN(host string, port uint, dbname, user, password string, sslmode string) string {
+	return fmt.Sprintf("host='%s' port=%d dbname='%s' user='%s' password='%s' sslmode='%s'",
+		dbQuote(host), port, dbQuote(dbname),
+		dbQuote(user), dbQuote(password), sslmode)
+}
 
 func opendb(user, password string) (*sql.DB, error) {
-	return nil, ErrNotImplementedYet
+	return sql.Open(driver, dbDSN(
+		config.Config.DBHost, config.Config.DBPort,
+		config.Config.DBName,
+		user, password,
+		config.Config.DBSSLMode))
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config/config.go	Mon Jun 25 18:06:21 2018 +0200
@@ -0,0 +1,20 @@
+package config
+
+var Config = NewConfiguration()
+
+type Configuration struct {
+	DBHost    string
+	DBPort    uint
+	DBName    string
+	DBSSLMode string
+}
+
+func NewConfiguration() *Configuration {
+	// TODO: Load from file.
+	return &Configuration{
+		DBHost:    "localhost",
+		DBPort:    5432,
+		DBName:    "gemma",
+		DBSSLMode: "require",
+	}
+}