Added environment variables
This commit is contained in:
46
config/config.go
Normal file
46
config/config.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// AppConfig contains application configuration
|
||||
var AppConfig Config
|
||||
|
||||
// Load application configuration
|
||||
func Load() error {
|
||||
|
||||
// load .env file
|
||||
err := godotenv.Load()
|
||||
|
||||
if err != nil {
|
||||
log.Println(".env file not presented. Retrieving configuration from environment variables")
|
||||
}
|
||||
|
||||
AppConfig = Config{
|
||||
|
||||
Service: Service{
|
||||
// 9000 DEFAULT FOR DEV ENVIRONMENT
|
||||
Port: getEnv("NOVATECH_SERVICE_PORT", "9000"),
|
||||
Environment: getEnv("NOVATECH_SERVICE_ENVIRONMENT", "DEV"),
|
||||
},
|
||||
AdminService: Service{
|
||||
// 8080 DEFAULT FOR DEV ENVIRONMENT
|
||||
Port: getEnv("NOVATECH_ADMIN_SERVICE_PORT", "8080"),
|
||||
Environment: getEnv("NOVATECH_ADMIN_SERVICE_ENVIRONMENT", "DEV"),
|
||||
},
|
||||
Database: Database{
|
||||
UserName: mustGetEnv("NOVATECH_DATABASE_USERNAME"),
|
||||
Password: mustGetEnv("NOVATECH_DATABASE_PASSWORD"),
|
||||
DatabaseName: mustGetEnv("NOVATECH_DATABASE_NAME"),
|
||||
HostName: mustGetEnv("NOVATECH_DATABASE_ADDRESS"),
|
||||
Port: mustGetEnv("NOVATECH_DATABASE_PORT"),
|
||||
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
73
config/helpers.go
Normal file
73
config/helpers.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// getEnv returns value for give key from environment
|
||||
// if key is not present in environment it returns defaultValue
|
||||
func getEnv(key, defaultValue string) string {
|
||||
v := os.Getenv(key)
|
||||
if len(v) > 0 {
|
||||
return v
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// getEnvInt returns integer value for give key from environment
|
||||
// if key is not present in environment it returns defaultValue
|
||||
// if key cannot be parsed to integer function will panic
|
||||
func getEnvInt(key string, defaultValue int) int {
|
||||
v := os.Getenv(key)
|
||||
if len(v) == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
valInteger, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
log.Fatalf("variable `%s` cannot be parsed to INTEGER", key)
|
||||
}
|
||||
|
||||
return valInteger
|
||||
}
|
||||
|
||||
// getEnvInt returns integer value for give key from environment
|
||||
// if key is not present in environment it returns defaultValue
|
||||
// if key cannot be parsed to integer function will panic
|
||||
func getEnvBoolWithDefault(key string, defaultValue bool) bool {
|
||||
v := os.Getenv(key)
|
||||
if len(v) == 0 {
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
val, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
log.Fatalf("variable `%s` cannot be parsed to BOOL", key)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
// getEnv extracts the bool value from environment variable
|
||||
func getEnvBool(env string) bool {
|
||||
envVal := mustGetEnv(env)
|
||||
value, err := strconv.ParseBool(envVal)
|
||||
|
||||
if err != nil {
|
||||
value = false
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
// mustGetEnv returns value for give key from environment
|
||||
// if key is not present in environment function will panic
|
||||
func mustGetEnv(key string) string {
|
||||
v := os.Getenv(key)
|
||||
if len(v) == 0 {
|
||||
log.Fatalf(" variable `%s` is not present in ENVIRONMENT", key)
|
||||
}
|
||||
return v
|
||||
}
|
||||
26
config/models.go
Normal file
26
config/models.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package config
|
||||
|
||||
// Config stores application configuration
|
||||
type Config struct {
|
||||
Service Service
|
||||
AdminService Service
|
||||
Database Database
|
||||
}
|
||||
|
||||
|
||||
// Service contains configuration for service
|
||||
type Service struct {
|
||||
Port string
|
||||
Environment string
|
||||
WebPageURL string
|
||||
}
|
||||
|
||||
// Database configuration
|
||||
type Database struct {
|
||||
UserName string
|
||||
Password string
|
||||
DatabaseName string
|
||||
HostName string
|
||||
Port string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user