This project demonstrates how to set up a to-do list application using FastAPI with a PostgreSQL database, all managed through Docker Compose. The application includes the following features:
- Users: Manage user accounts, authentication, and profiles.
- Companies: Associate users with companies to manage tasks at a corporate level.
- Tasks: Create, update, delete, and view tasks, with associations to users and companies.
- Auth Token: Secure endpoints with token-based authentication using JWT (JSON Web Tokens).
The setup also includes automatic database migration using Alembic to manage schema changes over time.
git clone https://github.com/tuonglevan/todo-fastapi.git
cd todo-fastapi
Create a .env
file in the root of the project and add the following contents:
POSTGRES_DB=todos
POSTGRES_USER=postgres
POSTGRES_PASSWORD=1234567
DEFAULT_PASSWORD_ADMIN=123456
# JWT
JWT_ACCESS_TOKEN_EXPIRE_MINUTES=60
JWT_SECRET_KEY=2334dfdfdfdfdf2334453
JWT_ALGORITHM=HS256
Use Docker Compose to build and run the application:
docker-compose up --build
This will:
- Build the FastAPI application image.
- Start the PostgreSQL database container.
- Apply database migrations using Alembic.
- Start the FastAPI application.
Once the containers are up and running, you can access the FastAPI application at: http://localhost:8000/docs
To stop the running containers, you can use:
docker-compose down
This will stop and remove the containers defined in the Docker Compose file.
If you need to run the Alembic migrations manually (e.g., after modifying models):
docker-compose run web alembic upgrade head
To create a new Alembic migration after updating your models:
docker-compose run web alembic revision -m "your message"
If you need to downgrade migrations, you have a few options:
To downgrade by one revision:
docker-compose run web alembic downgrade -1
To downgrade to a specific revision, replace revision_id
with the target revision ID:
docker-compose run web alembic downgrade <revision_id>
To downgrade all the way back to the base (earliest) state:
docker-compose run web alembic downgrade base