mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-11 01:36:37 +08:00
refactor(api): extract repository layer and thin HTTP handlers
Introduce internal/repository for data access and cache-backed system config reads. Move business logic into logics.go across admin push, user, template, cache, system_config, and upload/handler packages. Remove Gin from internal/util by relocating request-scoped helpers to oauth/gin_context.go. Propagate request context for config lookups in user flows. Slim model entities and delete model-level DB/cache helpers. Wire handlers to logics/repository so targeted packages no longer call db.DB directly. Update admin router tests to use ErrorHandlerMiddleware.
This commit is contained in:
@@ -4,15 +4,11 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/db"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -70,8 +66,6 @@ func (pc *PushChannel) Validate() error {
|
||||
return errors.New("request URL/address is required")
|
||||
}
|
||||
|
||||
// For custom and lark, we must enforce https:// URL prefix for security.
|
||||
// For email, it is an SMTP host:port, so no need for https:// prefix.
|
||||
if pc.Type != TypeEmail && !strings.HasPrefix(pc.URL, "https://") {
|
||||
return errors.New("request URL must use HTTPS protocol for security reasons")
|
||||
}
|
||||
@@ -102,58 +96,4 @@ func validateJSON(s string) error {
|
||||
return nil
|
||||
}
|
||||
return errors.New("payload schema must be a valid JSON format")
|
||||
}
|
||||
|
||||
// GetPushChannelByName 根据名称获取消息通道
|
||||
func GetPushChannelByName(ctx context.Context, name string) (*PushChannel, error) {
|
||||
var channel PushChannel
|
||||
err := db.DB(ctx).Where("name = ?", name).First(&channel).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &channel, nil
|
||||
}
|
||||
|
||||
const activePushChannelCacheTTL = 24 * time.Hour
|
||||
|
||||
// GetActivePushChannelByName 根据名称获取启用的消息通道 (优先从 Redis 缓存获取)
|
||||
func GetActivePushChannelByName(ctx context.Context, name string) (*PushChannel, error) {
|
||||
cacheKey := "push:channel:active:" + name
|
||||
var channel PushChannel
|
||||
if db.Redis != nil {
|
||||
if err := db.GetJSON(ctx, cacheKey, &channel); err == nil {
|
||||
return &channel, nil
|
||||
}
|
||||
}
|
||||
|
||||
err := db.DB(ctx).Where("name = ? AND enabled = ?", name, true).First(&channel).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if db.Redis != nil {
|
||||
// 缓存有效时间设置为 24 小时
|
||||
_ = db.SetJSON(ctx, cacheKey, channel, activePushChannelCacheTTL)
|
||||
}
|
||||
|
||||
return &channel, nil
|
||||
}
|
||||
|
||||
// DeleteActivePushChannelCache 清理启用消息通道的缓存
|
||||
func DeleteActivePushChannelCache(ctx context.Context, name string) {
|
||||
if db.Redis != nil {
|
||||
_ = db.Redis.Del(ctx, db.PrefixedKey("push:channel:active:"+name)).Err()
|
||||
}
|
||||
}
|
||||
|
||||
// AfterSave GORM 保存后钩子,用于自动清理缓存
|
||||
func (pc *PushChannel) AfterSave(tx *gorm.DB) error {
|
||||
DeleteActivePushChannelCache(tx.Statement.Context, pc.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AfterDelete GORM 删除后钩子,用于自动清理缓存
|
||||
func (pc *PushChannel) AfterDelete(tx *gorm.DB) error {
|
||||
DeleteActivePushChannelCache(tx.Statement.Context, pc.Name)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user