(readme created by AI)
π test-task1:
Project Description
A simple REST API written in Go with JWT-based authentication and PostgreSQL database support.
Features
- User registration with hashed password (bcrypt)
- Login with JWT token generation
- Protected endpoints using JWT
- CRUD operations on users
- Input validation (email, password length)
- Slog for logging
- PostgreSQL as storage
- Redis for caching
- Swagger documentation
- Uses context and graceful shutdown
Tech Stack
- Go (net/http)
- PostgreSQL (
github.com/lib/pq)
- JWT (
github.com/golang-jwt/jwt/v5)
- Bcrypt (
golang.org/x/crypto/bcrypt)
- Validator (
github.com/go-playground/validator/v10)
- SQL driver (
github.com/lib/pq)
- Redis (
github.com/go-redis/redis/v8)
- Swagger (
github.com/swaggo/swag)
Environment Variables example (.env)
CONFIG_PATH=./config/local.yaml
DB_PASSWORD=postgres
CACHE_PASSWORD=redis
HASH_COST=10
JWT_SECRET=somesecret
How to Run
Check Makefile for available commands.
- Set up PostgreSQL database
- Set up your Redis server
- Set up your
.env file and config on config/local.yaml.
- Run database migrations:
make migrate-up
- Rebuild swagger docs using:
swag init -g cmd/app/main.go
or use the Makefile:
make swag
- Run the app using:
go run cmd/app/main.go
Available Endpoints
| Method |
Endpoint |
Auth |
Description |
| POST |
/login |
β |
Login and get JWT |
| POST |
/users |
β |
Register a new user |
| GET |
/users |
β
|
Get all users |
| GET |
/users/{id} |
β
|
Get user by ID |
| PUT |
/users/{id} |
β
|
Update user (name/email) |
| DELETE |
/users/{id} |
β
|
Delete user by ID |
Assignment Requirements
- β
All endpoints implemented
- β
JWT authorization
- β
Passwords hashed with bcrypt
- β
Unique email constraint
- β
PostgreSQL storage
- β
Proper request validation
- β
Unauthorized access returns 401
π‘ API Endpoints
All endpoints (except POST /users and POST /login) require a valid JWT in the Authorization: Bearer <token> header.
π POST /login
Description: Authenticates the user and returns a JWT token.
Auth: β No.
Body:
{
"email": "john.doe@example.com",
"password": "P@ssw0rd123"
}
Response:
{
"token": "<jwt-token>"
}
β POST /users
Description: Registers a new user (sign up).
Auth: β No.
Body:
{
"name": "John Doe",
"email": "john.doe@example.com",
"password": "P@ssw0rd123"
}
Response:
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com"
}
π₯ GET /users
Description: Returns a list of all users.
Auth: β
Yes
Response:
[
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com"
},
"..."
]
π GET /users/{id}
Description: Returns a single user by ID.
Auth: β
Yes
Response:
{
"id": 1,
"name": "John Doe",
"email": "john.doe@example.com"
}
βοΈ PUT /users/{id}
Description: Updates a userβs name and email.
Auth: β
Yes
Body:
{
"name": "Updated Name",
"email": "updated@example.com"
}
Response:
{
"name": "Updated Name",
"email": "updated@example.com"
}
β DELETE /users/{id}
Description: Deletes a user by ID.
Auth: β
Yes
Response:
Status 204 No Content with no json body.