This project is a RESTful API built with Go (Golang), following best practices and clean architecture principles. It features a structured project layout, dependency injection, unit testing, and more.
The project is organized into various packages:
bootstrap: Bootstrapping and dependency injection setup.config: Configuration management.controller: HTTP handlers that process requests and return responses.database: Database connection setup and initialization.dto: Data Transfer Objects for input validation.errors: Custom error definitions and handling.migrations: Database migrations for evolving the database schema.models: Data models representing the business entities.repositories: Data access layer providing an abstraction over the database interactions.services: Business logic layer.services/mocks: Mock services for unit testing.
- Go 1.15 or higher
- PostgreSQL
- A proper Go workspace setup
- Clone the repository:
git clone https://github.com/yourusername/api-golang.git- Navigate to the project directory:
cd api-golang- Install the dependencies:
go mod tidy- Set up environment variables by creating a
.envfile in the project root with the following content:
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_HOST=localhost
DB_NAME=your_db_name
DB_PORT=5432
DB_SSLMODE=disable
- Run the database migrations:
make migrateupAir is a tool for live reloading your Go applications, allowing for automatic rebuilding and restarting whenever file changes are detected, significantly improving your development workflow.
- Install air by running:
go install github.com/cosmtrek/air@latest-
Create a configuration file named
.air.tomlin your project root, or use the default configuration by runningairinit. -
Start the development server with air:
airExecute the tests using the following command:
go test ./...