Skip to content

Dhruv9449/Chitros

Repository files navigation

Chitros

Social media backend API built on FastAPI


Try it here - https://chitros.dhruvshah.ml/docs


Table of contents


Features

Chitros has all the basic social media features such as -

  • Creating, modifying and deleting user profiles
  • Authentication using JWT access tokens
  • Creating, modifying and deleting posts
  • Following-Followers relationships
  • Sending and receiving follow requests
  • Likes, comments and replies
  • Viewing feed sorted by likes and date of upload
  • Pagination of feed

Tech stack

Web framework - FastAPI
Database - PostgreSQL
ORM - Sqlalchemy
Database migration tool - Alembic


Requirements

Python version 3.9
PostgreSQL version 14



Setting up and Installation

Cloning Repository

Clone this repository by running in your terminal-

git clone [email protected]:Dhruv9449/Chitros.git

or

git clone https://github.com/Dhruv9449/Chitros.git


Configuring Environment Variables

Configure your environment variables by creating a .local(if you want to run development server locally) and a .production(if you want to deploy in production) file as shown in .example in the Chitros/.env/ directory for docker.

For running it without docker, create a local.env file in the Chitros/ directory.

Database variables

  • DATABASE_USERNAME - Database username.
  • DATABASE_SERVER - Database server name.
  • DATABASE_PASSWORD - Database password.
  • DATABASE_HOSTNAME - Database host name.
  • DATABASE_NAME - Database name.
  • DATABASE_PORT - Database port(Usually 5432).

Postgres database variables(for docker)

  • POSTGRES_USER - Postgres database username.
  • POSTGRES_PASSWORD - Postgres database password.
  • POSTGRES_DB - Postgres database name.
  • POSTGRES_HOST - Postgres database host name.
  • POSTGRES_SERVER - Postgres database server name.
  • POSTGRES_PORT - Postgres database port(Usually 5432).

JWT authentication variables

  • SECRET_KEY - Secret SSH key encoding your JWTs.
  • ALGORITHM - Algorithm for encoding JWTs.
  • ACCESS_TOKEN_EXPIRE_MINUTES - The time after which a JWT should expire.

CORS variables

  • CORS_ORIGIN_WHITELIST - Whitelisted URLs from which the API can receive requests.

Use .env.example for reference.



Using Docker

Running locally

To run the server locally to view changes using hot reload, use the command -

docker compose -f docker-compose-dev.yml up --build

This should start a local development server at 0.0.0.0:8000



Deploying in production

To deploy the server in production, use the following command -

docker compose up --build

This should start production server, listening to requests at 0.0.0.0:80



Without using Docker

Setting up Virtual Environment

Install virtualenv and set up a virtual environment in the working directory using the following commands -

pip install virtualenv
virtualenv venv

Now, activate the virtual environment -

Windows

C:\Users\Username\Chitros> .\venv\Scripts\activate

Linux

user@hostname:~/Chitros$ source venv/bin/activate

Refer to the official virtualenv documentation for any further help.



Installing dependencies

To install all the dependancies for this project run the following command in your terminal -

pip install -r requirements.txt


Migrating changes to the database

Before running the server we need to migrate the changes made to the database using alembic. Run the following command -

alembic upgrade head

This should create the necessary changes to the database.



Run the server!

We can start the ASGI uvicorn server with the following commands -

uvicorn app.main:app

Note: You need to be in the Chitros/ directory while running this command.



Using the API

  • Data is received in the form of Form Fields for creation and updation.
  • Responses are in form of JSON.

Interactive documentation

You can try out the API using the interactive docs that are generated by Swagger UI. These can be accessed using the URL - http://127.0.0.1:8000/docs or http://localhost/docs

Note- You can also try out the API in the existing deployment at https://chitros.dhruvshah.ml/docs.



Thunder Client

If you are using VS code then you can also try out the API using the Thunder Client extension.

  • Install the extension and then you can import the thunderclient environment from Chitros/thunderclient/thunder-environment_Chitros.json.
  • After this you can import the thunderclient requests collection from Chitros/thunderclient/thunder-collection_Chitros.json.


Frontend Integration

If you want to use this API and build a frontend for it, you might want to use the sqlite3 database instead of postgres, to do that comment out the code under # For postgres and uncomment the code under # For sqlite3 in Chitros/app/db/db_setup.py and Chitros/app/db/models.py.

You will also add the frontend URL in the CORS_ORIGIN_WHITELIST in the environment variables file.
For example, for react you would set CORS_ORIGIN_WHITELIST=http://localhost:3000, http://localhost:3001.



License

Copyright © 2022 Dhruv9449
MIT License



Developed with ❤️ by Dhruv Shah

About

Chitros is a social media backend API made using FastAPI

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages