69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"gorm.io/driver/postgres"
|
|
)
|
|
import (
|
|
"github.com/spf13/viper"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var DB *gorm.DB
|
|
var App AppConfig
|
|
|
|
type AppConfig struct {
|
|
DB struct {
|
|
Host string `yaml:"host"`
|
|
Port string `yaml:"port"`
|
|
User string `yaml:"user"`
|
|
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() {
|
|
viper.AddConfigPath("./")
|
|
viper.SetConfigName("config")
|
|
viper.SetConfigType("yaml")
|
|
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=" + App.DB.Host +
|
|
" user=" + App.DB.User +
|
|
" password=" + App.DB.Password +
|
|
" dbname=" + App.DB.Name +
|
|
" port=" + App.DB.Port +
|
|
" sslmode=disable"
|
|
|
|
var err error
|
|
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
|
if err != nil {
|
|
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
|
|
}
|