view config/config.go @ 195:5dc8e734487a

Introduce database schemas as privilege-based namespaces Some privileges changed (e.g. for responsibility_areas), but additional privileges were not left off intentionally before. Search path settings have been replaced by schema-qualifying names in statements to prevent object definitions from being dependend on search path settings.
author Tom Gottfried <tom@intevation.de>
date Fri, 20 Jul 2018 17:28:16 +0200
parents 3457a60fb12d
children 11d1a488b08f
line wrap: on
line source

package config

import (
	"log"
	"os"
	"strconv"
)

var Config = NewConfiguration()

type Configuration struct {
	DBHost    string
	DBPort    uint
	DBName    string
	DBSSLMode string

	SessionStore string
}

func NewConfiguration() *Configuration {
	// TODO: Load from file.
	return &Configuration{
		DBHost:       envString("GEMMA_DB_HOST", "localhost"),
		DBPort:       envUint("GEMMA_DB_PORT", 5432),
		DBName:       envString("GEMMA_DB_NAME", "gemma"),
		DBSSLMode:    envString("GEMMA_DB_SSL_MODE", "require"),
		SessionStore: envString("GEMMA_SESSION_STORE", ""),
	}
}

func envString(key, def string) string {
	if v, ok := os.LookupEnv(key); ok {
		return v
	}
	return def
}

func envUint(key string, def uint) uint {
	if v, ok := os.LookupEnv(key); ok {
		x, err := strconv.ParseUint(v, 10, 64)
		if err != nil {
			log.Printf("warn: invalid uint env %s: %v\n", key, err)
			return def
		}
		return uint(x)
	}
	return def
}