Auth Service is a microservice for user authentication using JWT tokens, PostgreSQL, and Memcached.
-
User Registration (
POST /register
): Registers a new user in the system. -
Authentication and Token Generation (
POST /login
): Authenticates the user and returns a JWT token. -
Token Validation (
GET /validate-token
): Validates the token using Memcached and the database as a fallback.
- Go: Programming language.
- PostgreSQL: Database for storing user information.
- Memcached: Caching layer for token validation.
- Goose: Database migration tool.
- Gin: Web framework for building REST APIs.
-
Clone the repository:
git clone https://github.com/your-repo/auth-service.git cd auth-service
-
Ensure you have Go, Docker, and Docker Compose installed.
-
Start the services (PostgreSQL, Memcached, and the application):
docker-compose up --build
-
Run database migrations:
make migrate-up
-
The service will be available at:
http://localhost:8080
curl -X POST http://localhost:8080/register \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "testpassword"
}'
curl -X POST http://localhost:8080/login \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "testpassword"
}'
curl -X GET http://localhost:8080/validate-token \
-H "Authorization: Bearer <your JWT token>"
- Build the application:
make build
- Run the application:
make run
- Format the code:
make fmt
- Lint the code:
make lint
- Run migrations:
make migrate-up
- Rollback migrations:
make migrate-down
The following environment variables can be used for configuration:
Variable | Description | Default |
---|---|---|
DATABASE_URL |
PostgreSQL connection string | postgres://user:password@localhost:5432/auth_db?sslmode=disable |
MEMCACHED_HOST |
Memcached host | localhost |
MEMCACHED_PORT |
Memcached port | 11211 |
auth-service/
├── cmd/
│ └── auth-service/ # Application entry point
├── internal/
│ ├── controller/ # API controllers
│ ├── model/ # Database models
│ ├── repository/ # Database access layer
│ └── service/ # Business logic
├── config/ # Configuration files
├── db/
│ └── migrations/ # Database migrations
├── Dockerfile # Docker build configuration
├── docker-compose.yml # Docker Compose configuration
├── Makefile # Automation tasks
└── README.md # Project documentation