Skip to content

sachingupta0019/FastAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 

Repository files navigation

FastAPI Blog API Project

 https://fast-api-blog.vercel.app/docs

Overview

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.

Features

  • 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).

Screenshots

API Documentation Home

image

API Endpoints

image

Technologies Used

  • 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.

Prerequisites

  • Python 3.9 or higher
  • PostgreSQL (optional, can use SQLite for development)

Installation

  1. Clone the repository:

    git clone https://github.com/your-username/fastapi-blog.git
    cd fastapi-blog
  2. Create a virtual environment and activate it:

    python -m venv env
    source env/bin/activate  # On Windows: env\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt
  4. 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, and db_name with your PostgreSQL credentials.

  5. Run database migrations:

    alembic upgrade head
  6. Start the development server:

    uvicorn app.main:app --reload
  7. Access the API documentation:

Project Structure

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         

API Endpoints

Authentication

  • POST /auth/login - User login
  • POST /auth/register - User registration

Blog Posts

  • 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

Comments

  • POST /posts/{post_id}/comments/ - Add a comment to a post
  • GET /posts/{post_id}/comments/ - Get comments for a post

Running Tests

To run the test suite:

pytest

Deployment

  1. Configure a production database and update the .env file.

  2. Use a production server like gunicorn with an ASGI server such as uvicorn:

    gunicorn -k uvicorn.workers.UvicornWorker app.main:app
  3. Set up a reverse proxy (e.g., Nginx) for better performance and security.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-name).
  3. Commit your changes (git commit -m 'Add a new feature').
  4. Push to the branch (git push origin feature-name).
  5. Open a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Notes

Note

Future enhancements may include role-based access control (RBAC), comments, and category filtering. Contributions and suggestions are welcome!

Happy Coding!