This document provides a comprehensive guide to setting up and using the User Service. Follow these steps to get the service running and understand its key components.
The User Service is a backend service designed for managing users. It supports operations such as creating, updating, retrieving, and deleting user records.
Caching: Redis is used to cache verified tokens and frequently accessed user data, improving performance.
Token Validation: Token validation requires a separate Authentication Service. The User Service will not work correctly without the Authentication Service being set up.
- User CRUD operations
- Redis caching for performance optimization
- Health check endpoint to monitor service status
- JWT Middleware for secure access
- Dependency injection for modular and testable code
- Docker and Docker Compose installed on your system.
- PostgreSQL and Redis configured via Docker Compose.
- An Authentication Service for token validation.
The service relies on PostgreSQL and Redis. Use the provided docker-compose.yml
file to set up the required containers.
-
Download or create the
docker-compose.yml
file with the following content:services: postgres: image: postgres:latest container_name: postgres_container_users_v2 environment: POSTGRES_USER: test POSTGRES_PASSWORD: 1234 POSTGRES_DB: users ports: - "5433:5432" volumes: - postgres_data:/var/lib/postgresql/data networks: - mynetwork pgadmin: image: dpage/pgadmin4 container_name: pgadmin_container_users_v2 environment: PGADMIN_DEFAULT_EMAIL: [email protected] PGADMIN_DEFAULT_PASSWORD: admin ports: - "5051:80" depends_on: - postgres networks: - mynetwork redis: image: redis:latest container_name: redis_container_users_v2 ports: - "6379:6379" networks: - mynetwork volumes: postgres_data: networks: mynetwork: driver: bridge
-
Start the services:
docker-compose up -d
-
Verify that the PostgreSQL, PgAdmin, and Redis containers are running.
Configure the service using the .env
file. Below is the required configuration:
DB_DSN=host=localhost user=test password=1234 dbname=users port=5433 sslmode=disable TimeZone=Asia/Colombo
The SetUpRouter
function initializes the Gin router and sets up routes, middleware, and controllers. It connects to both PostgreSQL and Redis.
Example routes:
- Health Check:
GET /
- User Operations:
GET /v1/users
- Get all usersGET /v1/user/:id
- Get user by IDPOST /v1/users
- Create a new userPUT /v1/update
- Update a userDELETE /v1/delete/:id
- Delete a user
- Used to cache verified tokens and user results.
- Improves the performance of frequent operations.
- Requires a separate Authentication Service.
- Without the Authentication Service, token validation will fail.
- Gin Framework: For building HTTP web services.
- Redis: For caching.
- PostgreSQL: Primary database.
- Docker Compose: For managing dependencies and infrastructure.
- Addition of more endpoints for advanced user management.
- Improved role-based access control.
- Enhanced logging and monitoring.
Once the database and caching layers are up, start the User Service:
go run main.go
Access the health check endpoint to verify the service is running:
curl http://localhost:8080/
Expected response:
{
"status": "Service is up and running"
}