mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
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
45 lines
898 B
Go
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
|
|
}
|