- Implement the backend of a microblogging service
- You need to rename the file .env.template to .env and, if necessary, modify the variables.
- In the root folder, run the command:
docker-compose up
ordocker-compose up -d
- You can find all the test users in the file
alembic/versions/65272ae975a2_add_test_user.py
Here they are anyway:
{"username": "testuser1", "api_key": "test"},
{"username": "testuser2", "api_key": "test2"},
{"username": "testuser3", "api_key": "test3"}
The backend is designed so that you can only see the tweets of the user you are following
Before you begin, make sure you have the following prerequisites installed on your system:
- Python 3.11.5
- PostgreSQL 15.4
- Clone the project repository to your computer
git clone https://github.com/r00tk3y/fastapi-Twitter
- Enter the repository:
cd fastapi-Twitter
To isolate project dependencies and avoid conflicts with system libraries, it's recommended to create and activate a virtual environment. Follow these steps:
-
Create a virtual environment in the project's root folder:
python -m venv venv
-
Activate the virtual environment based on your operating system:
- Windows
venv\Scripts\activate.bat
- Linux
source venv/bin/activate
After activating the virtual environment, install the project dependencies using the package manager for your application stack:
- For python use
pip
:- Dependencies for the production environment:
pip install -r requirements.txt
- Dependencies for the development environmen
These dependencies may include code analysis tools (linters) and additional libraries for testing.pip install -r requirements.dev.txt
In the folder src
Enter the command in the terminal:
alembic upgrade head
Rename the configuration template file from .env.template to .env in the project's root directory:
mv .env.template .env
POSTGRES_HOST=localhost
adjust this value, otherwise you will get gaia socket errors
To run your server using Uvicorn, follow these steps::
-
Ensure that you have activated the virtual environment as indicated in "Step 3."
-
Start your application using Uvicorn in the root directory:
uvicorn src.main:app --host 0.0.0.0 --port 8000 --reload
-
Navigate to the
src/tests
folder -
Run tests via
pytest
or for detailed output
pytest -s
You can modify this in the alembic/versions/_add_test_user.py folder In this step, we will add initial data to the database and establish relationships between users.
To begin with, we will insert data into the users' table. In the example below, we use the op.bulk_insert
operation to insert multiple users into the users_table
. Each user has a username (username)
and an API key (api_key)
. These values will already be added to the database if you completed Step 5.
op.bulk_insert(
users_table,
[
{"username": "testuser1", "api_key": "test"},
{"username": "testuser2", "api_key": "test2"},
{"username": "testuser3", "api_key": "test3"},
],
)
Often in applications, there are relationships between users, such as "followers" and "following." For this purpose, we create a separate table called user_to_user that will store information about who is following whom.
users_to_users_table = sa.table(
"user_to_user",
sa.column("follower_id", sa.Integer),
sa.column("following_id", sa.Integer),
)
Then, we use op.bulk_insert
to add subscription records. In the example below, user with follower_id
1
is following user with following_id
3
, and vice versa.
op.bulk_insert(
users_to_users_table,
[
{"follower_id": 1, "following_id": 3},
{"follower_id": 3, "following_id": 1},
],
)