Skip to content

P2: Refactor large handler file (server.go) #68

@BadgerOps

Description

@BadgerOps

Large Handler File Needs Refactoring

Priority: P2 - Medium
Estimate: 4 hours
Review Reference: REVIEW.md (P2-1)

Issue

internal/http/server.go is 1171 lines with mixed concerns:

  • Route registration
  • Request handling
  • Business logic (CIDR computation)
  • CSV export
  • Error handling

Impact: Low - Maintenance difficulty as codebase grows

Solution

Split into logical files:

internal/http/
├── server.go          (100 lines) - Server struct, RegisterRoutes
├── middleware.go      (50 lines)  - Middleware functions
├── handlers_pools.go  (300 lines) - Pool CRUD handlers
├── handlers_accounts.go (200 lines) - Account CRUD handlers
├── handlers_blocks.go (300 lines) - Block enumeration
├── handlers_export.go (200 lines) - Export functionality
├── handlers_health.go (50 lines)  - Health/ready endpoints
├── cidr.go           (150 lines) - CIDR validation/computation
├── helpers.go        (50 lines)  - writeJSON, writeErr
└── types.go          (50 lines)  - apiError, blockInfo, etc.

Acceptance Criteria

  • server.go reduced to ~100 lines (Server struct, RegisterRoutes)
  • Handlers grouped by resource in separate files
  • CIDR logic extracted to cidr.go
  • Export functionality in handlers_export.go
  • All tests still pass
  • No circular dependencies
  • Imports cleaned up

Benefits

  • Easier navigation and maintenance
  • Clear separation of concerns
  • Easier to review diffs
  • Better code organization for new contributors

Validation

make lint
make test
# Verify all tests pass after refactoring

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions