A RESTful API for managing todo items with user authentication, built with Go.
This Todo API provides a simple and efficient way to manage todo items. It includes user authentication, todo creation, retrieval, and management features. The API is built using Go and follows clean architecture principles.
- User registration and authentication
- JWT-based authentication
- Todo item management (create, retrieve)
- PostgreSQL database for data persistence
- Database migrations using Goose
- Go 1.22 or higher
- Docker and Docker Compose
- PostgreSQL (or use the provided Docker setup)
-
Clone the repository:
git clone https://github.com/yourusername/todo-api.git cd todo-api
-
Create a
.env
file in the root directory with the following content:PORT=8080 DB_URL=postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable JWT_SECRET=your_jwt_secret_key
-
Start the PostgreSQL database using Docker Compose:
docker-compose up -d
-
Run database migrations:
make migration-up
-
Start the API server:
make start
The API will be available at http://localhost:8080
.
- POST /api/v1/auth/login - Authenticate a user
Response:
{ "email": "[email protected]", "password": "password123" }
{ "token": "jwt_token_here" }
-
POST /api/v1/users - Create a new user (register)
{ "first_name": "John", "last_name": "Doe", "email": "[email protected]", "password": "password123" }
-
GET /api/v1/users/{id} - Get a user by ID (requires authentication)
-
GET /api/v1/users - Get all users (requires authentication)
-
GET /api/v1/users?email=[email protected] - Get a user by email (requires authentication)
-
POST /api/v1/todos - Create a new todo (requires authentication)
{ "text": "Buy groceries", "user_id": 1, "completed": false }
-
GET /api/v1/todos - Get all todos (requires authentication)
-
GET /api/v1/todos/{id} - Get a todo by ID (requires authentication)
CREATE TABLE users (
id bigserial PRIMARY KEY,
first_name text NOT NULL,
last_name text NOT NULL,
email citext UNIQUE NOT NULL,
password_hash text NOT NULL,
created_at timestamp(0) with time zone NOT NULL DEFAULT NOW()
);
CREATE TABLE todos (
id bigserial PRIMARY KEY,
text text NOT NULL,
completed bool NOT NULL DEFAULT false,
user_id int,
created_at timestamp(0) with time zone NOT NULL DEFAULT NOW(),
FOREIGN KEY (user_id) REFERENCES users(id)
);
The project includes a Makefile with the following commands:
make start
- Start the API servermake new-migration name=<migration_name>
- Create a new migration filemake migration-up
- Apply all pending migrationsmake migration-down
- Revert the last applied migrationmake migration-status
- Check the status of migrations
The API uses JWT (JSON Web Token) for authentication. To access protected endpoints, include the JWT token in the Authorization header:
Authorization: Bearer <your_jwt_token>
The API returns appropriate HTTP status codes and error messages in JSON format using the apierrors.APIError
type:
{
"error": {
"domain": "error",
"status_code": 400,
"message": "Invalid user credentials",
"key": "InvalidCredentials"
}
}
The project follows a clean architecture pattern with the following structure:
cmd/api
- Application entry pointconfig
- Configuration loadinginternal/domain
- Business logic and domain modelsinternal/infrastructure
- External services and toolsinternal/transport
- HTTP handlers and middlewaremigrations
- Database migration files
If you'd like to contribute, please fork the repository and open a pull request to the main
branch.