A Go backend service implementing hexagonal architecture with user management functionality.
- User Registration: Register new users with secure password hashing
- User Login: Authenticate users and receive JWT tokens
- JWT Protection: All user endpoints protected with JWT middleware
- Token Validation: HMAC (HS256) signed tokens with configurable secret
- Get Current User: Fetch authenticated user's profile
- Get User by ID: Fetch a specific user by their ID
- List All Users: Retrieve all users in the system
- Update User: Modify user's name and email
- Delete User: Remove a user from the system
- HTTP Request Logging: Logs all HTTP requests with method, path, and execution time
- Configurable Log Levels: Support for INFO, WARN, ERROR levels
- Multiple Log Formats: Simple, detailed, and JSON structured logging
- Health Check Endpoints:
/healthand/readyfor monitoring
- Protocol Buffers: Defined .proto files for type-safe communication
- gRPC Server: High-performance gRPC server with JWT authentication
- HTTP Gateway: REST-like HTTP endpoints that proxy to gRPC methods
- Token Security: JWT token validation via gRPC metadata
POST /api/v1/auth/register
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}
POST /api/v1/auth/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "password123"
}
Note: All user endpoints require JWT token in Authorization header: Bearer <token>
GET /api/v1/users/me
Authorization: Bearer <jwt_token>
GET /api/v1/users/{id}
Authorization: Bearer <jwt_token>
GET /api/v1/users
Authorization: Bearer <jwt_token>
PUT /api/v1/users/{id}
Authorization: Bearer <jwt_token>
Content-Type: application/json
{
"name": "Updated Name",
"email": "updated@example.com"
}
DELETE /api/v1/users/{id}
Authorization: Bearer <jwt_token>
GET /health
GET /ready
The gRPC server runs on port 9000 and provides the following services:
POST /grpc/users- Create user via gRPCGET /grpc/users- List users via gRPCGET /grpc/users/{id}- Get user by ID via gRPC
UserService.CreateUser- Create new userUserService.GetUser- Get user by IDUserService.ListUsers- List all usersUserService.UpdateUser- Update userUserService.DeleteUser- Delete user
gRPC Authentication: Include JWT token in metadata:
authorization: Bearer <jwt_token>
This project follows hexagonal architecture principles:
- Domain Layer (
internal/domain/): Core business entities - Ports Layer (
internal/ports/): Interfaces for external dependencies - Service Layer (
internal/service/): Business logic implementation - Adapters Layer (
internal/adapters/): External integrations- HTTP handlers for REST API
- MongoDB repository implementation
docker-compose up -d- Start MongoDB
- Configure environment variables in
.env:PORT=8000 MONGO_URI=mongodb://localhost:27017 DB_NAME=appdb JWT_SECRET=your-super-secret-jwt-key-change-this-in-production LOG_LEVEL=INFO DETAILED_LOGGING=false JSON_LOGGING=false GRPC_PORT=9000 - Run the servers:
make deps # Download dependencies make dev # Run HTTP server with auto-reload # OR make run # Build and run HTTP server # For gRPC server make run-grpc # Run gRPC server # Test gRPC HTTP gateway make test-grpc # Test gRPC endpoints
Servers:
- HTTP Server: Port specified in
.env(default: 3000) - gRPC Server: Port 9000
- gRPC HTTP Gateway: Port 8081
Run the tests:
go test ./tests/...