refactor: extract SHA3 hashing utilities to utils/hash.go

- Move all SHA3-512 hashing functions to utils package
- Update import statements and function calls
- Maintain zero functional changes
- First step in systematic main.go refactoring

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-09-18 18:36:47 +03:00
parent 7d7e6e412a
commit f9965c8f9c
4 changed files with 134 additions and 25 deletions

68
refactor.md Normal file
View File

@@ -0,0 +1,68 @@
# Refactoring Proposal for KVS Main.go
After analyzing your 3,990-line main.go file, I've identified clear functional areas that can be separated into manageable modules.
Here's my comprehensive refactoring proposal:
Proposed File Structure
kvs/
├── main.go # Entry point + minimal server setup
├── config/
│ └── config.go # Configuration structures and loading
├── types/
│ └── types.go # All data structures and type definitions
├── auth/
│ ├── auth.go # Authentication & authorization logic
│ ├── jwt.go # JWT token management
│ ├── middleware.go # Auth middleware
│ └── permissions.go # Permission checking utilities
├── storage/
│ ├── storage.go # BadgerDB operations and utilities
│ ├── compression.go # ZSTD compression/decompression
│ ├── ttl.go # TTL and metadata management
│ └── revision.go # Revision history system
├── cluster/
│ ├── gossip.go # Gossip protocol implementation
│ ├── members.go # Member management
│ ├── sync.go # Data synchronization
│ └── merkle.go # Merkle tree operations
├── server/
│ ├── server.go # Server struct and core methods
│ ├── handlers.go # HTTP request handlers
│ ├── routes.go # Route setup
│ └── lifecycle.go # Server startup/shutdown logic
├── features/
│ ├── ratelimit.go # Rate limiting middleware and utilities
│ ├── tamperlog.go # Tamper-evident logging
│ └── backup.go # Backup system
└── utils/
└── hash.go # Hashing utilities (SHA3, etc.)
Key Benefits
1. Clear Separation of Concerns: Each package handles a specific responsibility
2. Better Testability: Smaller, focused functions are easier to unit test
3. Improved Maintainability: Changes to one feature don't affect others
4. Go Best Practices: Follows standard Go project layout conventions
5. Reduced Coupling: Clear interfaces between components
Functional Areas Identified
1. Configuration (~100 lines): Config structs, defaults, loading
2. Types (~400 lines): All data structures and constants
3. Authentication (~800 lines): User/Group/Token management, JWT, middleware
4. Storage (~600 lines): BadgerDB operations, compression, TTL, revisions
5. Clustering (~1,200 lines): Gossip, members, sync, Merkle trees
6. Server (~600 lines): Server struct, handlers, routes, lifecycle
7. Features (~200 lines): Rate limiting, tamper logging, backup
8. Utilities (~90 lines): Hashing and other utilities
Migration Strategy
1. Start with the most independent modules (types, config, utils)
2. Move storage and authentication components
3. Extract clustering logic
4. Refactor server components last
5. Create commits for each major module migration
The refactoring will maintain zero functional changes - purely cosmetic restructuring for better code organization.