Files
invoke-ai--invokeai/invokeai/app/services/app_settings/app_settings_service.py
T
wehub-resource-sync cddb07a176
docs / deploy (push) Has been cancelled
docs / changes (push) Has been cancelled
docs / check-and-build (push) Has been cancelled
build container image / cpu (push) Has been cancelled
build container image / cuda (push) Has been cancelled
build container image / rocm (push) Has been cancelled
frontend checks / frontend-checks (push) Has been cancelled
frontend tests / frontend-tests (push) Has been cancelled
lfs checks / lfs-check (push) Has been cancelled
python checks / python-checks (push) Has been cancelled
python tests / py3.12: macos-default (push) Has been cancelled
python tests / py3.11: windows-cpu (push) Has been cancelled
python tests / py3.12: windows-cpu (push) Has been cancelled
python tests / py3.11: linux-cpu (push) Has been cancelled
typegen checks / typegen-checks (push) Has been cancelled
uv lock checks / uv-lock-checks (push) Has been cancelled
openapi checks / openapi-checks (push) Has been cancelled
python tests / py3.11: macos-default (push) Has been cancelled
python tests / py3.12: linux-cpu (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:22:06 +08:00

75 lines
2.3 KiB
Python

"""Service for managing application-level settings stored in the database."""
from typing import Optional
from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase
class AppSettingsService:
"""Service for accessing application-level settings from the database.
This service provides a simple key-value store for application-level configuration
that needs to be persisted across restarts, such as JWT secrets.
"""
def __init__(self, db: SqliteDatabase) -> None:
"""Initialize the app settings service.
Args:
db: The SQLite database instance
"""
self._db = db
def get(self, key: str) -> Optional[str]:
"""Get a setting value by key.
Args:
key: The setting key
Returns:
The setting value if found, None otherwise
"""
try:
with self._db.transaction() as cursor:
cursor.execute("SELECT value FROM app_settings WHERE key = ?;", (key,))
row = cursor.fetchone()
return row[0] if row else None
except Exception:
return None
def set(self, key: str, value: str) -> None:
"""Set a setting value.
Args:
key: The setting key
value: The setting value
"""
with self._db.transaction() as cursor:
cursor.execute(
"""
INSERT INTO app_settings (key, value)
VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET
value = excluded.value,
updated_at = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW');
""",
(key, value),
)
def get_jwt_secret(self) -> str:
"""Get the JWT secret key from the database.
Returns:
The JWT secret key
Raises:
RuntimeError: If the JWT secret is not found in the database
"""
secret = self.get("jwt_secret")
if secret is None:
raise RuntimeError(
"JWT secret not found in database. This should have been created during database migration. "
"Please ensure database migrations have been run successfully."
)
return secret