A RESTful API built in Go using the Container Pattern (Dependency Injection) for clean architecture and maintainability.
This project demonstrates a well-structured Go API following clean architecture principles with a layered approach:
- Handlers: HTTP request/response handling
- Services: Business logic and validation
- Repositories: Data access layer
- Database: PostgreSQL using GORM
The project uses the Container Pattern for dependency injection, ensuring loose coupling and testability.
graph TD
Entrypoint[Entrypoint] --> Handlers[Handlers]
Handlers --> Services[Services]
Services --> Repositories[Repositories]
Repositories --> DB[(DB)]
Services --> ServicesDetail
Repositories --> RepositoriesDetail
subgraph ServicesDetail["Services"]
direction LR
S1[Installment Service<br/>Business Logic]
end
subgraph RepositoriesDetail["Repositories"]
direction LR
R1[Installment Repository<br/>Data Access]
end
- Entrypoint (
cmd/api/main.go): Application bootstrap, database connection, and server initialization - Handlers: HTTP layer that receives requests, validates input, and returns responses
- Services: Business logic layer that contains validation rules and orchestrates operations
- Repositories: Data access layer that interacts with the database
- Interfaces: Contract definitions for services and repositories (enables dependency injection)
api-in-go/
├── cmd/
│ └── api/
│ └── main.go # Application entrypoint
├── src/
│ ├── handlers/ # HTTP handlers
│ │ ├── handlers.go # Handler container
│ │ └── installment/
│ │ └── installment.go # Installment endpoints
│ ├── services/ # Business logic
│ │ ├── services.go # Service container
│ │ └── installment/
│ │ └── service.go # Installment business logic
│ ├── repositories/ # Data access
│ │ ├── repositories.go # Repository container
│ │ └── installment/
│ │ └── repository.go # Installment data access
│ ├── interfaces/ # Contract definitions
│ │ └── installment.go # Installment interfaces
│ └── structs/ # Data models
│ ├── installment.go # Installment model
│ └── response.go # API response model
├── go.mod # Go module definition
├── go.sum # Dependency checksums
└── README.md # This file
- Go 1.25.4 or higher
- PostgreSQL database
- Environment variables configured (see Environment Variables section)
git clone <repository-url>
cd api-in-gogo mod init github.com/lucas54neves/api-in-gogo mod downloadCreate a .env file in the root directory with the following variables:
DB_HOST=localhost
DB_PORT=5432
DB_USER=your_username
DB_PASSWORD=your_password
DB_NAME=your_database_nameThe project uses the following main dependencies:
- Fiber v2: Fast HTTP web framework
- GORM: ORM library for Go
- PostgreSQL Driver: GORM driver for PostgreSQL
- GoDotEnv: Environment variable loader
go run cmd/api/main.goThe server will start on port 3001 by default.
go build -o bin/api cmd/api/main.go
./bin/apiGET /health
Returns the API health status.
Response:
{
"status": "ok",
"message": "API is running"
}POST /installments
Creates a new installment.
Request Body:
{
"id": "uuid-string",
"value": 100.50,
"due_date": 15
}Success Response (201 Created):
{
"message": "Installment created successfully"
}Error Responses:
-
400 Bad Request: Invalid request body
{ "message": "Invalid request body", "tag": "BAD_REQUEST" } -
500 Internal Server Error: Error creating installment
{ "message": "Error creating installment", "tag": "INTERNAL_SERVER_ERROR" }
Business Rules:
- The
due_datemust be a valid day of the month (1-31) - The
due_datecannot be in the past (must be >= current day)
The application uses PostgreSQL with GORM for database operations. The database connection is configured via environment variables.
The application automatically migrates the Installment model on startup. The Installment struct includes:
ID(string): Unique identifierValue(float64): Installment amountDueDate(int): Day of the month when the installment is due
To add a new feature following the project's architecture:
- Create the struct in
src/structs/ - Define interfaces in
src/interfaces/ - Implement repository in
src/repositories/{feature}/ - Implement service in
src/services/{feature}/ - Implement handler in
src/handlers/{feature}/ - Register in containers:
- Add to
RepositoryContainerinsrc/repositories/repositories.go - Add to
ServiceContainerinsrc/services/services.go - Register handler in
src/handlers/handlers.go
- Add to
- Update main.go to run auto-migration for new models
- Follow Go naming conventions
- Use interfaces for dependency injection
- Keep business logic in services, not handlers
- Keep data access logic in repositories, not services
This project is licensed under the MIT License - see the LICENSE file for details.
The MIT License is a permissive open-source license that allows:
- ✅ Commercial use
- ✅ Modification
- ✅ Distribution
- ✅ Private use
- ✅ Patent use
The only requirement is to include the original copyright notice and license text.