Files
pj0235-eai_agentplatform/eai_agentplatform/backend-go/internal/config/config.go
T
eaiadminandClaude Code 14f303459e refactor: 后端仓库层收口(A1:课程/产品/素材)+ 收进工作区既有对象化重构
本提交含两部分。第一部分是本轮工作;第二部分是此前一直留在工作区、
从未提交的对象化重构,与第一部分在文件上互相咬合(internal/repository
整个包都是未跟踪状态,且 api 层已有文件引用它),无法拆成两个可编译的提交。

一、仓库层收口 A1 批(本轮工作)

把 api 层手写的 store.DB 查询收进具名仓库方法,只给真正获益的对象做方法,
不机械包裹全量。本批迁移 22 处裸查询(courses.go 9 / media.go 12 / products.go 1),
新增方法:

- MediaFileRepo.ListByBind / ListForAudit / MarkExtracted
- KnowledgeChunkRepo.CountByMediaFile
- ProductRepo.GetVisibleByID

两条业务口径改由仓库单点持有,避免各处手写漂移:
「只有 approved 素材出现在课程详情」与「已停用产品不在课程详情露出」。

修掉两个真实缺陷:
- ProductRepo.GetByID 缺 Where 条件。此前 GET /api/products/{id} 对任意 id 都返回
  第一条产品、对不存在的 id 返回 200,且 PUT /api/products/{id} 会覆盖第一条产品
  —— 数据损坏级。全仓扫描确认这是唯一一处同型写法。
- ProductRepo.Delete 写 status="deleted",而 DELETE 处理器文档与回包都声称
  "inactive",接口在说谎;管理员用 status=all 拉列表会看到前端不认识的状态。
  已对齐为 inactive(与 CourseRepo.Delete 一致)。

删除 8 个零调用且列名不存在的死方法(一调即 SQL 报错):
- media_file 上的 file_path / file_type / approval_status 三列并不存在,
  GetByPath / ListByType / UpdateStatus 全废
- knowledge_chunk 上的 space_id 列不存在(模型早已改为 knowledge_space_key),
  List / Total / ListBySpaceIDs / DeleteBySpace / SearchByVector 全废
取舍边界:能对当前 schema 跑通的死方法保留,跑不通的删或修。

CourseRepo.List 补齐 status=all 档(此前传给它会当作 status='all' 过滤出空列表)。
该方法此前零调用,现与产品列表语义对齐。

验证:go build ./... 与 go test ./... 全绿;另用真实 HTTP 请求验证 34 项
(课程 17 / 产品 3 / 素材 14),跑在数据库副本与独立 KB_DATA_DIR 上,
含 multipart 真上传 → 审批 → pdftotext 提取 → 分片入库的完整链路。

二、此前未提交的对象化重构(非本轮工作)

- 新增 internal/repository 仓库层、connectors、skills、specialists、xapps、jsonutil,
  model/task_record|task_run|task_artifact、api/task_runtime|action_definition|chat_message
- 删除 api/app_definition、connectors、my_app_center、notification、office_skill、
  export_docx|pptx|xlsx、official_account_* 等,随 XApp/Skill/Specialist/Connector
  可插拔打包方向(AR10/AR11)调整
- 资产目录归位:backend-go/knowledge_source → assets/knowledge/source、
  training_materials → assets/training/materials;README 内相对路径同步加深两级;
  deploy env 补 ASSET_ROOT_DIR 并改 KNOWLEDGE_SOURCE_DIR / TRAINING_MATERIALS_DIR
- 前端新增 skills/ specialists/ connectors/ xapps/ 目录与对应页面

验证:前端 npm run build 通过(7.26s)。

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-09-19 01:23:51 +08:00

