A simple microservices project to learn Go, REST APIs, and PostgreSQL.
- Building REST APIs in Go
- Database operations with PostgreSQL
- Layered architecture (handlers → services → repository)
- Docker basics
- Testing APIs
user-service/
├── main.go # Entry point - starts the server
├── models/ # Data structures
│ └── user.go
├── database/ # Database connection
│ └── database.go
├── repository/ # Database queries
│ └── user_repository.go
├── services/ # Business logic
│ └── user_service.go
├── handlers/ # HTTP request handlers
│ └── user_handler.go
└── routes/ # URL routing
└── routes.go
docker-compose up -dcd user-service
go mod downloadgo run main.goYou should see:
🚀 Starting User Service...
✅ Database connected successfully
✅ Users table ready
✅ User Service running on http://localhost:8080
curl http://localhost:8080/healthcurl -X POST http://localhost:8080/users \
-H "Content-Type: application/json" \
-d '{"name":"Alice","email":"alice@example.com"}'Response:
{
"id": 1,
"name": "Alice",
"email": "alice@example.com",
"created_at": "2025-01-28T12:00:00Z"
}curl http://localhost:8080/users/1curl http://localhost:8080/usersWhen you create a user (POST /users):
-
Handler (
handlers/user_handler.go)- Receives HTTP request
- Parses JSON
- Calls service layer
-
Service (
services/user_service.go)- Validates data
- Calls repository layer
-
Repository (
repository/user_repository.go)- Executes SQL query
- Returns data
-
Response flows back through the layers
Once you're comfortable with this:
-
Add more features
- Update user endpoint
- Delete user endpoint
- Search users by email
-
Add validation
- Email format validation
- Duplicate email check
-
Add tests
- Unit tests for services
- Integration tests for handlers
-
Build a second service
- Product service
- Order service
"connection refused"
- Make sure PostgreSQL is running:
docker ps - Check if port 5432 is available
"package not found"
- Run
go mod downloadin user-service directory
"table already exists"
- That's fine! The code handles this with
IF NOT EXISTS
- Go by Example - Quick Go reference
- PostgreSQL Tutorial - Learn SQL
- REST API Best Practices - API design
MIT