prepared playbook for prod

This commit is contained in:
Yannic Kilcher
2023-01-15 23:30:16 +01:00
parent cc03376d86
commit 5d441b1570
9 changed files with 69 additions and 44 deletions
+19 -2
View File
@@ -11,7 +11,7 @@ import redis.asyncio as redis
from fastapi_limiter import FastAPILimiter
from fastapi_utils.tasks import repeat_every
from loguru import logger
from oasst_backend.api.deps import get_dummy_api_client
from oasst_backend.api.deps import api_auth, create_api_client
from oasst_backend.api.v1.api import api_router
from oasst_backend.api.v1.utils import prepare_conversation
from oasst_backend.config import settings
@@ -76,6 +76,20 @@ if settings.UPDATE_ALEMBIC:
logger.exception("Alembic upgrade failed on startup")
if settings.OFFICIAL_WEB_API_KEY:
@app.on_event("startup")
def create_official_web_api_client():
with Session(engine) as session:
create_api_client(
session=session,
api_key=settings.OFFICIAL_WEB_API_KEY,
description="The official web client for the OASST backend.",
frontend_type="web",
trusted=True,
)
if settings.RATE_LIMIT:
@app.on_event("startup")
@@ -111,10 +125,13 @@ if settings.DEBUG_USE_SEED_DATA:
role: str
tree_state: Optional[message_tree_state.State]
if not settings.OFFICIAL_WEB_API_KEY:
raise ValueError("Cannot use seed data without OFFICIAL_WEB_API_KEY")
try:
logger.info("Seed data check began")
with Session(engine) as db:
api_client = get_dummy_api_client(db)
api_client = api_auth(settings.OFFICIAL_WEB_API_KEY, db=db)
dummy_user = protocol_schema.User(id="__dummy_user__", display_name="Dummy User", auth_method="local")
ur = UserRepository(db=db, api_client=api_client)
+1 -23
View File
@@ -61,33 +61,11 @@ def create_api_client(
return api_client
def get_dummy_api_client(session: Session) -> ApiClient:
# make sure that a dummy api key exits in db (foreign key references)
DUMMY_API_KEY = "1234"
api_client: ApiClient = session.query(ApiClient).filter(ApiClient.api_key == DUMMY_API_KEY).first()
if api_client is None:
logger.info(f"ANY_API_KEY missing, inserting api_key: {DUMMY_API_KEY}")
api_client = create_api_client(
session=session,
api_key=DUMMY_API_KEY,
description="Dummy api key for debugging",
trusted=True,
frontend_type="Test frontend",
)
session.add(api_client)
session.commit()
return api_client
def api_auth(
api_key: APIKey,
db: Session,
) -> ApiClient:
if api_key or settings.DEBUG_SKIP_API_KEY_CHECK:
if settings.DEBUG_SKIP_API_KEY_CHECK or settings.DEBUG_ALLOW_DEBUG_API_KEY:
return get_dummy_api_client(db)
if api_key:
api_client = db.query(ApiClient).filter(ApiClient.api_key == api_key).first()
if api_client is not None and api_client.enabled:
return api_client
+1 -2
View File
@@ -59,6 +59,7 @@ class TreeManagerConfiguration(BaseModel):
class Settings(BaseSettings):
PROJECT_NAME: str = "open-assistant backend"
API_V1_STR: str = "/api/v1"
OFFICIAL_WEB_API_KEY: str = "1234"
POSTGRES_HOST: str = "localhost"
POSTGRES_PORT: str = "5432"
@@ -71,8 +72,6 @@ class Settings(BaseSettings):
REDIS_HOST: str = "localhost"
REDIS_PORT: str = "6379"
DEBUG_ALLOW_DEBUG_API_KEY: bool = False
DEBUG_SKIP_API_KEY_CHECK: bool = False
DEBUG_USE_SEED_DATA: bool = False
DEBUG_USE_SEED_DATA_PATH: Optional[FilePath] = (
Path(__file__).parent.parent / "test_data/realistic/realistic_seed_data.json"