Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rylew1 committed Apr 4, 2024
1 parent b34be60 commit 8b0c3e2
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 7 deletions.
57 changes: 54 additions & 3 deletions app/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pytest-watch = "^4.2.0"
pytest-lazy-fixture = "^0.6.3"
types-pyyaml = "^6.0.12.11"
setuptools = "^68.2.2"
debugpy = "^1.8.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
23 changes: 23 additions & 0 deletions app/src/api/debug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from pydantic import Field

from src.util.env_config import PydanticBaseEnvConfig


class DebugAdapterProtocolConfig(PydanticBaseEnvConfig):
enable: bool = Field(
default=False,
description="Enable debug mode. Overwritten to True when running a debug make target",
)
host: str = Field(default="0.0.0.0", description="The host for the debugger to listen on")
port: int = Field(default=5678, description="The port for the debugger to listen on")

wait_for_client: bool = Field(
default=True, description="Whether to wait for the debugger client to attach"
)
wait_for_client_message: str = Field(
default="Waiting for remote attach debugger...",
description="Message to log when waiting for debugger",
)

class Config:
env_prefix = "DEBUG_ADAPTER_PROTOCOL_"
36 changes: 32 additions & 4 deletions app/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@
import os
from typing import Optional

from apiflask import APIFlask
from flask import g
from werkzeug.exceptions import Unauthorized

import src.adapters.db as db
import src.adapters.db.flask_db as flask_db
import src.logging
import src.logging.flask_logger as flask_logger
from apiflask import APIFlask
from flask import g
from src.api.debug import DebugAdapterProtocolConfig
from src.api.healthcheck import healthcheck_blueprint
from src.api.schemas import response_schema
from src.api.users import user_blueprint
from src.auth.api_key_auth import User, get_app_security_scheme
from werkzeug.exceptions import Unauthorized

logger = logging.getLogger(__name__)


def create_app() -> APIFlask:
app = APIFlask(__name__)

print('app => ', app)

if is_running_in_local():
setup_debug_adapter()

src.logging.init(__package__)
flask_logger.init_app(logging.root, app)

Expand All @@ -34,6 +39,29 @@ def create_app() -> APIFlask:
return app


def is_running_in_local() -> bool:
print( 's.getenv("FLASK_ENV") => ', os.getenv("FLASK_ENV"))
return os.getenv("FLASK_ENV") in ("local", "dev")


def setup_debug_adapter() -> None:
debug_adapter_protocol_config = DebugAdapterProtocolConfig()

if debug_adapter_protocol_config.enable and os.environ.get("WERKZEUG_RUN_MAIN"):
# flask can initialize multiple times - only debugpy.listen() once
import debugpy

debugpy.listen(
(
debug_adapter_protocol_config.host,
debug_adapter_protocol_config.port,
)
)
if debug_adapter_protocol_config.wait_for_client:
logger.info(debug_adapter_protocol_config.wait_for_client_message)
debugpy.wait_for_client() # blocks execution until debugger is attached


def current_user(is_user_expected: bool = True) -> Optional[User]:
current = g.get("current_user")
if is_user_expected and current is None:
Expand Down

0 comments on commit 8b0c3e2

Please sign in to comment.