mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
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
This commit is contained in:
44
internal/service/sync.go
Normal file
44
internal/service/sync.go
Normal file
@@ -0,0 +1,44 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user