refactor: ai config.

This commit is contained in:
2026-04-27 15:03:43 +08:00
parent d7885c442f
commit 2d273f3c55
4 changed files with 46 additions and 16 deletions
+28 -5
View File
@@ -1,6 +1,7 @@
package config
import (
"fmt"
"gorm.io/driver/postgres"
)
import (
@@ -9,6 +10,7 @@ import (
)
var DB *gorm.DB
var App AppConfig
type AppConfig struct {
DB struct {
@@ -18,6 +20,11 @@ type AppConfig struct {
Password string `yaml:"password"`
Name string `yaml:"name"`
} `yaml:"database"`
AI struct {
BaseURL string `yaml:"base_url"`
APIKey string `yaml:"api_key"`
Model string `yaml:"model"`
} `yaml:"ai"`
}
func InitConfig() {
@@ -27,14 +34,17 @@ func InitConfig() {
if err := viper.ReadInConfig(); err != nil {
panic("Failed to read config: " + err.Error())
}
if err := viper.Unmarshal(&App); err != nil {
panic("Failed to parse config: " + err.Error())
}
}
func ConnectDB() {
dsn := "host=" + viper.GetString("database.host") +
" user=" + viper.GetString("database.user") +
" password=" + viper.GetString("database.password") +
" dbname=" + viper.GetString("database.name") +
" port=" + viper.GetString("database.port") +
dsn := "host=" + App.DB.Host +
" user=" + App.DB.User +
" password=" + App.DB.Password +
" dbname=" + App.DB.Name +
" port=" + App.DB.Port +
" sslmode=disable"
var err error
@@ -43,3 +53,16 @@ func ConnectDB() {
panic("Failed to connect database: " + err.Error())
}
}
func GetAIConfig() (baseURL, apiKey, model string, err error) {
if App.AI.BaseURL == "" {
return "", "", "", fmt.Errorf("missing config: ai.base_url")
}
if App.AI.APIKey == "" {
return "", "", "", fmt.Errorf("missing config: ai.api_key")
}
if App.AI.Model == "" {
return "", "", "", fmt.Errorf("missing config: ai.model")
}
return App.AI.BaseURL, App.AI.APIKey, App.AI.Model, nil
}