How kondohiroki/go-boilerplate works
Main Components
• Application Entry Points:
• CLI Commands: Located under the cmd directory. For example, cmd/serve_api.go for running the HTTP API, cmd/migrate.go for database migrations, and additional commands like make.go, queue.go, schedule.go, and version.go.
• Scheduler: The scheduled jobs reside under internal/scheduler/scheduler.go and internal/job for job definitions and handlers.
• Configuration:
• Config files are managed in the config directory (with YAML configurations) and loaded in cmd/root.go.
• HTTP & gRPC Interfaces:
• HTTP: Found in internal/interface/http, which includes routes for health checks, user endpoints, queue management, and miscellaneous endpoints.
• (Planned) gRPC: There is a placeholder directory under internal/interface/grpc for future gRPC API services.
• Business Logic:
• Located under internal/app (for example, internal/app/queue and internal/app/user), encapsulating domain logic for different services.
• Data Persistence:
• Database Migrations and Models: Found in internal/db/migrations and internal/db/model directories. The project uses PostgreSQL (configured via docker-compose and supported by both pgx and rdb implementations).
• Repositories and Helpers:
• Repository Layer: Found under internal/repository. This abstracts data access for job and user entities.
• Helper Modules: Multiple helpers exist (cache, pagination, queue utils, etc.) providing auxiliary functionalities.
• Logging:
• Logging is handled in internal/logger with settings encapsulated in zap_logger.go and additional logger middleware within internal/router/middleware.
• External Dependencies:
• Docker and docker-compose files indicate containerization.
• Tests are clearly separated in the test directory.
Relationships and Interactions
• The CLI commands (in cmd) serve as the entry point to load configuration, invoke business logic, and run tasks such as migrations or starting the HTTP server.
• When running as a web server, the application initializes the Fiber-based HTTP router (inside internal/router) which ties HTTP requests to corresponding handlers in internal/interface/http.
• HTTP endpoints interact with the business logic (internal/app) which then communicates with the repository layer (internal/repository) to perform database operations.
• The database layer is composed of migration scripts, models, and data access implementations, with PostgreSQL as the main database.
• Caching operations are modularized under internal/helper/cache, likely interfacing with Redis (as indicated by README that mentions “Redis Cache” and “Redis Reliable Queue”).
• The Scheduler interacts with both the job processing components (internal/job) and potential queue systems (internal/app/queue) to execute periodic tasks.
• Logging, error handling, and central responses are handled by dedicated modules (internal/logger, internal/interface/response, and pkg/exception) to enforce consistency across components.
Architectural Patterns and Principles
• Layered Architecture: The project is structured in layers – entry point (cmd), application logic (internal/app), interface (internal/interface), domain and infrastructure (internal/db, internal/repository, internal/helper).
• Modular Design: Clear separation between individual components (configuration, routing, logging, error handling) enables easier maintenance and extension.
• Extensibility: The boilerplate is designed to support adding more endpoints or background jobs easily, as shown by placeholders and organized directories dedicated to HTTP, CLI, gRPC, and job scheduling.
• Infrastructure as Code: Dockerfile and docker-compose files outline deployment and environmental setup, making it easier to containerize and deploy the system reliably.
Relevant Technologies & Frameworks
• Go Programming Language (minimum Go 1.20)
• Fiber Router for HTTP handling
• Zap Logger for logging
• Docker for containerization
• PostgreSQL for data persistence (with migration files and connection setups)
• Redis for caching and potentially reliable queuing
• Cobra for CLI interactions
Specific Elements
• Clearly labeled components for:
• CLI commands (cmd)
• HTTP API interface (internal/interface/http)
• (Future) gRPC interface (internal/interface/grpc)
• Business logic layer (internal/app)
• Database access (internal/db, internal/repository)
• Scheduler and Job processing (internal/scheduler, internal/job)
• Configuration module (config)
• Logging (internal/logger)
• Helper modules (internal/helper)
• Data flow from the CLI or HTTP entry points to business logic components.
• How the application communicates with the database and caching systems.
• Demonstrate the interaction between scheduled jobs and their execution handlers.
• Annotations: