FastAPI-API

FastAPI Social Media API

A FastAPI-based API for a social media platform with features like user authentication, post creation, and voting on posts. It utilizes PostgreSQL for data storage, Alembic for database migrations, and includes unit tests with pytest. The application can be deployed on Heroku, Docker, and an Ubuntu server, with continuous integration and continuous deployment (CI/CD) pipelines.

Table of Contents

Requirements

Make sure you have the following requirements installed:

Installation

  1. Clone this repository: https://github.com/dnl0037/FastAPI-API.git

  2. Create a virtual environment and activate it: ```bash python -m venv venv source venv/bin/activate On Windows: venv\Scripts\activate
  3. Install the dependencies: ```bash pip install -r requirements.txt
  4. Configure environment variables (e.g., in a .env file): You will need to set this variables according to your database.
    • DB_USERNAME=
    • DB_HOSTNAME=
    • DB_PORT=
    • DB_PASSWORD=
    • DB_NAME=
    • SECRET_KEY= This is required to create a Token for user authentication Check the file app/oauth2.py
    • ALGORITHM= This is required to create a Token for user authentication
    • TOKEN_EXPIRATION_TIME= This is required to create a Token for user authentication
  5. Apply database migrations: ```bash alembic upgrade head
  6. Run the application locally: ```bash uvicorn main:app –host 0.0.0.0 –port 8000 –reload
  7. The application will be available at http://localhost:8000.

Usage with Postman

1. Create a User

2. Log In (Obtain a Token)

3. Use the Access Token

4. Voting

Project Structure

The most important folders in this project are:

API Endpoints

Users

Posts

Voting

Data Models

Post Model

User Model

Votes Model

Example of relationships:

These data models define the core structure of the application, allowing users to create posts, vote on posts, and maintain user accounts.

Testing

To run unit tests using pytest:

  1. Make sure you have pytest installed. If not, you can install it using pip:

    ```bash pip install pytest

  2. Navigate to the root directory of your project in your terminal.

  3. Run the following command to execute all unit tests:

    pytest
    

Deployment

Heroku

Ubuntu DigitalOcean

Docker

CI/CD pipeline

This configuration .github/workflows/build-deploy.yml file defines a Continuous Integration/Continuous Deployment (CI/CD) pipeline that automates the build, test, and deployment of the application. It ensures your project runs smoothly in different environments and updates automatically when changes are made to your repository.

Description

This pipeline file runs every time a push or pull request is made to the repository. The pipeline is divided into two main jobs:

build (Build)

This job is responsible for building and testing the application. Here’s a detailed description of the actions it performs:

  1. Environment Setup: Necessary environment variables for the application, such as database credentials and secret keys, are set.

  2. PostgreSQL Service: A PostgreSQL container is started for database testing.

  3. Environment Preparation:

    • Checkout Git Repository: The codebase is fetched from the repository.

    • Install Python 3.9: Python 3.9 is set up for the environment.

    • Upgrade Pip: Pip is upgraded to the latest version.

    • Install Dependencies: Required dependencies specified in requirements.txt are installed.

    • Run Tests with Pytest: The application is tested using Pytest.

    • Docker Image Build and Push:

    • Login to Docker Hub:

    • The pipeline logs in to Docker Hub using credentials from secrets.

    • Set up Docker Buildx:

    • Docker Buildx is set up for building and pushing images.

    • Build and Push Docker Image:

    • The Docker image is built and pushed to the Docker Hub registry.

    • You can specify image tags and caching options in this section.

    • Image Digest:

    • The image digest is displayed in the pipeline output.

deploy

This job runs on an Ubuntu-based environment and deploys the application. It depends on the build job to complete successfully. Here’s a description of the actions it performs:

  1. Environment Preparation:

    • Checkout Git Repository: The codebase is fetched from the repository.

    • Deploy to Heroku: The application is deployed to Heroku using the akhileshns/heroku-deploy action. Heroku API key, app name, and email are provided from secrets.

    • Deploy to Ubuntu Server: The application is deployed to an Ubuntu server using the appleboy/ssh-action. SSH credentials and deployment script are provided from secrets.

This CI/CD pipeline ensures that your application is built, tested, and deployed automatically, reducing manual intervention and ensuring a streamlined development process.

Contribution

If you want to contribute to this project, follow these steps:

  1. Fork the repository.
  2. Create a new branch for your contribution.
  3. Make your changes and create a pull request.