From 97567209e1872fa03779ff14fe85e0094495364e Mon Sep 17 00:00:00 2001 From: berkay Date: Sat, 15 Feb 2025 17:26:21 +0300 Subject: [PATCH] test api build --- .idea/.gitignore | 3 + .idea/inspectionProfiles/Project_Default.xml | 35 +++++++++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/test-dummy-api.iml | 8 ++ .idea/vcs.xml | 6 ++ Dockerfile | 30 ++++++++ app.py | 59 ++++++++++++++ docker-compose.yml | 7 ++ pyproject.toml | 76 +++++++++++++++++++ 11 files changed, 245 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/test-dummy-api.iml create mode 100644 .idea/vcs.xml create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 docker-compose.yml create mode 100644 pyproject.toml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..4129666 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,35 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..24918cf --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c275a89 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/test-dummy-api.iml b/.idea/test-dummy-api.iml new file mode 100644 index 0000000..585fdc6 --- /dev/null +++ b/.idea/test-dummy-api.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1bf529a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +FROM python:3.12-slim + +WORKDIR /app + +# Install system dependencies and Poetry +RUN apt-get update \ + && apt-get install -y --no-install-recommends \ + gcc \ + && rm -rf /var/lib/apt/lists/* \ + && pip install --no-cache-dir poetry + +# Copy Poetry configuration +COPY /pyproject.toml ./pyproject.toml + +# Configure Poetry and install dependencies with optimizations +RUN poetry config virtualenvs.create false \ + && poetry install --no-interaction --no-ansi --no-root --only main \ + && pip cache purge \ + && rm -rf ~/.cache/pypoetry + +# Copy application code +COPY . . + +# Set Python path to include app directory +ENV PYTHONPATH=/app \ + PYTHONUNBUFFERED=1 \ + PYTHONDONTWRITEBYTECODE=1 + +# Run the application using the configured uvicorn server +CMD ["poetry", "run", "python", "app.py"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..be17d33 --- /dev/null +++ b/app.py @@ -0,0 +1,59 @@ +""" +FastAPI Application Entry Point + +This module initializes and configures the FastAPI application with: +- CORS middleware for cross-origin requests +- Request timing middleware for performance monitoring +- Custom exception handlers for consistent error responses +- Prometheus instrumentation for metrics +- API routers for endpoint organization +""" + +import uvicorn +from arrow import now + +from fastapi import FastAPI +from fastapi.responses import JSONResponse, RedirectResponse + + +app = FastAPI( + title="Dummy api", + description="Api from test purposes", + default_response_class=JSONResponse, +) # Initialize FastAPI app + + +@app.get(path="/current/date", summary="Receive token via device id and encrypt data") +def receive_api_current_date(): + """ + Output + * utc_string: str + * utc_float_timestamp: float + * utc_timezone: Optional[str] + * current_string: str + * current_float_timestamp: float + * current_timezone: Optional[str] + * source: str + * source_address: str + """ + return dict( + utc_string=str(now()), + utc_float_timestamp=now().float_timestamp, + utc_timezone=now().tzname(), + current_string=now().shift(hours=+3).__str__(), + current_float_timestamp=now().shift(hours=+3).float_timestamp, + current_timezone="GMT +3", + source="TITLE", + source_address="Ankara/Turkey", + ) + +if __name__ == "__main__": + # Run the application with Uvicorn + config_dict = dict( + app="app:app", + host = "0.0.0.0", + port = 8000, + log_level = "info", + reload = True, + ) + uvicorn.Server(uvicorn.Config(**config_dict)).run() diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..8127a01 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,7 @@ +services: + + dummy-api-service: + build: + context: . + ports: + - "8000:8000" diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..bbe7404 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,76 @@ +[tool.poetry] +name = "wag-management-api-services" +version = "0.1.0" +description = "WAG Management API Services" +authors = ["Karatay Berkay "] + +[tool.poetry.dependencies] +python = "^3.12" +# FastAPI and Web +fastapi = "^0.104.1" +uvicorn = "^0.24.0" +pydantic = "^2.5.2" + +# MongoDB +motor = "3.3.2" # Pinned version +pymongo = "4.5.0" # Pinned version to match motor + +# PostgreSQL +sqlalchemy = "^2.0.23" +sqlalchemy-mixins = "^2.0.5" +psycopg2-binary = "^2.9.9" + +# Redis +redis = "^5.0.1" +arrow = "^1.3.0" + +# Email +redmail = "^0.6.0" + +# Testing +pytest = "^7.4.3" +pytest-asyncio = "^0.21.1" +pytest-cov = "^4.1.0" + +# Utilities +python-dateutil = "^2.8.2" +typing-extensions = "^4.8.0" + +[tool.poetry.group.dev.dependencies] +black = "^23.11.0" +isort = "^5.12.0" +mypy = "^1.7.1" +flake8 = "^6.1.0" + +[build-system] +requires = ["poetry-core>=1.0.0"] +build-backend = "poetry.core.masonry.api" + +[tool.black] +line-length = 88 +target-version = ['py39'] +include = '\.pyi?$' + +[tool.isort] +profile = "black" +multi_line_output = 3 +include_trailing_comma = true +force_grid_wrap = 0 +use_parentheses = true +line_length = 88 + +[tool.mypy] +python_version = "3.9" +warn_return_any = true +warn_unused_configs = true +disallow_untyped_defs = true +check_untyped_defs = true + +[tool.pytest.ini_options] +minversion = "6.0" +addopts = "-ra -q --cov=Services" +testpaths = [ + "Ztest", +] +python_files = ["test_*.py"] +asyncio_mode = "auto"