Step 1: Fork the repository
Go to the GitHub repository thirdygayares/keriderya-api.
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/keriderya-api.git
(Replace your-username
with your actual GitHub username.)
- Change into the directory:
cd keriderya-api
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
- 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).
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", "role": "user", "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/4
Replace “4"with the ID of the user you want to retrieve.
Connect to your database cloud to check if the user data is now added
POST/restaurant-category
{
"name": "Sample"
}