This is a backend API boilerplate built with Go, following Data-Oriented Design (DOD) principles. It uses GORM for PostgreSQL database operations and Gin for the HTTP framework.
- Complete user management system (CRUD operations)
- Authentication system with JWT tokens
- Password hashing with bcrypt
- PostgreSQL database integration with GORM
- Structured error handling
- Environment-based configuration
- Request logging
This project follows DOD principles:
- Procedural code with clear data types
- Data flow optimization
- Bundling global variables into structs
- Module-level encapsulation
- Parameterized functions
- Focused on data organization and access patterns
.
├── cmd/
│ └── api/
│ └── main.go # Application entry point
├── config/
│ └── config.go # Configuration loader
├── internal/
│ ├── api/
│ │ ├── handlers/
│ │ │ ├── auth.go # Authentication handlers
│ │ │ └── users.go # User CRUD handlers
│ │ ├── middleware/
│ │ │ ├── auth.go # JWT authentication middleware
│ │ │ └── logging.go # Request logging middleware
│ │ └── server.go # API server setup
│ ├── data/
│ │ ├── models/
│ │ │ └── user.go # User model definition
│ │ └── store/
│ │ ├── postgres.go # Database connection
│ │ └── users.go # User data operations
│ └── utils/
│ ├── hash.go # Password hashing utilities
│ └── token.go # JWT token utilities
├── go.mod # Go module definition
├── go.sum # Go module checksums
└── README.md # Project documentation
- Go 1.21 or higher
- PostgreSQL 12 or higher
-
Clone the repository:
git clone https://github.com/yourusername/go-api-dod.git cd go-api-dod -
Install dependencies:
go mod download -
Set up environment variables:
cp .env.example .envEdit the
.envfile to match your environment. -
Set up the database:
createdb goapi -
Build and run the server:
go build -o api ./cmd/api ./apiOr simply run:
go run ./cmd/api
-
POST /signup- Register a new user- Request:
{ "email": "user@example.com", "password": "password123" } - Response:
{ "token": "JWT_TOKEN", "user": { "id": "UUID", "email": "user@example.com" } }
- Request:
-
POST /login- Login with existing user- Request:
{ "email": "user@example.com", "password": "password123" } - Response:
{ "token": "JWT_TOKEN", "user": { "id": "UUID", "email": "user@example.com" } }
- Request:
-
GET /users- List all users- Headers:
Authorization: Bearer JWT_TOKEN - Query Parameters:
limit=10&offset=0 - Response:
[{ "id": "UUID", "email": "user@example.com", "created_at": "TIMESTAMP", "updated_at": "TIMESTAMP" }]
- Headers:
-
GET /users/:id- Get a user by ID- Headers:
Authorization: Bearer JWT_TOKEN - Response:
{ "id": "UUID", "email": "user@example.com", "created_at": "TIMESTAMP", "updated_at": "TIMESTAMP" }
- Headers:
-
POST /users- Create a new user- Headers:
Authorization: Bearer JWT_TOKEN - Request:
{ "email": "newuser@example.com", "password": "password123" } - Response:
{ "id": "UUID", "email": "newuser@example.com", "created_at": "TIMESTAMP" }
- Headers:
-
PUT /users/:id- Update a user- Headers:
Authorization: Bearer JWT_TOKEN - Request:
{ "email": "updated@example.com" } - Response:
{ "id": "UUID", "email": "updated@example.com", "created_at": "TIMESTAMP", "updated_at": "TIMESTAMP" }
- Headers:
-
DELETE /users/:id- Delete a user- Headers:
Authorization: Bearer JWT_TOKEN - Response:
{ "message": "User deleted successfully" }
- Headers:
This project is licensed under the MIT License - see the LICENSE file for details.