view config/config.go @ 212:229f385448fa

Make map extent mandatory The extent of the countries responsibility area seems a reasonable default.
author Tom Gottfried <tom@intevation.de>
date Mon, 23 Jul 2018 17:41:31 +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
}