diff options
| author | Bobby <[email protected]> | 2025-05-09 23:33:10 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2025-05-09 23:33:10 +0530 |
| commit | 5b14961af44330ae85ed3e3f0b8ba2e8e452efa1 (patch) | |
| tree | ab2529c02dd025fdfb7c2dec75482b9384697e27 /config/config.go | |
| parent | f2085f139cff393873d15842afe9fbff72ca5b09 (diff) | |
| download | kage-server-5b14961af44330ae85ed3e3f0b8ba2e8e452efa1.tar.xz kage-server-5b14961af44330ae85ed3e3f0b8ba2e8e452efa1.zip | |
basic HTTP server
Diffstat (limited to 'config/config.go')
| -rw-r--r-- | config/config.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..8706fe1 --- /dev/null +++ b/config/config.go @@ -0,0 +1,71 @@ +package config + +import ( + "kage/utils/logger" + "os" + "strconv" + "strings" + + "github.com/joho/godotenv" +) + +var ( + Config *ServerConfig + log = logger.NewLogger().WithPrefix("Config") +) + +func init() { + godotenv.Load() + + Config = &ServerConfig{ + Port: getEnvAsInt("PORT", 3000), + Debug: getEnvAsBool("DEBUG", false), + } + + if Config.Debug { + log.WithTimestamp().WithTimeFormat("02/01/2006 03:04:05 PM") + } + + if Config.Port == 0 { + log.Fatalf("Port is not set in the environment variables") + } + + log.Successf("Configuration loaded successfully") +} + +func getEnv(key string, defaultValue string) string { + value, exists := os.LookupEnv(key) + if !exists { + return defaultValue + } + + return strings.TrimSpace(value) +} + +func getEnvAsInt(key string, defaultValue int) int { + value := getEnv(key, "") + if value == "" { + return defaultValue + } + + intValue, err := strconv.Atoi(value) + if err != nil { + return defaultValue + } + + return intValue +} + +func getEnvAsBool(key string, defaultValue bool) bool { + value := getEnv(key, "") + if value == "" { + return defaultValue + } + + boolValue, err := strconv.ParseBool(value) + if err != nil { + return defaultValue + } + + return boolValue +} |
