Files
go-web-gin/env/env.go

78 lines
1.9 KiB
Go

package env
import (
"os"
"strings"
)
// RunEnv represents the runtime environment
type RunEnv string
const (
// Local environment for local development
Local RunEnv = "LOCAL"
// Development environment for development/testing
Development RunEnv = "DEVELOPMENT"
// Production environment for production use
Production RunEnv = "PRODUCTION"
// RunEnvKey is the environment variable key for run environment
RunEnvKey = "RUN_ENV"
// CfgPathKey is the environment variable key for config file path
CfgPathKey = "CFG_PATH"
// DefaultCfgPath is the default config file path
DefaultCfgPath = "config/config.yml"
)
// GetRunEnv returns the current run environment from env variable
// Defaults to Local if not set or invalid
func GetRunEnv() RunEnv {
env := os.Getenv(RunEnvKey)
switch strings.ToUpper(env) {
case string(Development):
return Development
case string(Production):
return Production
default:
return Local
}
}
// Get returns the environment variable value for the given key
// Returns empty string if not set
func Get(key string) string {
return os.Getenv(key)
}
// GetWithDefault returns the environment variable value for the given key
// Returns defaultValue if not set
func GetWithDefault(key, defaultValue string) string {
if val := os.Getenv(key); val != "" {
return val
}
return defaultValue
}
// IsLocal returns true if current environment is Local
func IsLocal() bool {
return GetRunEnv() == Local
}
// IsDevelopment returns true if current environment is Development
func IsDevelopment() bool {
return GetRunEnv() == Development
}
// IsProduction returns true if current environment is Production
func IsProduction() bool {
return GetRunEnv() == Production
}
// GetCfgPath returns the config file path from env variable
// Returns DefaultCfgPath if not set
func GetCfgPath() string {
return GetWithDefault(CfgPathKey, DefaultCfgPath)
}