refactor: cleanup unused code and simplify internal packages
This commit performs a significant cleanup of the codebase by removing unused functions, methods, and entire files across various internal modules. This reduces technical debt and simplifies the project structure. Key changes include: - **cache**: Removed unused cache key generators and metrics snapshots. - **dto**: Removed redundant converter functions and segment creation helpers. - **middleware**: Deleted the unused `logger.go` middleware and simplified `ratelimit.go` and `casbin.go`. - **model**: Removed unused ID helpers, database closing functions, and batch decryption logic. - **pkg**: Cleaned up unused utility functions in `circuitbreaker`, `crypto`, `cursor`, `hook`, and `utils`. - **service**: Deleted `account_cleanup_service.go` and removed unused helper functions in `log_cleanup_service.go` and `sensitive_service.go`. - **repository**: Removed unused private loading methods in `comment_repo.go`.
This commit is contained in:
@@ -15,6 +15,3 @@ func GetAvatarOrDefault(username, nickname, avatar string) string {
|
||||
return DefaultAvatarURL
|
||||
}
|
||||
|
||||
func GetAvatarOrDefaultFromInfo(info AvatarInfo) string {
|
||||
return GetAvatarOrDefault(info.Username, info.Nickname, info.Avatar)
|
||||
}
|
||||
|
||||
@@ -61,12 +61,6 @@ var (
|
||||
globalSnowflakeErr error
|
||||
)
|
||||
|
||||
// InitSnowflake 初始化全局雪花算法实例
|
||||
// nodeID: 机器ID,范围0-1023,可以通过环境变量 NODE_ID 配置
|
||||
func InitSnowflake(nodeID int64) error {
|
||||
globalSnowflake, globalSnowflakeErr = NewSnowflake(nodeID)
|
||||
return globalSnowflakeErr
|
||||
}
|
||||
|
||||
// GetSnowflake 获取全局雪花算法实例
|
||||
// 如果未初始化,会自动使用默认配置初始化
|
||||
@@ -191,71 +185,12 @@ func (s *Snowflake) waitNextMillis(timestamp int64) int64 {
|
||||
return now
|
||||
}
|
||||
|
||||
// ParseID 解析雪花算法ID,提取其中的信息
|
||||
// id: 要解析的雪花算法ID
|
||||
// 返回值:包含时间戳、机器ID、序列号的结构体
|
||||
func ParseID(id int64) *IDInfo {
|
||||
// 提取序列号(低12位)
|
||||
sequence := id & maxSequence
|
||||
|
||||
// 提取机器ID(中间10位)
|
||||
nodeID := (id >> nodeIDShift) & maxNodeID
|
||||
|
||||
// 提取时间戳(高41位)
|
||||
timestamp := (id >> timestampShift) + customEpoch
|
||||
|
||||
return &IDInfo{
|
||||
Timestamp: timestamp,
|
||||
NodeID: nodeID,
|
||||
Sequence: sequence,
|
||||
}
|
||||
}
|
||||
|
||||
// currentTimestamp 获取当前时间戳(毫秒)
|
||||
func currentTimestamp() int64 {
|
||||
return time.Now().UnixNano() / 1e6
|
||||
}
|
||||
|
||||
// GetNodeID 获取当前机器ID
|
||||
func (s *Snowflake) GetNodeID() int64 {
|
||||
return s.nodeID
|
||||
}
|
||||
|
||||
// GetCustomEpoch 获取自定义纪元时间
|
||||
func GetCustomEpoch() int64 {
|
||||
return customEpoch
|
||||
}
|
||||
|
||||
// IDToTime 将雪花算法ID转换为生成时间
|
||||
// 这是一个便捷方法,等价于 ParseID(id).Timestamp
|
||||
func IDToTime(id int64) time.Time {
|
||||
info := ParseID(id)
|
||||
return time.Unix(0, info.Timestamp*1e6) // 毫秒转纳秒
|
||||
}
|
||||
|
||||
// ValidateID 验证ID是否为有效的雪花算法ID
|
||||
// 检查时间戳是否在合理范围内
|
||||
func ValidateID(id int64) bool {
|
||||
if id <= 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
info := ParseID(id)
|
||||
|
||||
// 检查时间戳是否在合理范围内
|
||||
// 不能早于纪元时间,不能晚于当前时间太多(允许1分钟的时钟偏差)
|
||||
now := currentTimestamp()
|
||||
if info.Timestamp < customEpoch || info.Timestamp > now+60000 {
|
||||
return false
|
||||
}
|
||||
|
||||
// 检查机器ID和序列号是否在有效范围内
|
||||
if info.NodeID < 0 || info.NodeID > maxNodeID {
|
||||
return false
|
||||
}
|
||||
if info.Sequence < 0 || info.Sequence > maxSequence {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package utils
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ValidateEmail 验证邮箱
|
||||
@@ -43,39 +42,6 @@ func ValidatePassword(password string) bool {
|
||||
return hasUpper && hasLower && hasDigit
|
||||
}
|
||||
|
||||
// ValidatePasswordWithDetail 验证密码强度并返回详细信息
|
||||
func ValidatePasswordWithDetail(password string) (bool, string) {
|
||||
if len(password) < 8 {
|
||||
return false, "密码长度至少8位"
|
||||
}
|
||||
if len(password) > 50 {
|
||||
return false, "密码长度不能超过50位"
|
||||
}
|
||||
|
||||
var hasUpper, hasLower, hasDigit bool
|
||||
for _, c := range password {
|
||||
switch {
|
||||
case 'A' <= c && c <= 'Z':
|
||||
hasUpper = true
|
||||
case 'a' <= c && c <= 'z':
|
||||
hasLower = true
|
||||
case '0' <= c && c <= '9':
|
||||
hasDigit = true
|
||||
}
|
||||
}
|
||||
|
||||
if !hasUpper {
|
||||
return false, "密码必须包含大写字母"
|
||||
}
|
||||
if !hasLower {
|
||||
return false, "密码必须包含小写字母"
|
||||
}
|
||||
if !hasDigit {
|
||||
return false, "密码必须包含数字"
|
||||
}
|
||||
|
||||
return true, ""
|
||||
}
|
||||
|
||||
// ValidatePhone 验证手机号
|
||||
func ValidatePhone(phone string) bool {
|
||||
@@ -84,13 +50,3 @@ func ValidatePhone(phone string) bool {
|
||||
return matched
|
||||
}
|
||||
|
||||
// SanitizeHTML 清理HTML,防止XSS攻击
|
||||
func SanitizeHTML(input string) string {
|
||||
input = strings.ReplaceAll(input, "&", "&")
|
||||
input = strings.ReplaceAll(input, "<", "<")
|
||||
input = strings.ReplaceAll(input, ">", ">")
|
||||
input = strings.ReplaceAll(input, "\"", """)
|
||||
input = strings.ReplaceAll(input, "'", "'")
|
||||
input = strings.ReplaceAll(input, "/", "/")
|
||||
return input
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user