Step 1: Fork the repository
Go to the GitHub repository thirdygayares/restaurant_menu.
Click on the "Fork" button on the top right of the repository page.
This creates a copy of the repository in your GitHub account.
Step 2: Clone your repository
Navigate to your GitHub profile and open the newly forked repository.
Click on the green "Code" button and copy the HTTPS link.
Open your terminal and run the following command to clone the repository:
git clone https://github.com/your-username/flask-api-with-mysql.git
(Replace
your-username
with your actual GitHub username.)Change into the directory:
cd flask-api-with-mysql
Step 3: Set up the Virtual Environment
A. Create a Virtual Environment
Create a virtual environment in your project directory by running:
python -m venv myvenv
Activate the virtual environment:
myvenv\Scripts\activate
B. Install Dependencies
Install the required dependencies by running:
pip install -r requirements.txt
This will install all the packages listed in the
requirements.txt
file.
C. Set up the .env
File
Create a
.env
file in the root directory of the project.Add the following content to the
.env
file (replace{password}
,{host}
,{port}
, and{databasename1}
with your actual database credentials):FLASK_ENV=development DATABASE_URL=mysql://avnadmin:{password}@{host}:{port}/{databasename1} SECRET_KEY=your_secret_key
The
SECRET_KEY
can be any random string that Flask will use to sign cookies and sessions.
D. Add Certificates (Optional)
- If required by the application (e.g., for secure connections), copy the SSL certificates to the
app/certs
folder. Ensure the necessary certificates (cert.pem
,key.pem
, etc.) are in place.
E. Run Database Migrations
Run the following command to upgrade the database:
flask db upgrade
This will apply any pending migrations to the database.
F. Run the Application
Finally, run the application by executing:
python run.py
This should start the Flask application on
http://127.0.0.1:5000
.
Step 4: Create a Database on Aiven Dashboard
Go to the Aiven dashboard.
Create a new PostgreSQL or MySQL database.
-
Make sure to replace the database connection details (like
password
,host
,port
, anddatabasename1
) in your.env
file with the values provided by Aiven.
Step 5: Explain the Code and Show Postman Requests
Code Explanation
Flask Application: The project is a Flask-based application that manages restaurant menu items and users. It uses a MySQL database for storing user and menu information.
Endpoints:
POST /users
: Creates a new user in the database.GET /users/<id>
: Retrieves a user by their ID.
Here is an overview of the most important files:
run.py
: This is the entry point of the Flask application. It initializes the app and starts the server.app/models.py
: Contains the models for the database, such as theUser
model for user-related data.app/routes.py
: Contains the Flask routes that define the application’s API endpoints (e.g., creating and fetching users).
Example API Requests
Create User (POST)
To create a new user, use the
POST
method at the following endpoint:http://127.0.0.1:5000/users
Body: JSON format with user details:
{ "username": "john_doe", "email": "john@example.com", "password": "securepassword" }
-
Get User by ID (GET)
To fetch a user by their ID, use the
GET
method:http://127.0.0.1:5000/users/1
Replace “1"with the ID of the user you want to retrieve.
Postman Screenshot
Once you have Postman set up, you can test the API by performing the following steps:
Create User:
Set the request type to
POST
.Use the URL
http://127.0.0.1:5000/users
.Add the JSON body as shown above.
Click "Send".
Get User by ID:
Set the request type to
GET
.Use the URL
http://127.0.0.1:5000/users/1
(replace 1 with the ID of the user).Click "Send".
After sending the requests, you should see the appropriate responses in Postman (either a confirmation for creating a user or the user details when retrieving by ID).
Check your database