|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "syscall" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/psmohan/configmate/pkg/configmate" |
| 12 | +) |
| 13 | + |
| 14 | +// Config defines the application configuration structure |
| 15 | +type Config struct { |
| 16 | + // Server configuration |
| 17 | + Server ServerConfig `config:"server"` |
| 18 | + |
| 19 | + // Database configuration |
| 20 | + Database DatabaseConfig `config:"database"` |
| 21 | + |
| 22 | + // Application settings |
| 23 | + AppName string `config:"app_name,required"` |
| 24 | + Debug bool `config:"debug,default=false"` |
| 25 | + Port int `config:"port,default=8080"` |
| 26 | +} |
| 27 | + |
| 28 | +// ServerConfig holds server-specific settings |
| 29 | +type ServerConfig struct { |
| 30 | + Host string `config:"host,default=localhost"` |
| 31 | + ReadTimeout int `config:"read_timeout,default=30"` |
| 32 | + WriteTimeout int `config:"write_timeout,default=30"` |
| 33 | +} |
| 34 | + |
| 35 | +// DatabaseConfig holds database connection settings |
| 36 | +type DatabaseConfig struct { |
| 37 | + URL string `config:"url,required"` |
| 38 | + MaxConnections int `config:"max_connections,default=10"` |
| 39 | + ConnMaxLifetime int `config:"conn_max_lifetime,default=300"` |
| 40 | +} |
| 41 | + |
| 42 | +func main() { |
| 43 | + // Create logger and metrics |
| 44 | + logger := configmate.NewDefaultLogger(configmate.LogLevelInfo) |
| 45 | + metrics := configmate.NewDefaultMetrics() |
| 46 | + |
| 47 | + var cfg Config |
| 48 | + |
| 49 | + // Load configuration with all features enabled |
| 50 | + loader := configmate.NewLoader( |
| 51 | + configmate.WithPrefix("APP"), |
| 52 | + configmate.WithLogger(logger), |
| 53 | + configmate.WithMetrics(metrics), |
| 54 | + configmate.WithCache(true), |
| 55 | + configmate.WithCacheTTL(5*time.Minute), |
| 56 | + configmate.WithWatch(true), |
| 57 | + configmate.WithSecureMode(true), |
| 58 | + configmate.WithOnReload(func(newCfg interface{}) { |
| 59 | + fmt.Println("🔄 Configuration reloaded!") |
| 60 | + printConfig(newCfg.(*Config)) |
| 61 | + }), |
| 62 | + ) |
| 63 | + |
| 64 | + // Load configuration |
| 65 | + err := loader.Load(&cfg, ".env", "config.yaml") |
| 66 | + if err != nil { |
| 67 | + log.Fatalf("Failed to load config: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + // Print loaded configuration |
| 71 | + fmt.Println("✅ Configuration loaded successfully!") |
| 72 | + printConfig(&cfg) |
| 73 | + |
| 74 | + // Print metrics |
| 75 | + fmt.Println("\n📊 Metrics:") |
| 76 | + stats := metrics.GetStats() |
| 77 | + fmt.Printf(" Load Count: %d\n", stats.LoadCount) |
| 78 | + fmt.Printf(" Average Duration: %v\n", stats.AverageDuration) |
| 79 | + fmt.Printf(" Cache Hits: %d\n", stats.CacheHits) |
| 80 | + fmt.Printf(" Cache Misses: %d\n", stats.CacheMisses) |
| 81 | + |
| 82 | + // Wait for interrupt signal |
| 83 | + fmt.Println("\n👀 Watching for configuration changes... (Press Ctrl+C to exit)") |
| 84 | + |
| 85 | + sigChan := make(chan os.Signal, 1) |
| 86 | + signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM) |
| 87 | + |
| 88 | + <-sigChan |
| 89 | + |
| 90 | + fmt.Println("\n👋 Shutting down...") |
| 91 | + if err := loader.Close(); err != nil { |
| 92 | + log.Printf("Error closing loader: %v", err) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func printConfig(cfg *Config) { |
| 97 | + fmt.Println("\n=== Configuration ===") |
| 98 | + fmt.Printf("App Name: %s\n", cfg.AppName) |
| 99 | + fmt.Printf("Debug Mode: %v\n", cfg.Debug) |
| 100 | + fmt.Printf("Port: %d\n", cfg.Port) |
| 101 | + fmt.Println("\nServer:") |
| 102 | + fmt.Printf(" Host: %s\n", cfg.Server.Host) |
| 103 | + fmt.Printf(" Read Timeout: %ds\n", cfg.Server.ReadTimeout) |
| 104 | + fmt.Printf(" Write Timeout: %ds\n", cfg.Server.WriteTimeout) |
| 105 | + fmt.Println("\nDatabase:") |
| 106 | + fmt.Printf(" URL: %s\n", cfg.Database.URL) |
| 107 | + fmt.Printf(" Max Connections: %d\n", cfg.Database.MaxConnections) |
| 108 | + fmt.Printf(" Conn Max Lifetime: %ds\n", cfg.Database.ConnMaxLifetime) |
| 109 | +} |
0 commit comments