Simple Service Example
Complete microservice example using all laresto-go-common packages.
Features Demonstrated
- ✅ Logger: Structured logging
- ✅ Database: PostgreSQL connection
- ✅ Cache: Redis for caching and sessions
- ✅ Kafka: Event publishing
- ✅ JWT: Authentication with access/refresh tokens
- ✅ Validation: Request validation
- ✅ Middleware: Request ID, logging, recovery, CORS, error handling
- ✅ Tracing: Distributed tracing with OpenTelemetry
- ✅ Error Handling: Standardized error responses
Prerequisites
Start the LaResto infrastructure:
cd ../laresto-docker-compose
docker-compose up -d
Running the Service
cd examples/simple-service
go run main.go
The service will start on port 8080.
API Endpoints
Health Check
curl http://localhost:8080/health
Create User
curl -X POST http://localhost:8080/users \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "Password123",
"name": "John Doe"
}'
Get User
curl http://localhost:8080/users/user-123
Login
curl -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "Password123"
}'
Get Profile (Protected)
# First login to get token
TOKEN=$(curl -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d '{"email":"user@example.com","password":"Password123"}' \
| jq -r '.access_token')
# Then access protected endpoint
curl http://localhost:8080/api/profile \
-H "Authorization: Bearer $TOKEN"
Environment Variables
# Service
PORT=8080
LOG_LEVEL=info
ENVIRONMENT=development
# Database
DB_HOST=localhost
DB_NAME=customer_db
DB_USER=laresto_app
DB_PASSWORD=laresto_app_dev_password
# Redis
REDIS_HOST=localhost
REDIS_PASSWORD=redis_dev_password
# Kafka
KAFKA_BROKER=localhost:9092
# JWT
JWT_ACCESS_SECRET=dev-access-secret
JWT_REFRESH_SECRET=dev-refresh-secret
# Tracing
TRACING_ENABLED=true
OTLP_ENDPOINT=localhost:4318
View Traces
Access Jaeger UI: http://localhost:16686
Search for traces from simple-service.
Architecture
HTTP Request
↓
Middleware (Request ID, Logging, Recovery)
↓
Validation (Request validation)
↓
Handler (Business logic)
↓
├─ Database (PostgreSQL queries)
├─ Cache (Redis operations)
└─ Kafka (Event publishing)
↓
Response (JSON with error handling)
Code Structure
- main.go: Service initialization and routing
- Dependencies: All laresto-go-common packages
- Middleware Stack: Production-ready middleware chain
- Error Handling: Standardized error responses
- Tracing: Full request lifecycle tracking
This example serves as a template for building LaResto microservices.