refactor: Update service and repository methods to use context

- Refactored multiple service and repository methods to accept context as a parameter, enhancing consistency and enabling better control over request lifecycles.
- Updated handlers to utilize context in method calls, improving error handling and performance.
- Cleaned up Dockerfile by removing unnecessary whitespace.
This commit is contained in:
lan
2025-12-03 15:27:12 +08:00
parent 4824a997dd
commit 0bcd9336c4
32 changed files with 833 additions and 497 deletions

View File

@@ -32,8 +32,8 @@ const (
RedisTTL = 0 // 永不过期,由应用程序管理过期时间
)
// signatureService 签名服务实现
type signatureService struct {
// SignatureService 签名服务(导出以便依赖注入)
type SignatureService struct {
profileRepo repository.ProfileRepository
redis *redis.Client
logger *zap.Logger
@@ -44,8 +44,8 @@ func NewSignatureService(
profileRepo repository.ProfileRepository,
redisClient *redis.Client,
logger *zap.Logger,
) *signatureService {
return &signatureService{
) *SignatureService {
return &SignatureService{
profileRepo: profileRepo,
redis: redisClient,
logger: logger,
@@ -53,7 +53,7 @@ func NewSignatureService(
}
// NewKeyPair 生成新的RSA密钥对
func (s *signatureService) NewKeyPair() (*model.KeyPair, error) {
func (s *SignatureService) NewKeyPair() (*model.KeyPair, error) {
privateKey, err := rsa.GenerateKey(rand.Reader, KeySize)
if err != nil {
return nil, fmt.Errorf("生成RSA密钥对失败: %w", err)
@@ -132,7 +132,7 @@ func (s *signatureService) NewKeyPair() (*model.KeyPair, error) {
}
// GetOrCreateYggdrasilKeyPair 获取或创建Yggdrasil根密钥对
func (s *signatureService) GetOrCreateYggdrasilKeyPair() (string, *rsa.PrivateKey, error) {
func (s *SignatureService) GetOrCreateYggdrasilKeyPair() (string, *rsa.PrivateKey, error) {
ctx := context.Background()
// 尝试从Redis获取密钥
@@ -201,7 +201,7 @@ func (s *signatureService) GetOrCreateYggdrasilKeyPair() (string, *rsa.PrivateKe
}
// GetPublicKeyFromRedis 从Redis获取公钥
func (s *signatureService) GetPublicKeyFromRedis() (string, error) {
func (s *SignatureService) GetPublicKeyFromRedis() (string, error) {
ctx := context.Background()
publicKey, err := s.redis.Get(ctx, PublicKeyRedisKey)
if err != nil {
@@ -218,7 +218,7 @@ func (s *signatureService) GetPublicKeyFromRedis() (string, error) {
}
// SignStringWithSHA1withRSA 使用SHA1withRSA签名字符串
func (s *signatureService) SignStringWithSHA1withRSA(data string) (string, error) {
func (s *SignatureService) SignStringWithSHA1withRSA(data string) (string, error) {
ctx := context.Background()
// 从Redis获取私钥