reface to openteam

This commit is contained in:
Sakurasan
2025-04-16 18:01:27 +08:00
parent bc223d6530
commit e7ffc9e8b9
92 changed files with 5345 additions and 1273 deletions

View File

@@ -3,37 +3,36 @@ package store
import (
"fmt"
"log"
"opencatd-open/team/consts"
"opencatd-open/team/model"
"os"
"opencatd-open/internal/model"
"opencatd-open/pkg/config"
"strings"
// "gocloud.dev/mysql"
// "gocloud.dev/postgres"
"github.com/glebarez/sqlite"
"github.com/google/wire"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
// "gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var DB *gorm.DB
var DBType consts.DBType
// var DBType consts.DBType
var IsPostgres bool
var DBSet = wire.NewSet(
InitDB,
)
func GetDB() *gorm.DB {
return DB
}
// InitDB 初始化数据库连接
func InitDB() (*gorm.DB, error) {
func InitDB(cfg *config.Config) (*gorm.DB, error) {
var db *gorm.DB
var err error
// 从环境变量获取DSN
dsn := os.Getenv("DSN")
dsn := cfg.DSN
if dsn == "" {
log.Println("No DSN provided, using SQLite as default")
@@ -43,10 +42,12 @@ func InitDB() (*gorm.DB, error) {
// 解析DSN来确定数据库类型
if strings.HasPrefix(dsn, "postgres://") {
IsPostgres = true
DBType = consts.DBTypePostgreSQL
cfg.DB_Type = "postgres"
db, err = initPostgres(dsn)
} else if strings.HasPrefix(dsn, "mysql://") {
DBType = consts.DBTypeMySQL
cfg.DB_Type = "mysql"
db, err = initMySQL(dsn)
} else {
if dsn != "" {
@@ -60,12 +61,12 @@ func InitDB() (*gorm.DB, error) {
DB = db
if IsPostgres {
err = db.AutoMigrate(&model.User{}, &model.Token{}, &model.ApiKey_PG{}, &model.Usage{}, &model.DailyUsage{})
err = db.AutoMigrate(&model.User{}, &model.Token{}, &model.ApiKey_PG{}, &model.Usage{}, &model.DailyUsage{}, &model.Passkey{})
if err != nil {
return nil, err
}
} else {
err = db.AutoMigrate(&model.User{}, &model.Token{}, &model.ApiKey{}, &model.Usage{}, &model.DailyUsage{})
err = db.AutoMigrate(&model.User{}, &model.Token{}, &model.ApiKey{}, &model.Usage{}, &model.DailyUsage{}, &model.Passkey{})
if err != nil {
return nil, err
}

67
pkg/store/gcache.go Normal file
View File

@@ -0,0 +1,67 @@
package store
import (
"errors"
"log"
"time"
"github.com/bluele/gcache"
"github.com/go-webauthn/webauthn/webauthn"
"github.com/google/uuid"
)
// WebAuthnSessionStore 使用 gcache 存储 WebAuthn 会话数据
type WebAuthnSessionStore struct {
cache gcache.Cache
}
// NewWebAuthnSessionStore 创建一个新的会话存储实例
func NewWebAuthnSessionStore() *WebAuthnSessionStore {
// 创建一个 LRU 缓存,最多存储 10000 个会话,每个会话有效期 5 分钟
gc := gcache.New(10000).
LRU().
Expiration(5 * time.Minute).
Build()
return &WebAuthnSessionStore{cache: gc}
}
// GenerateSessionID 生成唯一的会话ID
func GenerateSessionID() string {
return uuid.NewString()
}
// SaveWebauthnSession 保存 WebAuthn 会话数据
func (s *WebAuthnSessionStore) SaveWebauthnSession(sessionID string, data *webauthn.SessionData) error {
return s.cache.Set(sessionID, data)
}
// GetWebauthnSession 获取 WebAuthn 会话数据
func (s *WebAuthnSessionStore) GetWebauthnSession(sessionID string) (*webauthn.SessionData, error) {
val, err := s.cache.Get(sessionID)
if err != nil {
if errors.Is(err, gcache.KeyNotFoundError) {
return nil, errors.New("会话未找到或已过期")
}
return nil, err // 其他 gcache 错误
}
sessionData, ok := val.(*webauthn.SessionData)
if !ok {
// 如果类型断言失败,说明缓存中存储了错误类型的数据
log.Printf("警告会话存储中发现非预期的类型Key: %s", sessionID)
// 尝试删除无效数据
_ = s.cache.Remove(sessionID)
return nil, errors.New("无效的会话数据类型")
}
return sessionData, nil
}
// DeleteWebauthnSession 删除 WebAuthn 会话数据
func (s *WebAuthnSessionStore) DeleteWebauthnSession(sessionID string) {
s.cache.Remove(sessionID)
}
func (s *WebAuthnSessionStore) GetALL() map[any]any {
return s.cache.GetALL(false)
}