In the main.py file, there is a line related to database schema creation:
models.Base.metadata.create_all(bind=engine)
This line has been commented out to bypass a pytest error when running the tests in GitHub Actions. If you are running the application locally or with docker-compose and need to create the database schema, uncomment this line in main.py:
models.Base.metadata.create_all(bind=engine)
This will ensure that the database tables are created when the application starts.
Before running the project, you need to set up the environment variables. This is done by creating a .env file in the root directory of this project.
-
In the root directory of the project, create a file named
.env. -
Add the following content to the
.envfile:MYSQL_ROOT_PASSWORD=password01 MYSQL_DATABASE=aptardb DATABASE_CONNECTION_STRING=mysql+mysqlconnector://root:password01@mysql_db:3306/aptardb
-
Save the file.
These environment variables are required for connecting to the MySQL database used in the project.
MYSQL_ROOT_PASSWORD: Specifies the root password for the MySQL server.MYSQL_DATABASE: The name of the MySQL database to be used.DATABASE_CONNECTION_STRING: This is the connection string used by the application to connect to the MySQL database.
Use Docker Compose to build and run the containers. This will set up the FastAPI app, a Redis instance, a Celery worker, and the Flower monitoring tool.
docker-compose up --build
- FastAPI Application: Access the API at
http://localhost:8000. - Swagger Documentation: The API's documentation page can be accessed at
http://localhost:8000/docs. - Flower Monitoring Tool: Access Flower at
http://localhost:5555to monitor Celery tasks.
To stop the running containers, use:
docker-compose down
Begin by cloning the repository to your local machine, navigating into the project directory, and creating a virtual environment:
git clone https://github.com/ManassehV2/aptarapi.git
cd project-name
python3 -m venv aptarapivenv
Activate the virtual enviroment using the following command:
source ./aptarapivenv/bin/activate
Now install the dependencies from the requirements.txt file using the following command
pip install -r requirements.txt
To connect your application to the database, export the connection string as an environment variable:
export DB_CONNECTION_STRING="mysql+mysqlconnector://user:password@server/dbname"
Replace:
user: Your database username.password: Your database password.server: The database server's hostname or IP.dbname: The name of your database.
Verify:
Run echo $DB_CONNECTION_STRING to ensure it’s set correctly.
If you are running the app for the first time, uncomment the following line in main.py to create the database schema:
#models.Base.metadata.create_all(bind=engine)
This will initialize the database tables. Once the database is set up, you can comment this line out again to prevent re-creating the tables on subsequent runs.
Now, once you have the enviromenment prepared, you can run the application, from the root diroctory, with:
fastapi dev app/main.py
Once you have the application up and running, the API's docmentation page(swagger) can be accessed at http://127.0.0.1:8000/docs/ where you can send your requests to the app post end point
The following is the system architecture of the application:
