Skip to content

Commit

Permalink
add lifespan function capability + small things
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Karamanis committed Dec 3, 2024
1 parent c3b0bac commit 0d75e39
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion neu_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.4"
__version__ = "0.1.5"
1 change: 1 addition & 0 deletions neu_sdk/config/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def setup_logging(
console = Console(width=terminal_width) if terminal_width else None
rich_handler = RichHandler(
show_time=show_time,
show_level=True,
rich_tracebacks=rich_tracebacks,
tracebacks_show_locals=tracebacks_show_locals,
tracebacks_word_wrap=tracebacks_word_wrap,
Expand Down
22 changes: 19 additions & 3 deletions neu_sdk/interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from contextlib import asynccontextmanager
from datetime import UTC, datetime
from typing import Callable
from uuid import uuid4

from aredis_om import Migrator
Expand All @@ -11,22 +12,37 @@
from neu_sdk.registry import deregister_service, register_service


def create_app(service_name: str, app_version: str, schema_version: str, tags: list[str] = []):
def create_app(
service_name: str,
app_version: str,
schema_version: str,
tags: list[str] = [],
lifespan_before: list[Callable] = [],
lifespan_after: list[Callable] = [],
):
service_id = uuid4()

@asynccontextmanager
async def lifespan(app):
if settings.neu.devMode:
LOGGER.warning("You are working on developer mode")
assert await register_service(service_id=service_id, service_name=service_name, tags=tags)
assert await register_service(
service_id=service_id, service_name=service_name, tags=tags
)
await Migrator().run()
for f in lifespan_before:
await f
yield
await deregister_service(service_id=service_id)
for f in lifespan_after:
await f

app = FastAPI(
debug=settings.neu.devMode,
title=service_name,
docs_url=(settings.neu.service.docs.url if settings.neu.service.docs.enable else None),
docs_url=(
settings.neu.service.docs.url if settings.neu.service.docs.enable else None
),
redoc_url=None,
version=app_version,
license_info={
Expand Down
20 changes: 10 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ classifiers = [
]

dependencies = [
"pyjwt==2.10.1",
"bcrypt==4.2.1",
"python-dotenv==1.0.1",
"pydantic==2.10.2",
"pydantic-settings==2.6.1",
"rich==13.9.4",
"aiohttp==3.11.8",
"aiodns==3.2.0",
"deprecated==1.2.15",
"pyjwt>=2.10.1,<3.0.0",
"bcrypt>=4.2.1,<5.0.0",
"python-dotenv>=1.0.1,2.0.0",
"pydantic>=2.10.2,<3.0.0",
"pydantic-settings>=2.6.1,<3.0.0",
"rich>=13.9.4<14.0.0",
"aiohttp>=3.11.8,<4.0.0",
"aiodns>=3.2.0,<4.0.0",
"deprecated>=1.2.15<2.0.0",
]

[project.urls]
Expand All @@ -48,7 +48,7 @@ path = "neu_sdk/__init__.py"
[tool.hatch.envs.lint]
detached = true
require = ["default"]
dependencies = ["mypy>=1.13.0", "ruff>=0.7.2"]
dependencies = ["mypy", "ruff"]

[tool.hatch.envs.lint.scripts]
typing = "mypy --install-types --non-interactive {args:neu_sdk tests}"
Expand Down

0 comments on commit 0d75e39

Please sign in to comment.