forked from ryyst/kalzu-value-store
- 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>
80 lines
1.7 KiB
Go
80 lines
1.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// Start the server and initialize all services
|
|
func (s *Server) Start() error {
|
|
router := s.setupRoutes()
|
|
|
|
addr := fmt.Sprintf("%s:%d", s.config.BindAddress, s.config.Port)
|
|
s.httpServer = &http.Server{
|
|
Addr: addr,
|
|
Handler: router,
|
|
}
|
|
|
|
s.logger.WithFields(logrus.Fields{
|
|
"node_id": s.config.NodeID,
|
|
"address": addr,
|
|
}).Info("Starting KVS server")
|
|
|
|
// Start gossip and sync routines
|
|
s.startBackgroundTasks()
|
|
|
|
// Try to join cluster if seed nodes are configured and clustering is enabled
|
|
if s.config.ClusteringEnabled && len(s.config.SeedNodes) > 0 {
|
|
go s.bootstrap()
|
|
}
|
|
|
|
return s.httpServer.ListenAndServe()
|
|
}
|
|
|
|
// Stop the server gracefully
|
|
func (s *Server) Stop() error {
|
|
s.logger.Info("Shutting down KVS server")
|
|
|
|
// Stop cluster services
|
|
s.gossipService.Stop()
|
|
s.syncService.Stop()
|
|
|
|
// Close storage services
|
|
if s.storageService != nil {
|
|
s.storageService.Close()
|
|
}
|
|
|
|
s.cancel()
|
|
s.wg.Wait()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
if err := s.httpServer.Shutdown(ctx); err != nil {
|
|
s.logger.WithError(err).Error("HTTP server shutdown error")
|
|
}
|
|
|
|
if err := s.db.Close(); err != nil {
|
|
s.logger.WithError(err).Error("BadgerDB close error")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// startBackgroundTasks initializes and starts cluster services
|
|
func (s *Server) startBackgroundTasks() {
|
|
// Start cluster services
|
|
s.gossipService.Start()
|
|
s.syncService.Start()
|
|
}
|
|
|
|
// bootstrap joins cluster using seed nodes via bootstrap service
|
|
func (s *Server) bootstrap() {
|
|
// Use bootstrap service to join cluster
|
|
s.bootstrapService.Bootstrap()
|
|
}
|