A simple REST API made using net/http and sqlite3.
The project uses Layered Architecture with a clear responsability separation and business logic isolation.
├── cmd
├── internal
│ ├── customerrors
│ ├── database
│ │ └── migrations
│ ├── domain
│ ├── http
│ │ ├── handlers
│ │ └── routers
│ ├── middleware
│ ├── repositories
│ └── services
├── pkg
│ └── config
└── test
Application entrypoint. It should contain:
main.go- Server initialization
- Dependency setup
- Manual Dependency Injection
Private Application code (can't be imported by other modules).
Contains business entities and core domain models. This layer does not depends on HTTP or database implementations.
Responsible for data access. It is responsible for interaction with SQL or an ORM with each table.
Contains business logic. It is responsible for:
- Validations
- Business rules
- Orchestrating repository calls
Transport layer
- Receive HTTP requests
- Decode and validate input
- Call services
- Return JSON responses
- Route registration
- Mapping endpoint to handlers
- Middleware calls
HTTP middleware such as:
- Logging
- Authentication
- CORS
Handles:
- Database initialization
- Database connection setup
- Migration execution
Versioned .sql files.
Application specific errors.
Reusable configuration layer. Handles:
- Environment variables
- Configuration structs
Contains:
- Unit tests
HTTP Request
↓
Router
↓
Middleware
↓
Handler
↓
Service
↓
Repository
↓
Database