How park-seaweed/gin-gorm-prac works
Project’s Main Components
• Web Server & Routing: The entry point is main.go, which bootstraps the application. The routing is managed under the internal/router directory (e.g., router.go, post_router.go, user_router.go) and sets up the endpoints.
• Controllers: Located in internal/controller, these provide the HTTP handlers for the various endpoints (post_controller.go, user_controller.go).
• Services: Found in internal/service, these files (post_service.go, user_service.go) contain the business logic that the controllers use to process requests.
• Data Access / Repositories: The internal/repository folder includes repository implementations (post_repository.go, user_repository.go) that interface with the database via GORM.
• Models & DTOs: The internal/model directory contains the data models for posts and users, while internal/dto holds Data Transfer Objects used to move data around.
• Configuration: The internal/config package centralizes configuration (including database settings in internal/config/database/gorm_config.go and loader.go) which sets up environment variables and app settings.
• Logging & Middleware: The internal/logger and internal/middleware directories provide logging (with zap, for example) and middleware functionality to inject cross-cutting concerns like logging or error handling.
• Docker Compose: The docker-compose.yaml suggests containerized deployment, possibly orchestrating the application with a database container.
Relationships & Interactions
• The application is initiated in main.go where the configuration, logging, and routing setup are triggered.
• The Router maps HTTP calls to Controller functions.
• The Controllers delegate business logic to the Service layer.
• The Services interact with the Repository layer to perform operations against the database using GORM.
• Middleware and Logging components are integrated into the routing (or controller) layer to handle logging and cross-cutting concerns.
• The configuration modules load environment variables and DB settings, which are used across the system.
Architectural Patterns and Design Principles
• Layered Architecture: The project follows an MVC-like or layered approach, separating concerns into controllers, services, and repositories.
• Separation of Concerns: Each folder in the internal directory is given a distinct responsibility (routing, business logic, data access, configuration, logging).
• Dependency Injection: Although not explicitly stated, the separation implies that dependencies (e.g., database connections, configuration settings) are passed among layers, facilitating testability and modularity.
• Use of External Configurations and Docker for Deployment: The docker-compose and configuration files promote a containerized and environment-specific setup.