Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cat as singleton #520

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion core/cat/looking_glass/cheshire_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
MSG_TYPES = Literal["notification", "chat", "error", "chat_token"]

# main class
class CheshireCat:
class CheshireCat():
"""The Cheshire Cat.

This is the main class that manages everything.
Expand All @@ -40,6 +40,14 @@ class CheshireCat:

"""

_instance = None

def __new__(cls):
if not cls._instance:
cls._instance = super().__new__(cls)
cls._instance = CheshireCat()
return cls._instance

def __init__(self):
"""Cat initialization.

Expand Down Expand Up @@ -532,3 +540,5 @@ def get_static_path():
"""Allows the Cat expose the static files path."""
log.warning("This method will be removed, import cat.utils tu usit instead.")
return utils.get_static_path()

cat = CheshireCat()
4 changes: 2 additions & 2 deletions core/cat/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from cat.routes.static import public, admin, static
from cat.api_auth import check_api_key
from cat.routes.openapi import get_openapi_configuration_function
from cat.looking_glass.cheshire_cat import CheshireCat
from cat.looking_glass.cheshire_cat import cat


@asynccontextmanager
Expand All @@ -26,7 +26,7 @@ async def lifespan(app: FastAPI):
# - Not using midlleware because I can't make it work with both http and websocket;
# - Not using Depends because it only supports callables (not instances)
# - Starlette allows this: https://www.starlette.io/applications/#storing-state-on-the-app-instance
app.state.ccat = CheshireCat()
app.state.ccat = cat

# startup message with admin, public and swagger addresses
log.welcome()
Expand Down
Loading