- Added general authentication that seperates user calles from a gateway and cron scheduler updates
- Added notifications being seperate by user_id - Added user paths to subscribe/unsubscribe from a repository - Cleaned up main.py by moving handlers to its own module
This commit is contained in:
1 parent
7181cdbd1b
commit
e06a9f5224
5 files changed
+484
-87
No files matched your search
+215
-87
@@ -3,20 +3,33 @@ import os
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
from fastapi import APIRouter, Depends, FastAPI, HTTPException, Request, Response
|
||||
from fastapi import APIRouter, Depends, FastAPI, HTTPException, Request
|
||||
from service_auth import UserId
|
||||
|
||||
from auth_config import Caller, auth
|
||||
from db_module import close_connection_pool, create_connection_pool, get_db, start_healthcheck_thread
|
||||
from dockerhub_api import find_package_version_with_tag as find_package_version_with_tag_dockerhub
|
||||
from github_api import find_package_version_with_tag as find_package_version_with_tag_github
|
||||
from metrics_server import REQUEST_COUNTER, metrics_api
|
||||
from handlers import register_handlers
|
||||
from metrics_server import metrics_api
|
||||
from models import RepositoryType, RepositoryUpdate
|
||||
from repositories import (
|
||||
fetch_latest,
|
||||
reset_missing_checks,
|
||||
unsubscribe_every_subscriber_from_repository,
|
||||
validate_repository_combination_and_return_latest_tag,
|
||||
)
|
||||
from repository_errors import (
|
||||
ApiAuthenticationFailure,
|
||||
RateLimitedError,
|
||||
RepositoryApiError,
|
||||
RepositoryNotFoundError,
|
||||
RepositoryTagNotFoundError,
|
||||
)
|
||||
from secret_manager import cleanup_secret_files
|
||||
from send_notifications import NotificationClient
|
||||
from send_notifications import NotificationClient, NotificationError, NotificationUnavailableError
|
||||
from simple_logger_handler import LOG_LEVEL, setup_logger
|
||||
|
||||
logger = setup_logger(__name__)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("[FastAPI] Starting application...")
|
||||
@@ -51,103 +64,218 @@ api = FastAPI(
|
||||
dependencies=[Depends(auth.authenticate)],
|
||||
)
|
||||
|
||||
@api.middleware("http")
|
||||
async def prometheus_middleware(request: Request, call_next):
|
||||
logger.debug(f"[FastAPI] Incoming request: {request.method} {request.url.path}")
|
||||
status = 500
|
||||
try:
|
||||
response = await call_next(request)
|
||||
status = response.status_code
|
||||
logger.debug(f"[FastAPI] Request processed with status {status}")
|
||||
except Exception as e:
|
||||
logger.error(f"[FastAPI] Exception occurred: {e}", exc_info=True)
|
||||
raise
|
||||
finally:
|
||||
REQUEST_COUNTER.labels(request.method, request.url.path, status).inc()
|
||||
logger.debug(f"[FastAPI] Counter incremented for {request.method} {request.url.path} [{status}]")
|
||||
return response
|
||||
|
||||
register_handlers(api)
|
||||
user_router = APIRouter(dependencies=[Depends(auth.require(Caller.GATEWAY))])
|
||||
scheduler_router = APIRouter(dependencies=[Depends(auth.require(Caller.SCHEDULER))])
|
||||
|
||||
@scheduler_router.post("/github")
|
||||
def handle_suwayomi(
|
||||
request: Request,
|
||||
db = Depends(get_db)
|
||||
):
|
||||
|
||||
@scheduler_router.post("/update/{repository_type}")
|
||||
def update_watched_repositories(
|
||||
request: Request,
|
||||
repository_type: RepositoryType,
|
||||
db=Depends(get_db),
|
||||
):
|
||||
results = {"checked": 0, "new": 0, "errors": 0}
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"SELECT id, org, repo, tag, latest_entry, missing_checks "
|
||||
"FROM watched_repositories WHERE type = %s AND active = 1",
|
||||
(repository_type.value,),
|
||||
)
|
||||
repositories = cursor.fetchall()
|
||||
logger.debug(f"[FastAPI] Found {len(repositories)} active {repository_type.value} repositories to check.")
|
||||
|
||||
for repository_id, org, repo, tag, latest_entry, missing_checks in repositories:
|
||||
results["checked"] += 1
|
||||
try:
|
||||
online_version = fetch_latest(repository_type, org, repo, tag)
|
||||
except RateLimitedError as e:
|
||||
logger.warning(f"[FastAPI] Rate limited, stopping the {repository_type.value} check: {e}")
|
||||
results["errors"] += 1
|
||||
break
|
||||
except ApiAuthenticationFailure as e:
|
||||
logger.error(f"[FastAPI] {e}")
|
||||
results["errors"] += 1
|
||||
break
|
||||
except (RepositoryNotFoundError, RepositoryTagNotFoundError) as e:
|
||||
logger.warning(f"[FastAPI] {e}, deactivating repository ID {repository_id}")
|
||||
unsubscribe_every_subscriber_from_repository(db, request.app.state.notifier, repository_id, org, repo, tag)
|
||||
continue
|
||||
except RepositoryApiError as e:
|
||||
logger.warning(f"[FastAPI] Lookup failed for ID {repository_id}: {e}")
|
||||
results["errors"] += 1
|
||||
continue
|
||||
except Exception:
|
||||
logger.exception(f"[FastAPI] Unexpected error for ID {repository_id} ({org}/{repo}:{tag})")
|
||||
results["errors"] += 1
|
||||
continue
|
||||
|
||||
if missing_checks:
|
||||
reset_missing_checks(db, repository_id)
|
||||
if latest_entry == online_version:
|
||||
continue
|
||||
|
||||
try:
|
||||
|
||||
logger.info(f"[FastAPI] New version detected for org={org}, repo={repo}, tag={tag}")
|
||||
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"SELECT user_id, custom_name "
|
||||
"FROM watched_repositories_subscriptions "
|
||||
"WHERE repository_id = %s AND active = 1",
|
||||
(repository_id,),
|
||||
)
|
||||
subscribers = cursor.fetchall()
|
||||
|
||||
sent = 0
|
||||
failed = 0
|
||||
for user_id, custom_name in subscribers:
|
||||
try:
|
||||
request.app.state.notifier.send(
|
||||
title=custom_name or f"{org}/{repo}",
|
||||
body=f"New version {online_version} for tag {tag}",
|
||||
category="utility",
|
||||
user_id=user_id,
|
||||
)
|
||||
sent += 1
|
||||
except NotificationUnavailableError:
|
||||
failed = len(subscribers) - sent
|
||||
logger.error(
|
||||
f"[FastAPI] Notifications backend unavailable, skipping "
|
||||
f"{failed} remaining user(s) for repo={org}/{repo}"
|
||||
)
|
||||
break
|
||||
except NotificationError as e:
|
||||
failed += 1
|
||||
logger.error(
|
||||
f"[FastAPI] Failed to send notification to user_id={user_id} for repo={org}/{repo} "
|
||||
f"status={getattr(e, 'status_code', None)}"
|
||||
)
|
||||
|
||||
if subscribers and sent == 0:
|
||||
results["errors"] += 1
|
||||
logger.error(f"[FastAPI] All notifications failed for ID:{repository_id}, will retry later")
|
||||
continue
|
||||
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"UPDATE watched_repositories SET latest_entry = %s WHERE id = %s",
|
||||
(online_version, repository_id),
|
||||
)
|
||||
db.commit()
|
||||
logger.debug(f"[FastAPI] Notification sent and database updated for repository ID {repository_id}")
|
||||
results["new"] += 1
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
results["errors"] += 1
|
||||
logger.error(
|
||||
f"[FastAPI] Failed for ID {repository_id} (org={org}, repo={repo}, tag={tag}): {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
|
||||
if results["errors"]:
|
||||
logger.warning(f"[FastAPI] {repository_type.value} check finished with errors: {results}")
|
||||
raise HTTPException(status_code=500, detail=results)
|
||||
logger.info(f"[FastAPI] {repository_type.value} check complete: {results}")
|
||||
return results
|
||||
|
||||
@user_router.post("/add/repository")
|
||||
def add_repository_subscription(
|
||||
user_id: UserId,
|
||||
repository: RepositoryUpdate,
|
||||
db=Depends(get_db),
|
||||
):
|
||||
name = f"{repository.org}/{repository.repo}:{repository.tag}"
|
||||
logger.info(f"[FastAPI] Subscribe request: {repository.type.value} {name}, user_id={user_id}")
|
||||
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute(
|
||||
"SELECT id, active FROM watched_repositories "
|
||||
"WHERE type = %s AND org = %s AND repo = %s AND tag = %s",
|
||||
(repository.type.value, repository.org, repository.repo, repository.tag),
|
||||
)
|
||||
known = cursor.fetchone()
|
||||
|
||||
latest_entry = None
|
||||
if known is None or not known[1]:
|
||||
latest_entry = validate_repository_combination_and_return_latest_tag(repository)
|
||||
|
||||
try:
|
||||
logger.info("[FastAPI] Suwayomi handler invoked")
|
||||
online_version = find_package_version_with_tag_github("Suwayomi", "tachidesk", "stable")
|
||||
logger.debug(f"[FastAPI] Fetched latest Suwayomi version from GitHub: {online_version}")
|
||||
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute("SELECT latest_version FROM docker_repositories WHERE app='suwayomi'")
|
||||
local_state = cursor.fetchone()
|
||||
local_version = str(local_state[0]) if local_state and local_state[0] is not None else None
|
||||
logger.debug(f"[FastAPI] Comparing versions: local='{local_version}' vs online='{online_version}'")
|
||||
|
||||
if local_version != online_version:
|
||||
logger.debug("[FastAPI] Version mismatch detected. Updating database.")
|
||||
cursor.execute ("UPDATE docker_repositories SET latest_version=%s WHERE app='suwayomi'",
|
||||
(online_version,))
|
||||
db.commit()
|
||||
logger.info("[FastAPI] New Suwayomi version recorded in database")
|
||||
request.app.state.notifier.send(
|
||||
title="New Suwayomi version has been found",
|
||||
body=f"Version {online_version}",
|
||||
category="utility",
|
||||
user_id=1,
|
||||
)
|
||||
logger.debug("[FastAPI] Notification sent for Suwayomi update")
|
||||
return Response(status_code=200)
|
||||
|
||||
logger.debug("[FastAPI] No new Suwayomi version found")
|
||||
return Response(status_code=204)
|
||||
cursor.execute(
|
||||
"INSERT INTO watched_repositories (type, org, repo, tag, latest_entry) "
|
||||
"VALUES (%s, %s, %s, %s, %s) "
|
||||
"ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id), active = 1, missing_checks = 0, "
|
||||
"latest_entry = COALESCE(VALUES(latest_entry), latest_entry)",
|
||||
(repository.type.value, repository.org, repository.repo, repository.tag, latest_entry),
|
||||
)
|
||||
repository_id = cursor.lastrowid
|
||||
|
||||
cursor.execute(
|
||||
"INSERT INTO watched_repositories_subscriptions (user_id, repository_id, custom_name) "
|
||||
"VALUES (%s, %s, %s) "
|
||||
"ON DUPLICATE KEY UPDATE active = 1, custom_name = VALUES(custom_name)",
|
||||
(user_id, repository_id, repository.custom_name),
|
||||
)
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
logger.error(f"[FastAPI] Error in Suwayomi handler: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||
db.rollback()
|
||||
logger.exception(f"[FastAPI] Failed to subscribe user_id={user_id} to {name}")
|
||||
raise HTTPException(status_code=500) from e
|
||||
|
||||
logger.info(f"[FastAPI] user_id={user_id} subscribed to repository ID {repository_id} ({name})")
|
||||
return {"status": "ok", "detail": "Subscribed to repository", "name": name}
|
||||
|
||||
@user_router.post("/delete/repository")
|
||||
def delete_repository_subscription(
|
||||
user_id: UserId,
|
||||
repository: RepositoryUpdate,
|
||||
db=Depends(get_db),
|
||||
):
|
||||
name = f"{repository.org}/{repository.repo}:{repository.tag}"
|
||||
logger.info(f"[FastAPI] Unsubscribe request: {repository.type.value} {name}, user_id={user_id}")
|
||||
|
||||
@scheduler_router.post("/dockerhub")
|
||||
def handle_pihole(
|
||||
request: Request,
|
||||
db = Depends(get_db)
|
||||
):
|
||||
try:
|
||||
logger.info("[FastAPI] Pi-hole handler invoked")
|
||||
online_version = find_package_version_with_tag_dockerhub("pihole/pihole", "latest")
|
||||
logger.debug(f"[FastAPI] Fetched latest Pi-hole version from Docker Hub: {online_version}")
|
||||
|
||||
with db.cursor() as cursor:
|
||||
cursor.execute("SELECT latest_version FROM docker_repositories WHERE app='pihole'")
|
||||
local_state = cursor.fetchone()
|
||||
local_version = str(local_state[0]) if local_state and local_state[0] is not None else None
|
||||
logger.debug(f"[FastAPI] Comparing versions: local='{local_version}' vs online='{online_version}'")
|
||||
cursor.execute(
|
||||
"SELECT id FROM watched_repositories "
|
||||
"WHERE type = %s AND org = %s AND repo = %s AND tag = %s FOR UPDATE",
|
||||
(repository.type.value, repository.org, repository.repo, repository.tag),
|
||||
)
|
||||
known = cursor.fetchone()
|
||||
|
||||
if local_version != online_version:
|
||||
logger.debug("[FastAPI] Version mismatch detected. Updating database.")
|
||||
cursor.execute ("UPDATE docker_repositories SET latest_version=%s WHERE app='pihole'",
|
||||
(online_version,))
|
||||
db.commit()
|
||||
logger.info("[FastAPI] New Pi-hole version recorded in database")
|
||||
request.app.state.notifier.send(
|
||||
title="New Pi-hole version has been found",
|
||||
body=f"Version {online_version}",
|
||||
category="utility",
|
||||
user_id=1,
|
||||
if known is None:
|
||||
db.rollback()
|
||||
logger.info(f"[FastAPI] Repository {repository.type.value} {name} is not tracked, nothing to do")
|
||||
return {"status":"ok","detail":"Unsubscribed from repository","name":name}
|
||||
|
||||
repository_id = known[0]
|
||||
cursor.execute(
|
||||
"UPDATE watched_repositories_subscriptions SET active = 0 "
|
||||
"WHERE user_id = %s AND repository_id = %s",
|
||||
(user_id, repository_id),
|
||||
)
|
||||
|
||||
cursor.execute(
|
||||
"SELECT 1 from watched_repositories_subscriptions "
|
||||
"WHERE repository_id = %s AND active = 1 LIMIT 1 FOR SHARE",
|
||||
(repository_id,),
|
||||
)
|
||||
if cursor.fetchone() is None:
|
||||
cursor.execute(
|
||||
"UPDATE watched_repositories SET active = 0 WHERE id = %s",
|
||||
(repository_id,),
|
||||
)
|
||||
logger.debug("[FastAPI] Notification sent for Pi-hole update")
|
||||
return Response(status_code=200)
|
||||
|
||||
logger.debug("[FastAPI] No new Pi-hole version found")
|
||||
return Response(status_code=204)
|
||||
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
logger.error(f"[FastAPI] Error in Pi-hole handler: {e}", exc_info=True)
|
||||
raise HTTPException(status_code=500, detail=str(e)) from e
|
||||
db.rollback()
|
||||
logger.exception(f"[FastAPI] Failed to unsubscribe user_id={user_id} to {name}")
|
||||
raise HTTPException(status_code=500) from e
|
||||
|
||||
logger.info(f"[FastAPI] Unsubscribe request successful: {repository.type.value} {name}, user_id={user_id}")
|
||||
return {"status":"ok","detail":"Unsubscribed from repository","name":name}
|
||||
|
||||
api.include_router(user_router)
|
||||
api.include_router(scheduler_router)
|
||||
|
||||
Reference in new issue
Block a user