Try it here - https://chitros.dhruvshah.ml/docs
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
Web framework - FastAPI
Database - PostgreSQL
ORM - Sqlalchemy
Database migration tool - Alembic
Python version 3.9
PostgreSQL version 14
Clone this repository by running in your terminal-
git clone [email protected]:Dhruv9449/Chitros.git
or
git clone https://github.com/Dhruv9449/Chitros.git
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_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_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).
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_ORIGIN_WHITELIST
- Whitelisted URLs from which the API can receive requests.
Use .env.example
for reference.
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
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
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 -
C:\Users\Username\Chitros> .\venv\Scripts\activate
user@hostname:~/Chitros$ source venv/bin/activate
Refer to the official virtualenv documentation for any further help.
To install all the dependancies for this project run the following command in your terminal -
pip install -r requirements.txt
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.
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.
- Data is received in the form of Form Fields for creation and updation.
- Responses are in form of JSON.
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
.
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
.
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
.
Copyright © 2022 Dhruv9449
MIT License
Developed with ❤️ by Dhruv Shah