Files
ez-api/internal/service/sync.go
zenfun 58dfe5e9ac feat(server): initialize project skeleton with db and api setup
Establish the foundational structure for the ez-api server.

Key changes include:
- Set up main entry point with graceful shutdown and Gin router
- Configure database connections for PostgreSQL (GORM) and Redis
- Define core data models (User, Provider, Key, Model)
- Implement configuration loading and basic key creation handler
- Add Dockerfile for multi-stage builds and .gitignore
2025-12-02 13:35:17 +08:00

45 lines
898 B
Go

package service
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/ez-api/ez-api/internal/model"
"github.com/redis/go-redis/v9"
)
type SyncService struct {
rdb *redis.Client
}
func NewSyncService(rdb *redis.Client) *SyncService {
return &SyncService{rdb: rdb}
}
func (s *SyncService) SyncKey(key *model.Key) error {
// Hash the token
hasher := sha256.New()
hasher.Write([]byte(key.KeySecret))
tokenHash := hex.EncodeToString(hasher.Sum(nil))
redisKey := fmt.Sprintf("auth:token:%s", tokenHash)
// Store in Redis
// Using HSet to store multiple fields
fields := map[string]interface{}{
"status": key.Status,
"balance": key.Balance,
}
if key.ProviderID != nil {
fields["master_id"] = *key.ProviderID
} else {
fields["master_id"] = 0 // Default or handle as needed
}
err := s.rdb.HSet(context.Background(), redisKey, fields).Err()
return err
}