Files
MicroservicesExample/README.md
2025-11-29 23:48:40 +03:00

119 lines
4.3 KiB
Markdown

<<<<<<< HEAD
# MicroservicesExample
=======
# Microservices example
This repository needs to be used as a template for all microservices that we are going to have in our project. For any questions, please ask AK (@notmewhyitsalwaysme)
## Core principals:
1. KISS - Keep It Simple
2. DRY - Don't Repeat Yourself
3. Camell case only
4. LONG NAME OF VARIABLE OK, AS FAR AS IT DESCRIBES WHAT THE VARIABLE IS USED FOR
5. DO. NOT. USE. NAMES. SUCH. AS. "a, b, c, d, s" OR SOMETHING LIKE THAT YOUR STUPID BRAIN SUGGESTS
6. Principals 4 and 5 don't override each other. Truth is somewhere in the middle as always.
7. If you unsure how it's needed to be - ask others.
8. DO NOT drop_tables()
## Folder structure:
```
microservice_name/
├── main.py # Entry point for your FastAPI application
├── requirements.txt # Python dependencies
├── Dockerfile.txt # Docker containerfile
├── README.md # Project documentation
├── .gitignore # Define what to ignore during version control
├── app/ # Application directory
│ ├── __init__.py # Initialize the app package
│ ├── api/ # API endpoints
│ │ ├── __init__.py # Initialize the api package
│ │ ├── v1/ # Versioned API endpoints
│ │ │ ├── __init__.py
│ │ │ ├── endpoints.py # Define API routes and handlers
│ │ │ └── dependencies.py # Dependency injection
│ ├── core/ # Core functionality
│ │ ├── __init__.py
│ │ ├── config.py # Application configurations
│ │ ├── security.py # Security related utilities
│ │ └── ...
│ ├── db/ # Database related files
│ │ ├── __init__.py
│ │ ├── session.py # Database session handling
│ │ └── migrations/ # Database migrations
│ ├── repositories/ # database logic layer
│ │ ├── __init__.py
│ │ ├── user_repository.py # Example service
│ │ └── ...
│ ├── utils/ # Utility functions
│ │ ├── __init__.py
│ │ └── ...
│ └── schemas/ # Utility functions
│ ├── __init__.py
│ ├── pydantic_schema.py
│ └── ...
└── tests/ # Test directory
├── __init__.py
├── conftest.py # Test fixtures
├── test_main.py # Tests for main module
└── test_api/ # API endpoint tests
├── __init__.py
├── test_v1/ # Versioned API endpoint tests
│ ├── __init__.py
│ ├── test_endpoints.py
│ └── test_models.py
└── ...
```
### Some explainig:
1. The **endpoint.py** file consists of all your endpoints for your microservice.
2. The **dependencies.py** file is responsible for adding methods that inject dependencies, like the service layer or repository layer.
```python
def get_users_repository(db=Depends(Database)) -> GenericRepository:
return GenericRepository(db)
def get_users_service(repository=Depends(get_users_repository)) -> GenericService:
return GenericService(repository)
```
3. The **repository.py** file is responsible for managing all database operations. The **db** instance was injected again on the dependency layer.
```python
class UserRepository:
def __init__(self, db):
self.db = db
async def get_all_users(self):
...
async def get_user_by_email(self, user_email):
...
async def delete_user(self, user_id):
...
async def create_user(self, user_data):
...
async def update_user(self, user_id, user_data):
...
```
5. **Utils** store utility functions used throughout the application.
6. **Tests** holds test files for unit and integration testing.
7. **Core** contains core functionality such as application configurations and security utilities.
8. The **database.py** and **models.py** contains your sqlalchemy models
>>>>>>> 341c325 (restored)