Files
vision-tool/internal/config/config.go
Geliebte f9cdeb5d64 feat: 认证简化为固定进入密钥 + 页面增强(API 文档/主题切换/版权署名)
- 移除管理员初始化/登录/JWT 流程,改为固定进入密钥(19735500),
  首次启动自动初始化到数据库 settings 表(可改库更新)
- analyze 接口支持可选密钥鉴权:X-Access-Key 请求头或 access_key 字段,
  带密钥强制校验(错误 401),不带密钥保持免鉴权
- 解锁后顶栏"复制 API 文档"按钮:以当前访问 URL 为基础地址动态生成
  Markdown 文档(免鉴权/带密钥两种 curl 示例,供智能体直接调用)
- 白昼/黑夜主题切换:CSS 变量驱动,localStorage 持久化,默认跟随系统
- 修复 hidden 属性被 display:flex 覆盖导致解锁层无法隐藏的问题
- 页面去除"免费/Agnes"文案,页脚署名青梧映星软件开发工作室
- build.bat/build.sh 简化为 Linux 交叉编译(CGO_ENABLED=0)
- 依赖精简:移除 golang-jwt/bcrypt,直接依赖仅 gin/sqlite/gorm/yaml

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-13 01:21:18 +08:00

91 lines
2.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 (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
// Config 是应用统一配置,对应 config.yaml。
type Config struct {
Server ServerConfig `yaml:"server"`
Database DatabaseConfig `yaml:"database"`
Auth AuthConfig `yaml:"auth"`
Upload UploadConfig `yaml:"upload"`
AI AIConfig `yaml:"ai"`
Env EnvConfig `yaml:"env"`
}
type ServerConfig struct {
Port int `yaml:"port"`
}
type DatabaseConfig struct {
Driver string `yaml:"driver"`
Path string `yaml:"path"`
AutoMigrate bool `yaml:"auto_migrate"`
}
type AuthConfig struct {
AccessKey string `yaml:"access_key"` // 进入密钥,首次启动初始化到数据库
}
type UploadConfig struct {
Path string `yaml:"path"`
MaxSize int64 `yaml:"max_size"`
}
type AIConfig struct {
APIKey string `yaml:"api_key"`
BaseURL string `yaml:"base_url"`
Model string `yaml:"model"`
}
type EnvConfig struct {
Name string `yaml:"name"`
}
// hardcodedAPIKey 内置默认免费 Key(与 config.yaml 一致),
// 保证 CLI 工具在无配置文件时也能开箱即用。
const hardcodedAPIKey = "sk-68FL9xXRrJhxcY5TCGxoaFlQ94oCBioIJhyGfeDMCkCvA0SV"
// DefaultConfig 返回开箱即用的默认配置。
func DefaultConfig() *Config {
return &Config{
Server: ServerConfig{Port: 8080},
Database: DatabaseConfig{Driver: "sqlite", Path: "./data/app.db", AutoMigrate: true},
Auth: AuthConfig{AccessKey: "19735500"},
Upload: UploadConfig{Path: "uploads", MaxSize: 10 * 1024 * 1024},
AI: AIConfig{
APIKey: hardcodedAPIKey,
Model: "agnes-2.0-flash",
BaseURL: "https://apihub.agnes-ai.com/v1/chat/completions",
},
Env: EnvConfig{Name: "dev"},
}
}
// Load 从 yaml 文件加载配置;文件不存在时使用默认值(便于快速上手)。
// 环境变量覆盖:AGNES_API_KEY → ai.api_key。
func Load(path string) (*Config, error) {
cfg := DefaultConfig()
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return cfg, nil
}
return nil, fmt.Errorf("读取 %s: %w", path, err)
}
if err := yaml.Unmarshal(data, cfg); err != nil {
return nil, fmt.Errorf("解析 %s: %w", path, err)
}
// 环境变量覆盖
if key := os.Getenv("AGNES_API_KEY"); key != "" {
cfg.AI.APIKey = key
}
return cfg, nil
}