This commit is contained in:
Dev
2025-09-15 04:02:11 +03:00
commit fc86288f06
24 changed files with 2938 additions and 0 deletions

59
internal/config/config.go Normal file
View 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
}

View File

@@ -0,0 +1,82 @@
package config
import (
"os"
"testing"
"time"
)
func TestLoad(t *testing.T) {
// Set test environment variables
os.Setenv("GITHUB_TOKEN", "test-token")
os.Setenv("GITHUB_OWNER", "test-owner")
os.Setenv("GITHUB_REPO", "test-repo")
os.Setenv("PORT", "9090")
os.Setenv("CACHE_DURATION", "30m")
os.Setenv("UPDATE_INTERVAL", "10m")
os.Setenv("DEFAULT_THEME", "dark")
os.Setenv("BASE_URL", "https://example.com")
cfg, err := Load()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if cfg.GitHubToken != "test-token" {
t.Errorf("Expected GitHubToken to be 'test-token', got %s", cfg.GitHubToken)
}
if cfg.Port != "9090" {
t.Errorf("Expected Port to be '9090', got %s", cfg.Port)
}
if cfg.CacheDuration != 30*time.Minute {
t.Errorf("Expected CacheDuration to be 30m, got %v", cfg.CacheDuration)
}
if cfg.Theme != "dark" {
t.Errorf("Expected Theme to be 'dark', got %s", cfg.Theme)
}
if cfg.BaseURL != "https://example.com" {
t.Errorf("Expected BaseURL to be 'https://example.com', got %s", cfg.BaseURL)
}
}
func TestLoadMissingRequiredVars(t *testing.T) {
// Clear environment variables
os.Unsetenv("GITHUB_TOKEN")
os.Unsetenv("GITHUB_OWNER")
os.Unsetenv("GITHUB_REPO")
_, err := Load()
if err == nil {
t.Fatal("Expected error for missing required variables")
}
}
func TestGetEnv(t *testing.T) {
os.Setenv("TEST_VAR", "test-value")
defer os.Unsetenv("TEST_VAR")
if got := getEnv("TEST_VAR", "default"); got != "test-value" {
t.Errorf("Expected 'test-value', got %s", got)
}
if got := getEnv("NONEXISTENT_VAR", "default"); got != "default" {
t.Errorf("Expected 'default', got %s", got)
}
}
func TestGetDurationEnv(t *testing.T) {
os.Setenv("TEST_DURATION", "5m")
defer os.Unsetenv("TEST_DURATION")
if got := getDurationEnv("TEST_DURATION", 1*time.Minute); got != 5*time.Minute {
t.Errorf("Expected 5m, got %v", got)
}
if got := getDurationEnv("INVALID_DURATION", 1*time.Minute); got != 1*time.Minute {
t.Errorf("Expected 1m, got %v", got)
}
}