Files
s01e02/internal/config/config.go
2026-03-12 02:10:57 +01:00

87 lines
2.1 KiB
Go

package config
import (
"encoding/json"
"fmt"
"os"
)
// Config represents the application configuration
type Config struct {
APIKey string `json:"api_key"`
InputFile string `json:"input_file"`
OutputDir string `json:"output_dir"`
LocationsAPI string `json:"locations_api"`
AccessLevelAPI string `json:"access_level_api"`
LocationsURL string `json:"locations_url"`
LLM LLMConfig `json:"llm"`
}
// LLMConfig contains configuration for LLM provider
type LLMConfig struct {
Provider string `json:"provider"` // "openrouter" or "lmstudio"
Model string `json:"model"`
APIKey string `json:"api_key,omitempty"` // For OpenRouter
BaseURL string `json:"base_url,omitempty"` // For LM Studio
}
// Load loads configuration from a JSON file
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading config file: %w", err)
}
var cfg Config
if err := json.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parsing config file: %w", err)
}
return &cfg, nil
}
// Validate validates the configuration
func (c *Config) Validate() error {
if c.APIKey == "" {
return fmt.Errorf("api_key is required")
}
if c.InputFile == "" {
return fmt.Errorf("input_file is required")
}
if c.OutputDir == "" {
return fmt.Errorf("output_dir is required")
}
if c.LocationsAPI == "" {
return fmt.Errorf("locations_api is required")
}
if c.AccessLevelAPI == "" {
return fmt.Errorf("access_level_api is required")
}
if c.LocationsURL == "" {
return fmt.Errorf("locations_url is required")
}
if c.LLM.Provider != "openrouter" && c.LLM.Provider != "lmstudio" {
return fmt.Errorf("llm.provider must be 'openrouter' or 'lmstudio'")
}
if c.LLM.Model == "" {
return fmt.Errorf("llm.model is required")
}
if c.LLM.Provider == "openrouter" && c.LLM.APIKey == "" {
return fmt.Errorf("llm.api_key is required for openrouter provider")
}
if c.LLM.Provider == "lmstudio" && c.LLM.BaseURL == "" {
return fmt.Errorf("llm.base_url is required for lmstudio provider")
}
return nil
}