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.
.
├── .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
- Python 3.7+
- FastAPI
- SQLAlchemy
- Pydantic
- Uvicorn
- Starlette
- A database variable specified in a
.env
file
-
Clone the repository:
git clone https://github.com/yourusername/fastapi-posts.git cd fastapi-posts
-
Create and activate a virtual environment:
python -m venv env source env/bin/activate # On Windows use `env\Scripts\activate`
-
Install the dependencies:
pip install -r requirements.txt
-
Create a
.env
file:echo "DATABASE_PORT=5432" > .env
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
.
Located in the app/dto/post_dto.py
file, these classes handle the data transfer objects for requests and responses, validated using Pydantic.
Located in the app/model/post.py
file, these classes define the database schema using SQLAlchemy.
Located in the app/core/database.py
file, this class handles the database engine and session creation, using settings loaded from a .env
file.
Located in the app/repository/post_repository.py
file, these classes handle database operations like insert, update, delete, and select.
Located in the app/service/post_service.py
file, these classes contain business logic and interact with repository classes to perform operations.
Located in the app/controller/post_controller.py
file, these classes define the FastAPI endpoints for the application.
Custom logging middleware is located in the app/core/log_middleware.py
file. CORS middleware is added in app/main.py
.
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."
}'
curl -X 'GET' \
'http://127.0.0.1:8000/posts/1' \
-H 'accept: application/json'
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."
}'
curl -X 'DELETE' \
'http://127.0.0.1:8000/posts/1' \
-H 'accept: application/json'
curl -X 'GET' \
'http://127.0.0.1:8000/posts/' \
-H 'accept: application/json'
This project is licensed under the MIT License.
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.