Skip to content

Athul-dev-7/fastAPI

Repository files navigation

FastAPI-App

This is a sample FastAPI CRUD application that uses a PostgreSQL database.

To run the application locally

Requirements

  • Python 3.8 or higher
  • PostgreSQL 12 or higher

Installation

1. Clone the repository:

git clone https://github.com/Athul-dev-7/fastAPI.git

2. Move to the project's root directory:

cd fastAPI

3. Create a virtual environment and activate it:

python3 -m venv venv

source venv/bin/activate

4. Install the requirements:

pip install -r requirements.txt

5. Set up the database:

  • To install PostgreSQL on Ubuntu, you can follow these steps:

      1. Update the package manager's package list:

        sudo apt update

      1. Install the PostgreSQL package:

        sudo apt install postgresql postgresql-contrib

        This will install PostgreSQL and some additional utilities.

      1. After the installation is complete, the PostgreSQL service will be started automatically. You can check the status of the service by running:

        systemctl status postgresql

      1. By default, PostgreSQL creates a user named postgres with the role postgres. You can connect to the PostgreSQL server as this user by running:

        sudo su - postgres

      1. Once you are connected to the PostgreSQL server, you can create new user and database.
  • ADD .env file at the root of the project directory and edit these fields according to your database config:

    • DATABASE_HOST_NAME=
    • DATABASE_PORT=
    • DATABASE_NAME=
    • DATABASE_USERNAME=
    • DATABASE_PASSWORD=
    • SECRET_KEY=
    • ALGORITHM=
    • ACCESS_TOKEN_EXPIRE_MINUTES=
  • Run the following command to apply the database migrations:

    alembic upgrade head

6. Start the application:

uvicorn app.main:app --reload

The application will be available at http://localhost:8000.

7. To run the test cases:

pytest

8. To get the test coverage:

coverage report -m

To run the application in a Docker container

Requirements

  • Docker
  • Docker Compose

Steps

  • Once installed the above 2 requirements, go to the project's root directory containing both Dockerfile & docker-compose.yaml files and run this command:

    sudo docker-compose up --build -d

  • Once container is up, Please follow these commands

  • To list the running containers:

    • sudo docker-compose ps
  • To migrate database using alembic

    • sudo docker-compose exec -it api bash

    • alembic upgrade head

  • To down the container

    • docker-compose down to down the container

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published