gitblog1
This commit is contained in:
59
internal/config/config.go
Normal file
59
internal/config/config.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port string
|
||||
GitHubToken string
|
||||
GitHubOwner string
|
||||
GitHubRepo string
|
||||
CacheDuration time.Duration
|
||||
UpdateInterval time.Duration
|
||||
Theme string
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
cfg := &Config{
|
||||
Port: getEnv("PORT", "8080"),
|
||||
GitHubToken: getEnv("GITHUB_TOKEN", ""),
|
||||
GitHubOwner: getEnv("GITHUB_OWNER", ""),
|
||||
GitHubRepo: getEnv("GITHUB_REPO", ""),
|
||||
CacheDuration: getDurationEnv("CACHE_DURATION", 15*time.Minute),
|
||||
UpdateInterval: getDurationEnv("UPDATE_INTERVAL", 5*time.Minute),
|
||||
Theme: getEnv("DEFAULT_THEME", "light"),
|
||||
BaseURL: getEnv("BASE_URL", "http://localhost:8080"),
|
||||
}
|
||||
|
||||
if cfg.GitHubToken == "" {
|
||||
return nil, fmt.Errorf("GITHUB_TOKEN environment variable is required")
|
||||
}
|
||||
if cfg.GitHubOwner == "" {
|
||||
return nil, fmt.Errorf("GITHUB_OWNER environment variable is required")
|
||||
}
|
||||
if cfg.GitHubRepo == "" {
|
||||
return nil, fmt.Errorf("GITHUB_REPO environment variable is required")
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func getEnv(key, defaultValue string) string {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
return value
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
func getDurationEnv(key string, defaultValue time.Duration) time.Duration {
|
||||
if value := os.Getenv(key); value != "" {
|
||||
if duration, err := time.ParseDuration(value); err == nil {
|
||||
return duration
|
||||
}
|
||||
}
|
||||
return defaultValue
|
||||
}
|
Reference in New Issue
Block a user