feat: first

This commit is contained in:
2025-08-13 19:36:09 +02:00
parent fd2ba2e999
commit 5fc4d1d997
17 changed files with 2622 additions and 111 deletions

100
internal/config/config.go Normal file
View File

@@ -0,0 +1,100 @@
package config
import (
"fmt"
"os"
"strconv"
"strings"
)
// Config holds all configuration for the application
type Config struct {
Server ServerConfig
Log LogConfig
Metrics MetricsConfig
}
// ServerConfig holds server-related configuration
type ServerConfig struct {
GRPCPort string
HTTPPort string
}
// LogConfig holds logging configuration
type LogConfig struct {
Level string
Format string
}
// MetricsConfig holds metrics configuration
type MetricsConfig struct {
Enabled bool
}
// Load loads configuration from environment variables with defaults
func Load() *Config {
return &Config{
Server: ServerConfig{
GRPCPort: getEnv("SERVER_GRPC_PORT", "8080"),
HTTPPort: getEnv("SERVER_HTTP_PORT", "8090"),
},
Log: LogConfig{
Level: strings.ToLower(getEnv("LOG_LEVEL", "info")),
Format: strings.ToLower(getEnv("LOG_FORMAT", "json")),
},
Metrics: MetricsConfig{
Enabled: getEnvBool("METRICS_ENABLED", true),
},
}
}
// Validate validates the configuration
func (c *Config) Validate() error {
if c.Server.GRPCPort == "" {
return fmt.Errorf("gRPC port cannot be empty")
}
if c.Server.HTTPPort == "" {
return fmt.Errorf("HTTP port cannot be empty")
}
if c.Server.GRPCPort == c.Server.HTTPPort {
return fmt.Errorf("gRPC and HTTP ports cannot be the same")
}
validLogLevels := map[string]bool{
"debug": true,
"info": true,
"warn": true,
"error": true,
}
if !validLogLevels[c.Log.Level] {
return fmt.Errorf("invalid log level: %s", c.Log.Level)
}
validLogFormats := map[string]bool{
"json": true,
"text": true,
}
if !validLogFormats[c.Log.Format] {
return fmt.Errorf("invalid log format: %s", c.Log.Format)
}
return nil
}
// getEnv gets an environment variable or returns a default value
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
// getEnvBool gets a boolean environment variable or returns a default value
func getEnvBool(key string, defaultValue bool) bool {
if value := os.Getenv(key); value != "" {
if parsed, err := strconv.ParseBool(value); err == nil {
return parsed
}
}
return defaultValue
}

View File

@@ -0,0 +1,193 @@
package config
import (
"os"
"testing"
)
func TestLoad(t *testing.T) {
// Save original env vars
originalGRPCPort := os.Getenv("SERVER_GRPC_PORT")
originalHTTPPort := os.Getenv("SERVER_HTTP_PORT")
originalLogLevel := os.Getenv("LOG_LEVEL")
originalLogFormat := os.Getenv("LOG_FORMAT")
originalMetricsEnabled := os.Getenv("METRICS_ENABLED")
// Clean up after test
defer func() {
os.Setenv("SERVER_GRPC_PORT", originalGRPCPort)
os.Setenv("SERVER_HTTP_PORT", originalHTTPPort)
os.Setenv("LOG_LEVEL", originalLogLevel)
os.Setenv("LOG_FORMAT", originalLogFormat)
os.Setenv("METRICS_ENABLED", originalMetricsEnabled)
}()
tests := []struct {
name string
envVars map[string]string
expected Config
}{
{
name: "default values",
envVars: map[string]string{},
expected: Config{
Server: ServerConfig{
GRPCPort: "8080",
HTTPPort: "8090",
},
Log: LogConfig{
Level: "info",
Format: "json",
},
Metrics: MetricsConfig{
Enabled: true,
},
},
},
{
name: "custom values",
envVars: map[string]string{
"SERVER_GRPC_PORT": "9090",
"SERVER_HTTP_PORT": "9091",
"LOG_LEVEL": "debug",
"LOG_FORMAT": "text",
"METRICS_ENABLED": "false",
},
expected: Config{
Server: ServerConfig{
GRPCPort: "9090",
HTTPPort: "9091",
},
Log: LogConfig{
Level: "debug",
Format: "text",
},
Metrics: MetricsConfig{
Enabled: false,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clear all env vars first
os.Unsetenv("SERVER_GRPC_PORT")
os.Unsetenv("SERVER_HTTP_PORT")
os.Unsetenv("LOG_LEVEL")
os.Unsetenv("LOG_FORMAT")
os.Unsetenv("METRICS_ENABLED")
// Set test env vars
for key, value := range tt.envVars {
os.Setenv(key, value)
}
cfg := Load()
if cfg.Server.GRPCPort != tt.expected.Server.GRPCPort {
t.Errorf("expected GRPCPort %s, got %s", tt.expected.Server.GRPCPort, cfg.Server.GRPCPort)
}
if cfg.Server.HTTPPort != tt.expected.Server.HTTPPort {
t.Errorf("expected HTTPPort %s, got %s", tt.expected.Server.HTTPPort, cfg.Server.HTTPPort)
}
if cfg.Log.Level != tt.expected.Log.Level {
t.Errorf("expected LogLevel %s, got %s", tt.expected.Log.Level, cfg.Log.Level)
}
if cfg.Log.Format != tt.expected.Log.Format {
t.Errorf("expected LogFormat %s, got %s", tt.expected.Log.Format, cfg.Log.Format)
}
if cfg.Metrics.Enabled != tt.expected.Metrics.Enabled {
t.Errorf("expected MetricsEnabled %t, got %t", tt.expected.Metrics.Enabled, cfg.Metrics.Enabled)
}
})
}
}
func TestValidate(t *testing.T) {
tests := []struct {
name string
config Config
wantErr bool
}{
{
name: "valid config",
config: Config{
Server: ServerConfig{
GRPCPort: "8080",
HTTPPort: "8090",
},
Log: LogConfig{
Level: "info",
Format: "json",
},
},
wantErr: false,
},
{
name: "empty grpc port",
config: Config{
Server: ServerConfig{
GRPCPort: "",
HTTPPort: "8090",
},
Log: LogConfig{
Level: "info",
Format: "json",
},
},
wantErr: true,
},
{
name: "same ports",
config: Config{
Server: ServerConfig{
GRPCPort: "8080",
HTTPPort: "8080",
},
Log: LogConfig{
Level: "info",
Format: "json",
},
},
wantErr: true,
},
{
name: "invalid log level",
config: Config{
Server: ServerConfig{
GRPCPort: "8080",
HTTPPort: "8090",
},
Log: LogConfig{
Level: "invalid",
Format: "json",
},
},
wantErr: true,
},
{
name: "invalid log format",
config: Config{
Server: ServerConfig{
GRPCPort: "8080",
HTTPPort: "8090",
},
Log: LogConfig{
Level: "info",
Format: "invalid",
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
if (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}