Go authentication service with JWT authentication and authorization.
- Authentication: Register and login with email/password
- JWT tokens: HS256-signed tokens with 7-day expiry
- Protected routes: Profile management (get/update/delete) behind JWT middleware
- Password hashing: bcrypt with default cost
- PostgreSQL: via GORM ORM with migration support (up/down)
- Provider interface: database layer can be swapped by implementing the
Providerinterface - CORS middleware: handles preflight requests and sets appropriate headers
- Rate limiting: in-memory per-IP rate limiter (100 req/min default)
- Graceful shutdown: handles SIGINT/SIGTERM with 5s timeout
- Statistics: in-memory request tracking with thread-safe access
- Standardized responses: consistent
APIResponseformat across all endpoints - Docker support: multi-stage Dockerfile and docker-compose with PostgreSQL
| Method | Path | Auth | Description |
|---|---|---|---|
| POST | /api/v1/register |
No | Register a new user |
| POST | /api/v1/login |
No | Login and get JWT |
| GET | /api/v1/health |
No | Service health/stats |
| GET | /api/v1/me |
Yes | Get current profile |
| PUT | /api/v1/me |
Yes | Update profile |
| DELETE | /api/v1/me |
Yes | Delete account |
- Go 1.24+
- PostgreSQL 16+
Copy .env.example to .env and configure:
cp .env.example .env| Variable | Description | Default |
|---|---|---|
DB_HOST |
Database host | localhost |
DB_PORT |
Database port | 5432 |
DB_NAME |
Database name | postgres |
DB_USER |
Database user | postgres |
DB_PASS |
Database password | postgres |
DB_SSL |
SSL mode | disable |
SECRET |
JWT signing secret | yourSecretKey |
PORT |
Server port | 3000 |
docker-compose up --build# Run migrations
go run scripts/migrate.go -action up
# Start the server
go run cmd/main.gogo test ./... -v├── cmd/
│ └── main.go # Application entrypoint
├── scripts/
│ └── migrate.go # Database migration CLI
├── internal/
│ ├── app/
│ │ └── app.go # App bootstrap and graceful shutdown
│ ├── config/
│ │ └── config.go # Environment-based configuration
│ ├── models/
│ │ └── user.go # GORM data models
│ ├── provider/
│ │ ├── provider.go # Provider interface
│ │ └── postgres.go # PostgreSQL implementation
│ ├── statistics/
│ │ ├── statistics.go # Thread-safe statistics tracking
│ │ └── statistics_test.go
│ └── api/
│ ├── types/
│ │ └── types.go # Request/response DTOs
│ ├── controllers/
│ │ ├── controller.go # Route handlers
│ │ └── controller_test.go
│ ├── middlewares/
│ │ ├── middleware.go # JWT, CORS, rate limit
│ │ └── middleware_test.go
│ └── routes/
│ └── routes.go # Route definitions
├── Dockerfile
├── docker-compose.yml
├── .env.example
└── .gitignore