diff --git a/.gitignore b/.gitignore index 36b13f1..e3e3fcf 100644 --- a/.gitignore +++ b/.gitignore @@ -174,3 +174,9 @@ cython_debug/ # PyPI configuration file .pypirc +<<<<<<< HEAD +======= +# MacOS annoying file +.DS_Store + +>>>>>>> 341c325 (restored) diff --git a/Dockerfile.txt b/Dockerfile.txt new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index 1548803..1a54b6f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,118 @@ +<<<<<<< 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) 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/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