AnCao/pkg/config/config.go
yanlongqi 4ac3243f6e 重构AI配置并修复前端类型错误
1. 删除AIConfig中未使用的属性(BaseURL、Model)
2. 修复ExamManagement页面Tag组件的size属性错误
3. 添加shared_by.nickname类型定义
4. 优化AI评分提示词,移除冗余的评分依据列表

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 22:23:00 +08:00

88 lines
2.1 KiB
Go

package config
import (
"fmt"
"os"
"strconv"
)
// DatabaseConfig 数据库配置结构
type DatabaseConfig struct {
Host string
Port int
User string
Password string
DBName string
SSLMode string
}
// AIConfig AI服务配置结构
type AIConfig struct {
APIKey string
BaiduAppID string // 百度云AppBuilder应用ID
}
// GetDatabaseConfig 获取数据库配置
// 优先使用环境变量,如果没有设置则使用默认值
func GetDatabaseConfig() *DatabaseConfig {
// 从环境变量获取配置,如果未设置则使用默认值
host := getEnv("DB_HOST", "pgsql.yuchat.top")
port := getEnvAsInt("DB_PORT", 5432)
user := getEnv("DB_USER", "postgres")
password := getEnv("DB_PASSWORD", "longqi@1314")
dbname := getEnv("DB_NAME", "ankao")
sslmode := getEnv("DB_SSLMODE", "disable")
return &DatabaseConfig{
Host: host,
Port: port,
User: user,
Password: password,
DBName: dbname,
SSLMode: sslmode,
}
}
// getEnv 获取环境变量,如果不存在则返回默认值
func getEnv(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
// getEnvAsInt 获取整型环境变量,如果不存在或转换失败则返回默认值
func getEnvAsInt(key string, defaultValue int) int {
if value := os.Getenv(key); value != "" {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}
}
return defaultValue
}
// GetDSN 返回数据库连接字符串
func (c *DatabaseConfig) GetDSN() string {
return fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%d sslmode=%s",
c.Host,
c.User,
c.Password,
c.DBName,
c.Port,
c.SSLMode,
)
}
// GetAIConfig 获取AI服务配置
// 优先使用环境变量,如果没有设置则使用默认值
func GetAIConfig() *AIConfig {
apiKey := getEnv("AI_API_KEY", "bce-v3/ALTAK-TgZ1YSBmbwNXo3BIuzNZ2/768b777896453e820a2c46f38614c8e9bf43f845")
baiduAppID := getEnv("BAIDU_APP_ID", "7b336aaf-f448-46d6-9e5f-bb9e38a1167c")
return &AIConfig{
APIKey: apiKey,
BaiduAppID: baiduAppID,
}
}