diff --git a/src/auth_config.py b/src/auth_config.py index 43c3784..07592b9 100644 --- a/src/auth_config.py +++ b/src/auth_config.py @@ -6,8 +6,10 @@ from service_auth import KeySource, ServiceAuth class Caller(StrEnum): GATEWAY = "gateway" SCHEDULER = "scheduler" + ADMIN = "admin" auth = ServiceAuth({ Caller.GATEWAY: KeySource("GATEWAY_KEY", "/etc/secrets/gateway_key"), Caller.SCHEDULER: KeySource("SCHEDULER_KEY", "/etc/secrets/scheduler_key"), + Caller.ADMIN: KeySource("ADMIN_KEY","/etc/secrets/admin_key") }) \ No newline at end of file diff --git a/src/main.py b/src/main.py index b3f6922..b563f73 100644 --- a/src/main.py +++ b/src/main.py @@ -62,6 +62,7 @@ api = FastAPI( register_handlers(api) user_router = APIRouter(dependencies=[Depends(auth.require(Caller.GATEWAY))]) scheduler_router = APIRouter(dependencies=[Depends(auth.require(Caller.SCHEDULER))]) +admin_router = APIRouter(dependencies=[Depends(auth.require(Caller.ADMIN))]) def to_iso(dt: datetime) -> str: return dt.astimezone(UTC).isoformat(timespec="microseconds") @@ -315,15 +316,45 @@ def list_active_and_inactive_anime( return anime_list +@admin_router.post("/admin/update/names") +def update_all_names_to_most_recently_available(db=Depends(get_db)): + logger.info("[FastAPI] ADMIN: Got request to update all existing names") + try: + with db.cursor() as cursor: + cursor.execute( + "SELECT id, anilist_id from seadex_state" + ) + rows = cursor.fetchall() + for id, anilist_id in rows: + anime_name = get_anime_name(anilist_id) + cursor.execute( + "UPDATE seadex_state SET anime_name = %s " + "WHERE id = %s", + (anime_name,id), + ) + db.commit() + logger.info( + "[FastAPI] ADMIN: Updated id=%s, anilist_id=%s with name=%s", + id, anilist_id, anime_name, + ) + except Exception as e: + db.rollback() + logger.exception("[FastAPI] ADMIN: Failed to update all existing names") + raise HTTPException(status_code=500) from e + + logger.info("[FastAPI] ADMIN: Updated %s entries", len(rows)) + return {"status": "ok", "detail": f"Updated {len(rows)} entries"} + api.include_router(user_router) api.include_router(scheduler_router) +api.include_router(admin_router) async def start_servers(): logger.info("[Server] Starting main API (port 5000) and metrics server (port 9000)...") logger.info("[Server] Build commit id=%s", os.getenv("COMMIT_ID", "unknown")) - auth.load_keys(api, Caller.GATEWAY, Caller.SCHEDULER) - cleanup_secret_files(auth.key_paths(Caller.GATEWAY, Caller.SCHEDULER)) + auth.load_keys(api, Caller.GATEWAY, Caller.SCHEDULER, Caller.ADMIN) + cleanup_secret_files(auth.key_paths(Caller.GATEWAY, Caller.SCHEDULER, Caller.ADMIN)) config_main = uvicorn.Config(api, host="0.0.0.0", port=5000, log_level=LOG_LEVEL.lower(),log_config=None) config_metrics = uvicorn.Config(metrics_api, host="0.0.0.0", port=9000, log_level=LOG_LEVEL.lower(),log_config=None)