Python(3.10.8) Web Service that allows user authentication using JWT access tokens and all CRUD operations for data persistence to postgres DB with Docker support and CI/CD pipeline.
-
User login (Authenticate using JWT)
- Password hashing using passlib library and bcrypt algorithm
- JWT Token library - python-jose with cryptographic backend pyca/cryptography
-
REST API
- Framework used - FastAPI
- Create/Read/Update/Delete API for Posts (CRUD).
- Pagination support in GET all posts API using
limitandoffsetfrom sqlalchemy ORM features
http://127.0.0.1:8000/posts?limit=5&skip=5 - Search support for post title using query params
http://127.0.0.1:8000/posts?search=yo - Users can Like/Upvote posts. Check Swagger doc
http://127.0.0.1:8000/docsfor API.
In the payload JSONdirectionof1is like and0is unlike. - Request/Response model validation using pydantic
-
Database - Postgres
- ORM - SQLAlchemy, DB driver - Psycopg2
- DB Table Migration/update implemented using alembic.
- DB schemas inside persistence/db_models.py.
-
Configs (Urls, secrets, db connection strings, environment vars)
- All configs are stored in .env file and read in code using python-dotenv pydantic models. In production this file should not be checked in to git to keep passwords and secrets safe
-
Docker Support
- Docker files for
devandproduction. Docker images are currently uploaded to dockerhub riteshmahato/python-fastapi repository. - To run docker containers, just run
docker-compose -f docker-compose-dev.ymlon your localmachine and you're good to use all APIs.
- Docker files for
- Clone the repository
- Install all requirements (create a venv in you want to contain the installations)
pip install -r requirements.txt - Start Postgres Server on you local system
- Create a database named fastapi and owner named postgres OR change settings here as per your db/owner names
- Run the web server locally using uvicorn
python -m uvicorn app.main:app --reload - Navigate to
http://127.0.0.1:8000/docson your browser to get swagger docs for all APIs and their payloads. - The flow is to first create a new user using the Create User API and then Login using the Login API. This will give you the access token. Execute all other APIs by providing the bearer access token as the header.
- Run
docker-compose -f docker-compose-dev.ymland use all APIs athttp://127.0.0.1:8000/docs