初始化项目
This commit is contained in:
50
config/config.go
Normal file
50
config/config.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
DatabasePath string
|
||||
}
|
||||
|
||||
// LoadConfig loads configuration from environment variables
|
||||
func LoadConfig() *Config {
|
||||
cfg := &Config{
|
||||
DatabasePath: getEnv("DATABASE_PATH", "./butterfliu.db"),
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
// getEnv gets an environment variable with a default value
|
||||
func getEnv(key, defaultValue string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// getEnvAsInt gets an environment variable as integer
|
||||
func getEnvAsInt(key string, defaultValue int) int {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
if intValue, err := strconv.Atoi(value); err == nil {
|
||||
return intValue
|
||||
}
|
||||
log.Printf("Warning: Invalid integer value for %s: %s, using default: %d", key, value, defaultValue)
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// getEnvAsInt64 gets an environment variable as int64
|
||||
func getEnvAsInt64(key string, defaultValue int64) int64 {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
if intValue, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
return intValue
|
||||
}
|
||||
log.Printf("Warning: Invalid int64 value for %s: %s, using default: %d", key, value, defaultValue)
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
Reference in New Issue
Block a user