diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..dd8c178 Binary files /dev/null and b/.DS_Store differ diff --git a/Dockerfile.txt b/Dockerfile.txt new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index e463524..14c9dc2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,138 @@ -# MicroservicesExample +# Microservices example -simple example. \ No newline at end of file +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 +│ ├── services/ # Business logic layer +│ │ ├── __init__.py +│ │ ├── user_service.py # Example service +│ │ └── ... +│ ├── 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 **services.py** communicates with the repository layer that already has a repository instance injected into the service instance at the endpoint.py file + + ```python + class UserService: + def __init__(self, repository): + self.repository = repository + + async def create_user(self, user_data:UserBaseModel): + return await self.repository.create_user(user_data) + + async def read_users(self): + return await self.repository.get_all_users() + + async def read_user_by_email(self, user_email: str): + return await self.repository.get_user_by_email(user_email) + + async def update_user(self, user_id, user_data: UserBaseModel): + return await self.repository.update_user(user_id, user_data) + + async def delete_user(self, user_id: int): + return await self.repository.delete_user(user_id) + ``` + +4. 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 \ No newline at end of file diff --git a/app/.DS_Store b/app/.DS_Store new file mode 100644 index 0000000..e6c5def Binary files /dev/null and b/app/.DS_Store differ diff --git a/app/api/.DS_Store b/app/api/.DS_Store new file mode 100644 index 0000000..d039ac9 Binary files /dev/null and b/app/api/.DS_Store differ diff --git a/app/api/init.py b/app/api/init.py new file mode 100644 index 0000000..e69de29 diff --git a/app/api/v1/dependencies.py b/app/api/v1/dependencies.py new file mode 100644 index 0000000..e69de29 diff --git a/app/api/v1/endpoints.py b/app/api/v1/endpoints.py new file mode 100644 index 0000000..e69de29 diff --git a/app/api/v1/init.py b/app/api/v1/init.py new file mode 100644 index 0000000..e69de29 diff --git a/app/core/config.py b/app/core/config.py new file mode 100644 index 0000000..e69de29 diff --git a/app/core/init.py b/app/core/init.py new file mode 100644 index 0000000..e69de29 diff --git a/app/core/security.py b/app/core/security.py new file mode 100644 index 0000000..e69de29 diff --git a/app/db/init.py b/app/db/init.py new file mode 100644 index 0000000..e69de29 diff --git a/app/db/session.py b/app/db/session.py new file mode 100644 index 0000000..e69de29 diff --git a/app/repositories/init.py b/app/repositories/init.py new file mode 100644 index 0000000..e69de29 diff --git a/app/repositories/user_repository.py b/app/repositories/user_repository.py new file mode 100644 index 0000000..e69de29 diff --git a/app/schemas/init.py b/app/schemas/init.py new file mode 100644 index 0000000..e69de29 diff --git a/app/schemas/pydantic_schema.py b/app/schemas/pydantic_schema.py new file mode 100644 index 0000000..e69de29 diff --git a/app/services/init.py b/app/services/init.py new file mode 100644 index 0000000..e69de29 diff --git a/app/services/user_service.py b/app/services/user_service.py new file mode 100644 index 0000000..e69de29 diff --git a/app/utils/init.py b/app/utils/init.py new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/init.py b/tests/init.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_main.py b/tests/test_main.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_v1/init.py b/tests/test_v1/init.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_v1/test_endpoints.py b/tests/test_v1/test_endpoints.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_v1/test_models.py b/tests/test_v1/test_models.py new file mode 100644 index 0000000..e69de29