Skip to content

FastAPI REST web application for managing "Post" entities, built using object-oriented principles. It includes endpoints for creating, reading, updating, and deleting posts.

Notifications You must be signed in to change notification settings

vinodvpillai/fastapi-best-practices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI REST Web Application

This project is a FastAPI REST web application for managing "Post" entities, built using object-oriented principles. It includes endpoints for creating, reading, updating, and deleting posts.

Project Structure

.
├── .env
├── app
│   ├── __init__.py
│   ├── dto
│   │   ├── __init__.py
│   │   ├── post_dto.py
│   ├── model
│   │   ├── __init__.py
│   │   ├── post.py
│   ├── repository
│   │   ├── __init__.py
│   │   ├── post_repository.py
│   ├── service
│   │   ├── __init__.py
│   │   ├── post_service.py
│   ├── controller
│   │   ├── __init__.py
│   │   ├── post_controller.py
│   ├── core
│   │   ├── __init__.py
│   │   ├── logger_config.py
│   │   ├── database.py
│   │   ├── log_middleware.py
        ├── env_settings.py
│   └── main.py

Getting Started

Prerequisites

  • Python 3.7+
  • FastAPI
  • SQLAlchemy
  • Pydantic
  • Uvicorn
  • Starlette
  • A database variable specified in a .env file

Installation

  1. Clone the repository:

    git clone https://github.com/yourusername/fastapi-posts.git
    cd fastapi-posts
  2. Create and activate a virtual environment:

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

    pip install -r requirements.txt
  4. Create a .env file:

    echo "DATABASE_PORT=5432" > .env

Running the Application

To run the application, use the following command:

uvicorn app.main:app --reload

The application will be available at http://127.0.0.1:8000.

Project Components

DTO Classes

Located in the app/dto/post_dto.py file, these classes handle the data transfer objects for requests and responses, validated using Pydantic.

Model Classes

Located in the app/model/post.py file, these classes define the database schema using SQLAlchemy.

Database Configuration

Located in the app/core/database.py file, this class handles the database engine and session creation, using settings loaded from a .env file.

Repository Classes

Located in the app/repository/post_repository.py file, these classes handle database operations like insert, update, delete, and select.

Service Classes

Located in the app/service/post_service.py file, these classes contain business logic and interact with repository classes to perform operations.

Controller Classes

Located in the app/controller/post_controller.py file, these classes define the FastAPI endpoints for the application.

Middleware

Custom logging middleware is located in the app/core/log_middleware.py file. CORS middleware is added in app/main.py.

Example Usage

Create a Post

curl -X 'POST' \
  'http://127.0.0.1:8000/posts/' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "title": "My First Post",
  "content": "This is the content of my first post."
}'

Get a Post

curl -X 'GET' \
  'http://127.0.0.1:8000/posts/1' \
  -H 'accept: application/json'

Update a Post

curl -X 'PUT' \
  'http://127.0.0.1:8000/posts/1' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "title": "Updated Post Title",
  "content": "Updated content of my post."
}'

Delete a Post

curl -X 'DELETE' \
  'http://127.0.0.1:8000/posts/1' \
  -H 'accept: application/json'

Get All Posts

curl -X 'GET' \
  'http://127.0.0.1:8000/posts/' \
  -H 'accept: application/json'

License

This project is licensed under the MIT License.

Acknowledgements

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss changes.


This README file provides a comprehensive guide to setting up and using the FastAPI REST web application, covering all necessary components and usage examples.

About

FastAPI REST web application for managing "Post" entities, built using object-oriented principles. It includes endpoints for creating, reading, updating, and deleting posts.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages