- 流式放行前的上游错误判定加廉价子串预过滤("error" / response.failed): 长思考阶段几十上百条事件不再逐条 json.Unmarshal。 - usage_logs 去掉 idx_channel_created:没有任何查询按 channel_id 过滤该表 (渠道统计走 created_at 时间窗后在内存分组),复合索引只是每行一次多余的 B-tree 插入;schema v9 迁移里显式 DROP 旧库索引。至此该表只剩 (user_id, created_at)、created_at 两个真被用到的索引。 - 新增可选保留期(OT_USAGE_RETENTION_DAYS,默认 0=永久保留):每天分批 (2000 行/批、批间 50ms)清理过期 usage_logs,避免长期运行后时间窗查询 与备份随表增长变慢;余额流水 balance_logs 不在清理范围。 参数复测(记账稳态吞吐,3 次取中位): - batch 64(当前)30.2k rows/s;batch 128 19.0k、batch 256 14.3k(更差,保持 64) - wal_autocheckpoint(4000)、cache_size(-64MB) 与当前配置无差别,不引入 验证:go vet、go test ./...、-race(usage/proxy/store)全绿;新增保留期三项测试 (只删明细不动流水、未开启不动数据、跨分批删完)。
176 lines
5.2 KiB
Go
176 lines
5.2 KiB
Go
// Package app 应用容器:装配配置、数据库、密码/加密/JWT 与记账器。
|
|
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/openteam/server/internal/channel"
|
|
"github.com/openteam/server/internal/config"
|
|
"github.com/openteam/server/internal/passkey"
|
|
"github.com/openteam/server/internal/pkg/crypto"
|
|
"github.com/openteam/server/internal/pkg/jwt"
|
|
"github.com/openteam/server/internal/pkg/ratelimit"
|
|
"github.com/openteam/server/internal/store"
|
|
"github.com/openteam/server/internal/usage"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type App struct {
|
|
Cfg *config.Config
|
|
DB *gorm.DB
|
|
Hasher *crypto.PasswordHasher
|
|
Enc *crypto.Encryptor
|
|
JWT *jwt.Manager
|
|
Usage *usage.Recorder
|
|
Health *channel.HealthMonitor
|
|
Limit *ratelimit.Limiter
|
|
Passkeys *passkey.Service
|
|
// usageRetention 用量明细保留期任务(未开启时为 no-op)
|
|
usageRetention func()
|
|
startedAt time.Time
|
|
ctx context.Context
|
|
cancel context.CancelFunc
|
|
}
|
|
|
|
func New(cfg *config.Config) (*App, error) {
|
|
db, err := store.Open(cfg.DB.Driver, cfg.DB.DSN)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
a := &App{
|
|
Cfg: cfg,
|
|
DB: db,
|
|
Hasher: crypto.NewPasswordHasher(cfg.Auth.Argon2Time, cfg.Auth.Argon2Memory, cfg.Auth.Argon2Threads, cfg.Auth.Argon2KeyLen, cfg.Auth.SaltLen),
|
|
Enc: crypto.NewEncryptor(cfg.Master),
|
|
JWT: jwt.NewManager(cfg.JWT.Secret, cfg.JWT.Issuer, cfg.JWT.AccessTTL, cfg.JWT.RefreshTTL),
|
|
startedAt: time.Now(),
|
|
}
|
|
a.ctx, a.cancel = context.WithCancel(context.Background())
|
|
a.Usage = usage.NewRecorder(db)
|
|
a.Limit = ratelimit.New()
|
|
|
|
if err := a.Seed(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
a.Health = channel.NewHealthMonitor(db, a.Enc, channel.HealthConfig{
|
|
Interval: cfg.Proxy.HealthInterval,
|
|
FailThreshold: cfg.Proxy.HealthFailThreshold,
|
|
})
|
|
a.Health.Start(a.ctx)
|
|
|
|
// 用量明细保留期(默认 0=永久保留,只清明细不动余额流水)
|
|
a.usageRetention = usage.StartRetention(a.ctx, db, cfg.Usage.RetentionDays)
|
|
|
|
a.Passkeys, err = passkey.New(db, passkey.Config{
|
|
RPID: cfg.WebAuthn.RPID, Origin: cfg.WebAuthn.RPOrigin, Name: cfg.WebAuthn.RPName,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
func (a *App) Close() {
|
|
if a.usageRetention != nil {
|
|
a.usageRetention()
|
|
}
|
|
a.Usage.Close()
|
|
}
|
|
|
|
// Seed 首次启动初始化:管理员账号 + 默认渠道 + 默认模型。
|
|
func (a *App) Seed() error {
|
|
// 1. 管理员(从环境变量读取,默认 admin/admin123,生产必须改)
|
|
var count int64
|
|
a.DB.Model(&store.User{}).Where("role = ?", store.RoleAdmin).Count(&count)
|
|
if count == 0 {
|
|
password := a.Cfg.AdminPassword
|
|
if password == "" {
|
|
// config.validate 已拒绝缺 OT_ADMIN_PASSWORD 的生产配置;这里再拦一层,
|
|
// 防止绕过 Load 的调用方(测试/工具)带着公开默认密码进非开发环境。
|
|
if a.Cfg.Env != "development" {
|
|
return fmt.Errorf("非 development 环境必须显式设置 OT_ADMIN_PASSWORD,拒绝用默认密码创建管理员")
|
|
}
|
|
password = "admin123"
|
|
log.Printf("seed: using default admin password admin123 — DO NOT expose this instance to the public internet")
|
|
}
|
|
hash, err := a.Hasher.HashPassword(password)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
admin := store.User{
|
|
Username: envOr("OT_ADMIN_USERNAME", "admin"),
|
|
Email: envOr("OT_ADMIN_EMAIL", "admin@localhost"),
|
|
PasswordHash: hash,
|
|
Role: store.RoleAdmin,
|
|
Balance: 1000, // 初始余额,便于联调;生产由充值/调整决定
|
|
Status: store.UserStatusActive,
|
|
}
|
|
if err := a.DB.Create(&admin).Error; err != nil {
|
|
return err
|
|
}
|
|
log.Printf("seed: created admin user %q (change the default password!)", admin.Username)
|
|
}
|
|
|
|
// 2. 默认渠道(配置了上游 key 时创建)
|
|
if a.Cfg.Proxy.UpstreamKey != "" {
|
|
var chCount int64
|
|
a.DB.Model(&store.Channel{}).Count(&chCount)
|
|
if chCount == 0 {
|
|
enc, err := a.Enc.Encrypt(a.Cfg.Proxy.UpstreamKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ch := store.Channel{
|
|
Name: a.Cfg.Proxy.DefaultChannelName,
|
|
Provider: store.ChannelProviderOpenAI,
|
|
Formats: []string{store.FormatChat, store.FormatResponses},
|
|
BaseURL: a.Cfg.Proxy.UpstreamBaseURL,
|
|
APIKeyEnc: enc,
|
|
Weight: 1,
|
|
Priority: 0,
|
|
TimeoutMS: int(a.Cfg.Proxy.Timeout / time.Millisecond),
|
|
MaxConcurrency: 16,
|
|
HealthStatus: store.ChannelHealthHealthy,
|
|
Enabled: true,
|
|
}
|
|
if err := a.DB.Create(&ch).Error; err != nil {
|
|
return err
|
|
}
|
|
// 默认模型 + 绑定
|
|
m := store.Model{
|
|
Name: a.Cfg.Proxy.DefaultModel,
|
|
InputPrice: 0.15, // 每百万 token,示例价
|
|
OutputPrice: 0.60,
|
|
Enabled: true,
|
|
}
|
|
if err := a.DB.Create(&m).Error; err == nil {
|
|
a.DB.Create(&store.ChannelModelBinding{ChannelID: ch.ID, ModelID: m.ID, UpstreamModel: m.Name})
|
|
}
|
|
log.Printf("seed: created default channel %q (%s)", ch.Name, ch.BaseURL)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *App) Shutdown(ctx context.Context) {
|
|
a.cancel()
|
|
a.Usage.Close()
|
|
if sqlDB, err := a.DB.DB(); err == nil {
|
|
_ = sqlDB.Close()
|
|
}
|
|
_ = ctx
|
|
}
|
|
|
|
func envOr(key, fallback string) string {
|
|
v := envLookup(key)
|
|
if v == "" {
|
|
return fallback
|
|
}
|
|
return v
|
|
}
|