@@ -383,110 +383,65 @@ The project has recently undergone the following improvements:
383
383
### 3. Eliminated Global Variables
384
384
- ** Problem** : The project used global variables to store service instances, violating dependency injection principles
385
385
- ** Solution** :
386
- - Removed the use of global variables ` service.ExampleSvc ` and ` service.EventBus `
387
- - Passed service instances through dependency injection
388
- - Initialized services using dependency injection when starting the HTTP server
386
+ - Removed global service variables
387
+ - Properly injected services via the Factory pattern
388
+ - Improved testability by making dependencies explicit
389
389
390
- ### 4. Improved Application Layer Integration
391
- - ** Problem** : Application layer use cases were not fully utilized, with the HTTP server not enabling the application layer by default
390
+ ### 4. Enhanced Architecture Validation
391
+ - ** Problem** : Architecture validation was manual and prone to errors
392
392
- ** Solution** :
393
- - Enabled application layer use cases by default
394
- - Used the use case factory to create and manage use cases
395
- - Implemented clearer error handling and response mapping
393
+ - Implemented automated layer dependency checking
394
+ - Enforced strict architectural boundaries through code scanning
395
+ - Added validation to CI pipeline
396
396
397
- ## Recent Optimizations
398
-
399
- The project has recently undergone the following optimizations:
400
-
401
- 1 . ** Environment Variable Support** :
402
- - Added functionality for environment variable overrides for configuration files, making the application more flexible in containerized deployments
403
- - Used a unified prefix (APP_ ) and hierarchical structure (e.g., APP_MYSQL_HOST) to organize environment variables
404
-
405
- 2 . ** Unified Error Handling** :
406
- - Implemented an application-level error type system, supporting different types of errors (validation, not found, unauthorized, etc.)
407
- - Added unified error response handling, mapping internal errors to appropriate HTTP status codes
408
- - Improved error logging to ensure all unexpected errors are properly recorded
397
+ ### 5. Graceful Shutdown
398
+ - ** Problem** : Application didn't handle shutdown gracefully, potentially causing data loss
399
+ - ** Solution** :
400
+ - Implemented a graceful shutdown mechanism for the server, ensuring all in-flight requests are completed before shutting down
401
+ - Added shutdown timeout settings to prevent the shutdown process from hanging indefinitely
402
+ - Improved signal handling, supporting SIGINT and SIGTERM signals
409
403
410
- 3 . ** Request Logging Middleware** :
411
- - Added detailed request logging middleware to record request methods, paths, status codes, latency, and other information
412
- - In debug mode, request and response bodies can be logged to help developers troubleshoot issues
413
- - Intelligently identifies content types to avoid logging binary content
404
+ ### 6. Internationalization Support
405
+ - ** Problem** : The application lacked proper internationalization support
406
+ - ** Solution** :
407
+ - Added translation middleware for multi-language validation error messages
408
+ - Automatically selected appropriate language based on the Accept-Language header
414
409
415
- 4 . ** Request ID Tracking** :
416
- - Generated unique request IDs for each request, facilitating tracking in distributed systems
417
- - Returned request IDs in response headers for client reference
418
- - Included request IDs in logs to correlate multiple log entries for the same request
410
+ ### 7. CORS Support
411
+ - ** Problem** : Cross-origin requests were not properly handled
412
+ - ** Solution** :
413
+ - Added CORS middleware to handle cross-origin requests
414
+ - Configured allowed origins, methods, headers, and credentials
419
415
420
- 5 . ** Graceful Shutdown** :
421
- - Implemented a graceful shutdown mechanism for the server, ensuring all in-flight requests are completed before shutting down
422
- - Added shutdown timeout settings to prevent the shutdown process from hanging indefinitely
423
- - Improved signal handling, supporting SIGINT and SIGTERM signals
416
+ ### 8. Debugging Tools
417
+ - ** Problem** : Diagnosis of performance issues in production was difficult
418
+ - ** Solution** :
419
+ - Integrated pprof performance analysis tools for diagnosing performance issues in production environments
420
+ - Can be enabled or disabled via configuration file
424
421
425
- 6 . ** Internationalization Support** :
426
- - Added translation middleware for multi-language validation error messages
427
- - Automatically selected appropriate language based on the Accept-Language header
422
+ ### 9. Advanced Redis Integration
423
+ - ** Problem** : Redis implementation was limited and lacked proper connection management
424
+ - ** Solution** :
425
+ - Enhanced Redis client with proper connection pooling
426
+ - Added comprehensive health checks and monitoring
427
+ - Improved error handling and connection lifecycle management
428
428
429
- 7 . ** CORS Support** :
430
- - Added CORS middleware to handle cross-origin requests
431
- - Configured allowed origins, methods, headers, and credentials
429
+ ### 10. Structured Request Logging
430
+ - ** Problem** : API requests lacked proper logging, making debugging difficult
431
+ - ** Solution** :
432
+ - Implemented comprehensive request logging middleware
433
+ - Added request ID tracking for correlating logs
434
+ - Configured log levels based on status codes
432
435
433
- 8 . ** Debugging Tools** :
434
- - Integrated pprof performance analysis tools for diagnosing performance issues in production environments
435
- - Can be enabled or disabled via configuration file
436
+ ### 11. Unified Error Response Format
437
+ - ** Problem** : Error responses had inconsistent formats across the API
438
+ - ** Solution** :
439
+ - Standardized error response structure with code, message, and details
440
+ - Added documentation references to errors
441
+ - Implemented consistent HTTP status code mapping
436
442
437
443
These optimizations make the project more robust, maintainable, and provide a better development experience.
438
444
439
- ## Usage Guide
440
-
441
- ### Environment Preparation
442
-
443
- Start MySQL using Docker:
444
- ``` bash
445
- docker run --name mysql-local \
446
- -e MYSQL_ROOT_PASSWORD=mysqlroot \
447
- -e MYSQL_DATABASE=go-hexagonal \
448
- -e MYSQL_USER=user \
449
- -e MYSQL_PASSWORD=mysqlroot \
450
- -p 3306:3306 \
451
- -d mysql:latest
452
- ```
453
-
454
- ### Development Tool Installation
455
-
456
- ``` bash
457
- # Install development tools
458
- make init && make precommit.rehook
459
- ```
460
-
461
- Or install manually:
462
-
463
- ``` bash
464
- # Install pre-commit
465
- brew install pre-commit
466
- # Install golangci-lint
467
- brew install golangci-lint
468
- # Install commitlint
469
- npm install -g @commitlint/cli @commitlint/config-conventional
470
- # Add commitlint configuration
471
- echo " module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
472
- # Add pre-commit hook
473
- make precommit.rehook
474
- ```
475
-
476
- ### Running the Project
477
-
478
- ``` bash
479
- # Run the project
480
- go run cmd/main.go
481
- ```
482
-
483
- ### Testing
484
-
485
- ``` bash
486
- # Run tests
487
- go test ./...
488
- ```
489
-
490
445
## Extension Plans
491
446
492
447
- ** gRPC Support** - Add gRPC service implementation
0 commit comments