https://fast-api-blog.vercel.app/docs
A simple, scalable, and efficient blog application built using FastAPI. This project demonstrates the power of FastAPI for creating RESTful APIs and serves as a foundation for further development.This is a FastAPI-based Blog API that provides secure access to blog posts using OAuth2 and JWT token authentication. Only authenticated users can access and manage blog content.
- User authentication and authorization (JWT-based).
- CRUD operations for blog posts.
- Comment system for posts.
- Pagination for listing posts.
- Swagger UI and ReDoc for API documentation.
- Modular architecture for scalability and maintainability.
- Integration with a database (PostgreSQL or SQLite).
- Backend Framework: FastAPI
- Database: SQLAlchemy with PostgreSQL (or SQLite for development).
- Authentication: JSON Web Tokens (JWT).
- Asynchronous Programming: Powered by Python’s asyncio.
- Documentation: Automatic API docs with Swagger UI and ReDoc.
- Testing: pytest for testing.
- Python 3.9 or higher
- PostgreSQL (optional, can use SQLite for development)
-
Clone the repository:
git clone https://github.com/your-username/fastapi-blog.git cd fastapi-blog
-
Create a virtual environment and activate it:
python -m venv env source env/bin/activate # On Windows: env\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Configure environment variables:
Create a
.env
file in the root directory and add the following:DATABASE_URL=postgresql+asyncpg://username:password@localhost/db_name SECRET_KEY=your_secret_key ALGORITHM=HS256 ACCESS_TOKEN_EXPIRE_MINUTES=30
Replace
username
,password
, anddb_name
with your PostgreSQL credentials. -
Run database migrations:
alembic upgrade head
-
Start the development server:
uvicorn app.main:app --reload
-
Access the API documentation:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
fastapi-blog/
├── app/
├── blog/
│ ├── main.py # Entry point of the application
│ ├── models/ # Database models
│ ├── schemas/ # Pydantic models
│ ├── routers/ # API routes
│ ├── repositories/ # Business logic
│ └── tests/ # Test cases
├── main.py/ # Entry Point
├── .env # Environment variables
├── requirements.txt # Python dependencies
├── README.md # Project documentation
└── vercel.json
- POST
/auth/login
- User login - POST
/auth/register
- User registration
- GET
/posts/
- Get a list of posts (with pagination) - POST
/posts/
- Create a new post - GET
/posts/{post_id}
- Get a single post - PUT
/posts/{post_id}
- Update a post - DELETE
/posts/{post_id}
- Delete a post
- POST
/posts/{post_id}/comments/
- Add a comment to a post - GET
/posts/{post_id}/comments/
- Get comments for a post
To run the test suite:
pytest
-
Configure a production database and update the
.env
file. -
Use a production server like
gunicorn
with an ASGI server such asuvicorn
:gunicorn -k uvicorn.workers.UvicornWorker app.main:app
-
Set up a reverse proxy (e.g., Nginx) for better performance and security.
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature-name
). - Commit your changes (
git commit -m 'Add a new feature'
). - Push to the branch (
git push origin feature-name
). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE
file for details.
Note
Future enhancements may include role-based access control (RBAC), comments, and category filtering. Contributions and suggestions are welcome!
Happy Coding!