Files
AI-Expert-Sidebar/internal/config/config.go
T
2026-04-01 14:09:33 +08:00

49 lines
1.2 KiB
Go

package config
import (
"fmt"
"github.com/spf13/viper"
)
type DatabaseConfig struct {
DSN string `mapstructure:"dsn"`
}
type DeepSeekConfig struct {
APIKey string `mapstructure:"api_key"`
Model string `mapstructure:"model"`
TimeoutSeconds int `mapstructure:"timeout_seconds"`
MaxTokens int `mapstructure:"max_tokens"`
}
type AppConfig struct {
Database DatabaseConfig `mapstructure:"database"`
DeepSeek DeepSeekConfig `mapstructure:"deepseek"`
}
var Global AppConfig
func Load() error {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.ai-expert-sidebar")
// Defaults
viper.SetDefault("database.dsn",
"root:password@tcp(127.0.0.1:3306)/ai_expert?charset=utf8mb4&parseTime=True&loc=Local")
viper.SetDefault("deepseek.api_key", "")
viper.SetDefault("deepseek.model", "deepseek-chat")
viper.SetDefault("deepseek.timeout_seconds", 60)
viper.SetDefault("deepseek.max_tokens", 1024)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
return fmt.Errorf("config read error: %w", err)
}
}
return viper.Unmarshal(&Global)
}