A simple FastAPI application that allows you to manage tasks. You can create, update, retrieve, and delete tasks from a PostgreSQL database.
- Python (3.9+)
- PostgreSQL (Database server)
Below is a quick guide to installing PostgreSQL on an Ubuntu-like distribution. Adapt the steps if you are using a different OS.
- Update your package list:
sudo apt update
- Install PostgreSQL and its additional modules:
sudo apt install postgresql postgresql-contrib
- Switch to the
postgres
user:sudo -i -u postgres
- Enter the PostgreSQL shell:
psql
- Create a new database (for example,
simpletasks
):CREATE DATABASE tasksdb;
- Create a user (for example,
myuser
) and set a password:CREATE USER myuser WITH PASSWORD '123';
- Grant privileges for the user on the new database:
GRANT ALL PRIVILEGES ON DATABASE simpletasks TO myuser;
- Exit the PostgreSQL shell:
\q
- Exit the
myuser
user session:exit
In the project root directory, create a file named .env
. Add your database connection string:
POSTGRES_URL="postgresql://myuser:123@localhost:5432/tasksdb"
Adjust the user, password, host, and port according to your local PostgreSQL configuration.
- Clone the repository (or download the source code) and navigate to the project directory.
- (Optional) Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate
- Install the required dependencies:
pip install -r requirements.txt
- Run the application (using any ASGI server, such as uvicorn):
Alternatively, you can use the built-in uvicorn runner in your code or another process manager of your choice.
uvicorn app.main:app --host 0.0.0.0 --port 8000
Once the server is running, open your browser or HTTP client at: http://127.0.0.1:8000/docs
Below is an example of how to set up your systemd service to run the application using gunicorn with the Uvicorn worker.
Before proceeding, make sure:
-
Your PostgreSQL service is installed and running. You can verify by:
sudo systemctl status postgresql # or for version-specific: sudo systemctl status postgresql-17
If it’s not running, start it:
sudo systemctl start postgresql
-
The required Python packages (
gunicorn
,uvicorn
, etc.) are installed in an env on the server:pip install -r requirements.txt
-
Your .env file is in place with the correct database connection string.
-
PostgreSQL is properly configured with your database and user.
-
Create a systemd Service File (for example,
/etc/systemd/system/simple-tasks.service
) with the following content:[Unit] Description=Simple Tasks API After=network.target [Service] Type=simple User=youruser Group=yourgroup WorkingDirectory=/path/to/your/project Environment="PATH=/path/to/your/env/bin" ExecStart=/path/to/your/env/bin/gunicorn --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:7000 app.main:app Restart=always [Install] WantedBy=multi-user.target
Notes:
- Adjust
User
andGroup
to match your setup. - Update
WorkingDirectory
to the path to your project. - Change the
Environment
line to the path to your Python environment. - The
ExecStart
line reference should match the path where gunicorn is installed.
- Adjust
-
Reload systemd and enable/start the service:
sudo systemctl daemon-reload sudo systemctl enable simple-tasks.service sudo systemctl start simple-tasks.service
-
Check the service status:
systemctl status simple-tasks.service
Your FastAPI application will now run as a background service managed by systemd on port 7000.
Below is a summary of each endpoint provided by the tasks router:
- Method:
POST /tasks
- Request Body (JSON):
{ "title": "<string>", "is_completed": "<boolean>", "description": "<string or null>" }
- Description: Creates a new task in the database.
- Returns:
{ "message": "Task created successfully", "data": {"id": "<created task id>"}, "timestamp": "<creation time>" }
- Method:
GET /tasks/{task_id}
- URL Parameter:
task_id
(int): The ID of the task to retrieve.
- Description: Retrieves the details of a specific task by its ID.
- Returns:
{ "message": "Task retrieved successfully", "data": { "id": "<task id>", "title": "<string>", "is_completed": "<boolean>", "description": "<string or null>", "created_at": "<datetime>" }, "timestamp": "<time of retrieval>" }
- Method:
GET /tasks
- Description: Retrieves all tasks in the system.
- Returns:
{ "message": "Tasks retrieved successfully", "data": { "tasks": [ { "id": "<task id>", "title": "<string>", "is_completed": "<boolean>", "description": "<string or null>", "created_at": "<datetime>" }, ... ] }, "timestamp": "<time of retrieval>" }
- Method:
PUT /tasks/{task_id}
- URL Parameter:
task_id
(int): The ID of the task to update.
- Request Body (JSON):
{ "title": "<string or null>", "is_completed": "<boolean or null>", "description": "<string or null>" }
- Description: Updates the specified fields of a task.
- Returns:
{ "message": "Task updated successfully", "data": { "id": "<task id>", "title": "<string>", "is_completed": "<boolean>", "description": "<string or null>", "created_at": "<datetime>" }, "timestamp": "<time of update>" }
- Method:
DELETE /tasks/{task_id}
- URL Parameter:
task_id
(int): The ID of the task to delete.
- Description: Deletes the specified task from the system.
- Returns:
{ "message": "Task deleted successfully", "data": null, "timestamp": "<time of deletion>" }
If you have any questions or issues, feel free to contact:
- Email: sahashemi072@gmail.com