diff --git a/.gitignore b/.gitignore deleted file mode 100644 index e3e3fcf..0000000 --- a/.gitignore +++ /dev/null @@ -1,182 +0,0 @@ -# ---> Python -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# UV -# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -#uv.lock - -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/latest/usage/project/#working-with-version-control -.pdm.toml -.pdm-python -.pdm-build/ - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ - -# Cython debug symbols -cython_debug/ - -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ - -# Ruff stuff: -.ruff_cache/ - -# PyPI configuration file -.pypirc - -<<<<<<< HEAD -======= -# MacOS annoying file -.DS_Store - ->>>>>>> 341c325 (restored) diff --git a/README.md b/README.md deleted file mode 100644 index 9f5bf7a..0000000 --- a/README.md +++ /dev/null @@ -1,113 +0,0 @@ -# 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. Snake 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 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/B2B/__init__.py b/app/api/v1/B2B/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/api/v1/B2B/admin.py b/app/api/v1/B2B/admin.py new file mode 100644 index 0000000..a28d18a --- /dev/null +++ b/app/api/v1/B2B/admin.py @@ -0,0 +1,36 @@ +from fastapi import APIRouter, Query, Header + + +router = APIRouter( + prefix="/b2b", + tags=["b2b"] +) + + +@router.post("/products/{id}") +def create_product( + id: int, + token: str = Header(..., description="Authorization token", alias="Authorization"), + ) -> dict: + pass + +@router.put("/products/{id}") +def update_product( + id: int, + token: str = Header(..., description="Authorization token", alias="Authorization"), + ) -> dict: + pass + +@router.delete("/products/{id}") +def delete_product( + id: int, + token: str = Header(..., description="Authorization token", alias="Authorization"), + ) -> dict: + pass + +@router.post("/products/{id}/related") +def create_related( + id: int, + token: str = Header(..., description="Authorization token", alias="Authorization"), + ) -> dict: + pass diff --git a/app/api/v1/B2C/__init__.py b/app/api/v1/B2C/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/api/v1/B2C/products.py b/app/api/v1/B2C/products.py new file mode 100644 index 0000000..3755f04 --- /dev/null +++ b/app/api/v1/B2C/products.py @@ -0,0 +1,61 @@ +from fastapi import APIRouter, Query, Header + + +router = APIRouter( + prefix="/b2c", + tags=["b2c"] +) + + +@router.get("/products") +def get_products( + page: str = Query(..., description="page", alias="Page"), + limit: str = Query(..., description="limit of the items", alias="Limit"), + ) -> list[dict]: + # cache goes here + pass + +@router.get("/products/{id}") +def get_product_by_id( + id: int, + page: str = Query(..., description="page", alias="Page"), + limit: str = Query(..., description="limit of the items", alias="Limit"), + ) -> list[dict]: + # cache goes here + pass + +@router.get("/products/search") +def search( + search_query: str = Query(..., description="Search query"), + filters: str = Query(..., description="Filters"), + categories: str = Query(..., description="Categories") + ) -> list[dict]: + # cache goes here + pass + +@router.get("/products/{id}/related") +def get_related( + id: int, + page: str = Query(..., description="page", alias="Page"), + limit: str = Query(..., description="limit of the items", alias="Limit"), + ) -> list[dict]: + # cache goes here + pass + +@router.get("/products/featured") +def get_promo( + page: str = Query(..., description="page", alias="Page"), + limit: str = Query(..., description="limit of the items", alias="Limit"), + ) -> list[dict]: + # cache goes here + pass + +@router.get("/discounted") +def get_discount( + page: str = Query(..., description="page", alias="Page"), + limit: str = Query(..., description="limit of the items", alias="Limit"), + ) -> list[dict]: + # cache goes here + pass + + 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/__init__.py b/app/core/__init__.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/repositories/__init__.py b/app/repositories/__init__.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/utils/__init__.py b/app/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main.py b/main.py index e69de29..2426fd6 100644 --- a/main.py +++ b/main.py @@ -0,0 +1,20 @@ +from fastapi import FastAPI +from fastapi.responses import JSONResponse +from dotenv import load_dotenv +import uvicorn +import os + +from app.api.v1.B2C import products +from app.api.v1.B2B import admin + +app = FastAPI() +app.include_router(products.router) +app.include_router(admin.router) + +@app.get("/ping") +def ping(): + return {"status": "ok"} + +if __name__ == "__main__": + uvicorn.run("main:app", host="127.0.0.1", port=8000) + \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.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