WeatherService/src/config/Configuration.go

51 lines
1.2 KiB
Go

package config
import (
"fmt"
"os"
"strconv"
)
type Configuration struct {
ApiKey string
Language string
Units string
Port int
JwtSecret string
}
func (config Configuration) String() string {
return fmt.Sprintf("api_key=%s language=%s, units=%s, port=%d, jwt_secret=%s", config.ApiKey, config.Language, config.Units, config.Port, config.JwtSecret)
}
//returns a new Config struct
func LoadConfiguration() *Configuration {
return &Configuration{
ApiKey: getEnv("API_KEY", ""),
Language: getEnv("LANGUAGE", "en"),
Units: getEnv("UNITS", "metric"),
Port: getEnvAsInt("PORT", 8080),
JwtSecret: getEnv("JWT_SECRET", "super secret value"),
}
}
// Simple helper function to read an environment or return a default value
func getEnv(key string, defaultVal string) string {
if value, exists := os.LookupEnv(key); exists {
return value
}
return defaultVal
}
// Simple helper function to read an environment variable into integer or return a default value
func getEnvAsInt(name string, defaultVal int) int {
valueStr := getEnv(name, "")
if value, err := strconv.Atoi(valueStr); err == nil {
return value
}
return defaultVal
}