This project is a demonstration of a FastAPI application with PostgreSQL database integration, including user authentication, CRUD operations for posts, and post voting functionality. It also includes automated tests using Pytest and database migrations managed by Alembic.
- User API authentication
- CRUD operations for posts
- Voting system for posts
- Pytest for automated testing
- Alembic for database migrations
-
Install Docker on your system if you haven't already. Refer to the official Docker documentation for installation instructions specific to your platform.
-
Clone this repository:
git clone https://github.com/hemantgiri/fastapi-post-voting.git
-
Navigate to the project directory:
cd yourproject -
Create a
.envfile in the project directory with the following variables:DATABASE_HOSTNAME=localhost DATABASE_PORT=5432 DATABASE_PASSWORD= DATABASE_NAME= DATABASE_USERNAME= SECRET_KEY=YOUR_SECRET_KEY_HERE ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30- Replace
YOUR_SECRET_KEY_HEREwith a securely generated secret key. You can generate one using :
openssl rand -hex 32 SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"check this https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/ for more info.
- Replace
-
Build the Docker image:
docker build -t yourproject . -
Run the Docker container:
docker run -d --name container_name -p 8000:8000 image_name
-
Access the API at
http://localhost:8000.
-
Clone this repository:
git clone https://github.com/hemantgiri/fastapi-post-voting.git
-
Navigate to the project directory:
cd yourproject -
Create a virtual environment:
python3 -m venv venv
-
Activate the virtual environment:
source venv/bin/activate -
Install dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the project directory with the following variables:DATABASE_HOSTNAME=localhost DATABASE_PORT=5432 DATABASE_PASSWORD= DATABASE_NAME= DATABASE_USERNAME= SECRET_KEY=YOUR_SECRET_KEY_HERE ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30- Replace
YOUR_SECRET_KEY_HEREwith a securely generated secret key. You can generate one using :
openssl rand -hex 32 SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"check this https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/ for more info.
- Replace
-
Start the Uvicorn server:
uvicorn app.main:app --reload
-
Access the API at
http://localhost:8000.
Once the application is running, you can interact with the API endpoints using tools like cURL, Postman, or your preferred HTTP client.
To run the automated tests:
-
Navigate to the project directory if you're not already there:
cd yourproject -
Run Pytest:
pytest
Contributions are welcome! Please feel free to submit a pull request or open an issue if you find any bugs or have suggestions for improvements.