-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance EUI-64 Calculator with Robust Validation, Logging, and Testing #34
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The main Dockerfile is specific to production builds via GitHub Actions. Adding Dockerfile-dev and updating instructions in README.md.
- Added port configuration support - Implemented slog for structured error and failure handling - Refactored inline logic into modular functions for improved maintainability
Updated internal/eui64/eui64.go with: - Added constants for EUI-64 and IPv6 sizes/markers (e.g., macBytes, fffeMarkerLow) - Enhanced error handling for MAC length and prefix hextet validation - Replaced net.IP with custom []uint16 hextet array for IPv6 construction - Introduced ip6ToString for canonical IPv6 formatting with zero compression - Improved prefix parsing to support partial inputs and trim trailing "::" - Split logic into modular CalculateEUI64 and ip6ToString functions Strengthens robustness, flexibility, and maintainability of EUI-64 calculator.
Updated internal/handlers/handlers.go with: - Integrated validators for MAC and IPv6 prefix with specific error messages - Added slog for structured logging of validation and rendering errors - Replaced c.HTML with ui.Render for home and result pages - Introduced renderResult helper for consistent rendering with error handling - Enhanced Home handler with error logging and 500 status on failure - Restructured Calculate to validate inputs and log warnings/errors Improves input validation, observability, and code maintainability.
Introduced internal/validators/ipv6_prefix_validator.go with: - Defined constants maxHextets (4) and maxHextetLength (4) for prefix constraints - Implemented ValidateIPv6Prefix to trim whitespace, validate non-empty input, and check: - Maximum 4 hextets with proper hexadecimal digits (0-9, a-f, A-F) - No internal empty hextets, allowing trailing "::" for zero compression - Hextet length up to 4 characters - Added isHexDigit helper to verify hexadecimal characters Provides robust IPv6 prefix validation for EUI-64 calculator.
Introduced internal/validators/mac_validator.go with: - Defined constant macBytes (6) for standard 48-bit MAC address length - Implemented ValidateMAC to: - Trim whitespace and ensure non-empty input - Parse MAC address using net.ParseMAC - Verify length is exactly 6 bytes - Returns detailed errors for parsing failures or incorrect lengths Ensures valid MAC address input for EUI-64 calculations.
Updated cmd/server/main.go with: - Added StaticDir to Config, defaulting to "static" at project root - Enhanced LoadConfig to use os.Executable() for dynamic path resolution and STATIC_DIR env var override - Modified SetupRouter to use config.StaticDir for static file serving - Logged static_dir in server startup message Resolves static file access issues for both runtime and test environments.
Updated cmd/server/main_test.go with: - Modified setupRouter to set config.StaticDir to "../../static" using filepath.Join - Ensures static file serving points to root-level static/ during tests - Added path/filepath import Maintains test reliability with configurable static directory in main.go.
Codecov ReportAttention: Patch coverage is
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request significantly improves the EUI-64 Calculator by refactoring core components, adding comprehensive validation, introducing structured logging, and establishing a robust test suite. The changes enhance reliability, maintainability, and observability while preserving the application's core functionality.
Key Changes:
Server Initialization (cmd/server/main.go)
Config
struct andLoadConfig
for port andTRUSTED_PROXIES
env parsing (default port ":8080").SetupRouter
for modular Gin setup in release mode with middleware and trusted proxies.slog
for structured error logging, exiting on failures.Test Suite (cmd/server/main_test.go)
setupRouter
helper and two test functions:TestRouterSetup
: Verifies routing for home, calculate, static files, and 404 responses.TestTrustedProxies
: Ensures correct client IP handling with/without trusted proxies and error cases.httptest
andtestify/assert
for robust HTTP testing.EUI-64 Logic (internal/eui64/eui64.go)
macBytes
,prefixMaxHextets
) for clarity and maintainability.net.IP
with custom[]uint16
hextet array andip6ToString
for zero-compressed IPv6 formatting.Handlers (internal/handlers/handlers.go)
validators
for MAC and IPv6 prefix checks with specific error messages.slog
for logging validation and rendering errors.c.HTML
withui.Render
, introducedrenderResult
helper for consistent error handling.Home
andCalculate
with detailed error logging and HTTP status codes.Input Validation (internal/validators/)
net.ParseMAC
.Impact:
slog
improves debugging and monitoring.Testing:
main_test.go
, covering routing, proxy handling, and response rendering.