refactor: remove duplicate Server methods and clean up main.go

- Removed all duplicate Server methods from main.go (630 lines)
- Fixed import conflicts and unused imports
- main.go reduced from 3,298 to 340 lines (89% reduction)
- Clean modular structure with server package handling all server functionality
- Achieved clean build with no compilation errors

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-20 17:18:59 +03:00
parent a5ea869b28
commit 85f3aa69d2
6 changed files with 958 additions and 3355 deletions

View File

@@ -7,46 +7,47 @@ import (
"os"
"path/filepath"
"sync"
"time"
"github.com/dgraph-io/badger/v3"
"github.com/dgraph-io/badger/v4"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
"github.com/kalzu/kvs/auth"
"github.com/kalzu/kvs/cluster"
"github.com/kalzu/kvs/storage"
"github.com/kalzu/kvs/types"
"kvs/auth"
"kvs/cluster"
"kvs/storage"
"kvs/types"
)
// Server represents the KVS node
type Server struct {
config *types.Config
db *badger.DB
mode string // "normal", "read-only", "syncing"
modeMu sync.RWMutex
logger *logrus.Logger
httpServer *http.Server
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
config *types.Config
db *badger.DB
mode string // "normal", "read-only", "syncing"
modeMu sync.RWMutex
logger *logrus.Logger
httpServer *http.Server
ctx context.Context
cancel context.CancelFunc
wg sync.WaitGroup
// Cluster services
gossipService *cluster.GossipService
syncService *cluster.SyncService
merkleService *cluster.MerkleService
bootstrapService *cluster.BootstrapService
// Storage services
storageService *storage.StorageService
revisionService *storage.RevisionService
// Phase 2: Backup system
cronScheduler *cron.Cron // Cron scheduler for backups
backupStatus types.BackupStatus // Current backup status
backupMu sync.RWMutex // Protects backup status
cronScheduler *cron.Cron // Cron scheduler for backups
backupStatus types.BackupStatus // Current backup status
backupMu sync.RWMutex // Protects backup status
// Authentication service
authService *auth.AuthService
authService *auth.AuthService
}
// NewServer initializes and returns a new Server instance
@@ -109,7 +110,7 @@ func NewServer(config *types.Config) (*Server, error) {
return nil, fmt.Errorf("failed to initialize storage service: %v", err)
}
server.storageService = storageService
// Initialize revision service
server.revisionService = storage.NewRevisionService(storageService)
@@ -168,9 +169,9 @@ func (s *Server) getJoinedTimestamp() int64 {
func (s *Server) getBackupStatus() types.BackupStatus {
s.backupMu.RLock()
defer s.backupMu.RUnlock()
status := s.backupStatus
// Calculate next backup time if scheduler is running
if s.cronScheduler != nil && len(s.cronScheduler.Entries()) > 0 {
nextRun := s.cronScheduler.Entries()[0].Next
@@ -178,6 +179,6 @@ func (s *Server) getBackupStatus() types.BackupStatus {
status.NextBackupTime = nextRun.Unix()
}
}
return status
}
}