This project is a simple RESTful API built using Flask, PostgreSQL, and Docker. It demonstrates basic CRUD (Create, Read, Update, Delete) operations for managing user data stored in a PostgreSQL database. The API is containerized using Docker for easy deployment.
- Create a User: Add a new user with a unique username and email.
- Read Users: Retrieve all users or a specific user by ID.
- Update a User: Modify the username and email of an existing user.
- Delete a User: Remove a user from the database.
- Dockerized: The entire application is containerized for seamless deployment.
- Database Integration: Uses PostgreSQL as the database backend.
- Backend: Flask (Python)
- Database: PostgreSQL
- Containerization: Docker and Docker Compose
- ORM: SQLAlchemy
git clone <repository-url>
cd <repository-directory>
Ensure the following environment variable is set in docker-compose.yml
:
DB_URL=postgresql://postgres:postgres@flask_db:5432/postgres
Build and run the containers using Docker Compose:
docker-compose up --build
This will:
- Start a PostgreSQL database container.
- Start the Flask application container on port
4000
.
- URL:
/test
- Method:
GET
- Description: Returns a test message to verify the API is running.
- Response:
{
"message": "test route"
}
- URL:
/users
- Method:
POST
- Description: Add a new user.
- Request Body:
{
"username": "exampleUser",
"email": "example@example.com"
}
- Response:
{
"message": "user created"
}
- URL:
/users
- Method:
GET
- Description: Retrieve all users.
- Response:
[
{
"id": 1,
"username": "exampleUser",
"email": "example@example.com"
}
]
- URL:
/users/<int:id>
- Method:
GET
- Description: Retrieve a specific user by their ID.
- Response:
{
"user": {
"id": 1,
"username": "exampleUser",
"email": "example@example.com"
}
}
- URL:
/users/<int:id>
- Method:
PUT
- Description: Update an existing user's information.
- Request Body:
{
"username": "updatedUser",
"email": "updated@example.com"
}
- Response:
{
"message": "user updated"
}
- URL:
/users/<int:id>
- Method:
DELETE
- Description: Delete a user by ID.
- Response:
{
"message": "user deleted"
}
-
Install Python dependencies:
pip install -r requirements.txt
-
Set up the PostgreSQL database and update the
DB_URL
environment variable. -
Run the Flask application:
flask run --host=0.0.0.0 --port=4000
- Database Connection Error: Ensure the
DB_URL
indocker-compose.yml
is correct and matches the database service configuration. - Port Conflict: If port
4000
or5432
is in use, modify thedocker-compose.yml
file to use a different port.