新增功能: 1. AI智能评分系统 - 集成OpenAI兼容API进行简答题评分 - 提供分数、评语和改进建议 - 支持自定义AI服务配置(BaseURL、APIKey、Model) 2. 题目列表页面 - 展示所有题目和答案 - Tab标签页形式的题型筛选(选择题、多选题、判断题、填空题、简答题) - 关键词搜索功能(支持题目内容和编号搜索) - 填空题特殊渲染:****显示为下划线 - 判断题不显示选项,界面更简洁 3. UI优化 - 答题结果组件重构,支持AI评分显示 - 首页新增"题目列表"快速入口 - 响应式设计,适配移动端和PC端 技术改进: - 添加AI评分服务层(internal/services/ai_grading.go) - 扩展题目模型支持AI评分结果 - 更新配置管理支持AI服务配置 - 添加AI评分测试脚本和文档 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
91 lines
2.0 KiB
Go
91 lines
2.0 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 {
|
|
BaseURL string
|
|
APIKey string
|
|
Model string
|
|
}
|
|
|
|
// 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 {
|
|
baseURL := getEnv("AI_BASE_URL", "https://ai.yuchat.top")
|
|
apiKey := getEnv("AI_API_KEY", "sk-OKBmOpJx855juSOPU14cWG6Iz87tZQuv3Xg9PiaJYXdHoKcN")
|
|
model := getEnv("AI_MODEL", "deepseek-v3")
|
|
|
|
return &AIConfig{
|
|
BaseURL: baseURL,
|
|
APIKey: apiKey,
|
|
Model: model,
|
|
}
|
|
}
|