146 lines
4.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"os"
"path/filepath"
"strconv"
"strings"
)
// Config 全局配置,从环境变量读取(带默认值)。
// system_config 表中的值在运行时覆盖对应字段(见 service)。
type Config struct {
Port string
DBPath string // SQLite 文件路径
JWTSecret string
JWTExpireMin int
LLMBaseURL string
LLMAPIKey string
LLMModel string
EmbedModel string
AssetRootDir string
KBDataDir string
KnowledgeSourceDir string
TrainingMaterialsDir string
// 定期备份。SQLite 是单文件、又没有任何外部主从,坏一份就是全丢,
// 所以默认开着:启动补一次,之后每 BackupIntervalHours 小时一次,保留最近 N 份。
BackupEnabled bool
BackupDir string
BackupKeep int
BackupIntervalHours int
FileMaxDoc int64
FileMaxVideo int64
ChunkThreshold int64
LibreOfficeBin string
PdftotextBin string
KingdeeBaseURL string
KingdeeAccountID string
KingdeeUsername string
KingdeePassword string
KingdeeLCID int
KingdeeTimeoutSec int
}
// Load 读取环境变量构造配置。缺失的用默认值(部署时通过 .env / systemd 注入)。
func Load() *Config {
baseDir := backendBaseDir()
assetRootDir := getenv("ASSET_ROOT_DIR", filepath.Join(appRootDir(baseDir), "assets"))
return &Config{
Port: getenv("PORT", "8080"),
DBPath: getenv("DB_PATH", filepath.Join(baseDir, "data", "eai_agentplatform.db")),
JWTSecret: getenv("JWT_SECRET", "change-this-to-a-strong-secret-in-production"),
JWTExpireMin: getenvInt("JWT_EXPIRE_MINUTES", 480),
LLMBaseURL: getenv("LLM_BASE_URL", "http://127.0.0.1:11434/v1"),
LLMAPIKey: getenv("LLM_API_KEY", "ollama"),
LLMModel: getenv("LLM_MODEL", "qwen2.5:7b"),
EmbedModel: getenv("EMBED_MODEL", "bge-m3"),
AssetRootDir: assetRootDir,
KBDataDir: getenv("KB_DATA_DIR", filepath.Join(baseDir, "data", "kb_data")),
KnowledgeSourceDir: getenv("KNOWLEDGE_SOURCE_DIR", filepath.Join(assetRootDir, "knowledge", "source")),
TrainingMaterialsDir: getenv("TRAINING_MATERIALS_DIR", filepath.Join(assetRootDir, "training", "materials")),
// 备份目录必须落在 data/ 下:systemd 单元是 ProtectSystem=strict,
// 只有 data/ 可写(ReadWritePaths),换到别处会静默失败。
BackupEnabled: getenvBool("BACKUP_ENABLED", true),
BackupDir: getenv("BACKUP_DIR", filepath.Join(baseDir, "data", "backups")),
BackupKeep: getenvInt("BACKUP_KEEP", 7),
BackupIntervalHours: getenvInt("BACKUP_INTERVAL_HOURS", 24),
FileMaxDoc: getenvInt64("FILE_MAX_SIZE_DOC", 209715200),
FileMaxVideo: getenvInt64("FILE_MAX_SIZE_VIDEO", 2147483648),
ChunkThreshold: getenvInt64("CHUNK_THRESHOLD", 104857600),
LibreOfficeBin: getenv("LIBREOFFICE_BIN", "libreoffice"),
PdftotextBin: getenv("PDFTOTEXT_BIN", "pdftotext"),
KingdeeBaseURL: getenv("KINGDEE_BASE_URL", ""),
KingdeeAccountID: getenv("KINGDEE_ACCOUNT_ID", ""),
KingdeeUsername: getenv("KINGDEE_USERNAME", ""),
KingdeePassword: getenv("KINGDEE_PASSWORD", ""),
KingdeeLCID: getenvInt("KINGDEE_LCID", 2052),
KingdeeTimeoutSec: getenvInt("KINGDEE_TIMEOUT_SEC", 15),
}
}
func backendBaseDir() string {
if exe, err := os.Executable(); err == nil && exe != "" {
return filepath.Clean(filepath.Join(filepath.Dir(exe), ".."))
}
if wd, err := os.Getwd(); err == nil && wd != "" {
return wd
}
return "."
}
func appRootDir(baseDir string) string {
return filepath.Clean(filepath.Join(baseDir, ".."))
}
func getenv(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
// getenvBool 只认显式的假值,其余(含没设)都算真 —— 默认开着的东西,
// 不该因为写了个看不懂的值就悄悄关掉。
func getenvBool(key string, def bool) bool {
v := strings.ToLower(strings.TrimSpace(os.Getenv(key)))
switch v {
case "":
return def
case "0", "false", "no", "off":
return false
default:
return true
}
}
func getenvInt(key string, def int) int {
if v := os.Getenv(key); v != "" {
if n, err := strconv.Atoi(v); err == nil {
return n
}
}
return def
}
func getenvInt64(key string, def int64) int64 {
if v := os.Getenv(key); v != "" {
if n, err := strconv.ParseInt(v, 10, 64); err == nil {
return n
}
}
return def
}