import asyncio from contextlib import asynccontextmanager import uvicorn from fastapi import Depends, FastAPI, HTTPException, Request from db_module import close_connection_pool, create_connection_pool, get_db, start_healthcheck_thread from feed_handler import grab_gitea_release_information, grab_royal_road_chapter_information from metrics_server import REQUEST_COUNTER, metrics_api from secret_manager import cleanup_secret_files from send_notifications import NotificationClient from simple_logger_handler import LOG_LEVEL, setup_logger logger = setup_logger(__name__) @asynccontextmanager async def lifespan(app: FastAPI): logger.info("[FastAPI] Starting application...") app.state.notifier = NotificationClient() logger.info("[FastAPI] Creating MySQL connection pool...") create_connection_pool("royalRoadPool") start_healthcheck_thread() logger.info("[FastAPI] MySQL healthcheck thread started.") SECRET_PATHS = frozenset({ "/etc/secrets/api_key", "/etc/secrets/db_username", "/etc/secrets/db_password" }) cleanup_secret_files(SECRET_PATHS) yield logger.info("[FastAPI] Closing MySQL connection pool...") close_connection_pool() logger.info("[FastAPI] Shutdown complete.") api = FastAPI( title="Docker Repository Query", description="Queries Dockerhub and GHCR for new docker images", version="1.0.0", lifespan=lifespan ) @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 @api.get("/gitea") def get_release_information(request: Request, db = Depends(get_db)): """ Checks for new Gitea releases and updates the database accordingly. Behaviour: - Fetches all active Gitea feeds and their last known release link. - Queries each feed for its latest release. - Updates `last_entry` if the latest release link differs from the stored one. - Sends a notification for each changed release. - A failure on one feed is logged and the remaining feeds are still checked. Returns: A JSON object with checked/new/error counts. If any feed failed, a 500 is returned with the same counts as the detail. """ logger.info("[FastAPI] Checking for new Gitea releases") results = {"checked": 0, "new": 0, "errors": 0} with db.cursor() as cursor: cursor.execute( "SELECT id, rss_feed, feed_name, last_entry " "FROM rss_feeds WHERE active = 1 AND type = %s", ("gitea",), ) feeds = cursor.fetchall() logger.debug(f"[FastAPI] Found {len(feeds)} active Gitea feeds to check.") for feed_id, rss_feed, feed_name, last_entry in feeds: results["checked"] += 1 try: release_title, link = grab_gitea_release_information(rss_feed) logger.debug(f"[FastAPI] ID:{feed_id},RSS Feed {rss_feed}: db={last_entry}, online={link}") if link == last_entry: continue logger.info(f"[FastAPI] Release change for ID {feed_id}: {release_title}") request.app.state.notifier.send( title=feed_name, body=release_title, link=link, category="utility" ) cursor.execute( "UPDATE rss_feeds SET last_entry = %s WHERE id = %s", (link, feed_id), ) db.commit() logger.debug(f"[FastAPI] Notification sent and database updated for Gitea feed ID {feed_id}") results["new"] += 1 except Exception as e: db.rollback() results["errors"] += 1 logger.error( f"[FastAPI] Failed for feed ID {feed_id} ({rss_feed}): {e}", exc_info=True, ) if results["errors"]: logger.warning(f"[FastAPI] Gitea check finished with errors: {results}") raise HTTPException(status_code=500, detail=results) logger.info(f"[FastAPI] Gitea check complete: {results}") return results @api.get("/royalroad") def get_chapters(request: Request, db = Depends(get_db)): """ Checks for new Royalroad chapters and updates the database accordingly. Behaviour: - Fetches all active stories and their last known chapter ID. - Queries Royalroad for each story's latest chapter. - Updates `last_entry` if a new chapter is found. - Sends a notification for any newly discovered chapters. - A failure on one story is logged and the remaining stories are still checked. Returns: A JSON object with checked/new/error counts. If any story failed, a 500 is returned with the same counts as the detail. """ logger.info("[FastAPI] Checking for new Royalroad chapters") results = {"checked": 0, "new": 0, "errors": 0} with db.cursor() as cursor: cursor.execute( "SELECT id, rss_feed, last_entry " "FROM rss_feeds WHERE active = 1 AND type = %s", ("royalroad",), ) feeds = cursor.fetchall() logger.debug(f"[FastAPI] Found {len(feeds)} active stories to check.") for feed_id, rss_feed, last_entry in feeds: results["checked"] += 1 try: chapter_title, link, story_title, online_latest_entry = grab_royal_road_chapter_information(rss_feed) logger.debug(f"[FastAPI] ID:{feed_id},RSS Feed {rss_feed}: db={last_entry}, online={online_latest_entry}") if online_latest_entry == last_entry: continue logger.info(f"[FastAPI] New chapter detected for story ID {feed_id}: {story_title}") request.app.state.notifier.send( title=story_title, body=chapter_title, link=link, category="royal-road" ) cursor.execute( "UPDATE rss_feeds SET last_entry = %s WHERE id = %s", (online_latest_entry, feed_id), ) db.commit() logger.debug(f"[FastAPI] Notification sent and database updated for story ID {feed_id}") results["new"] += 1 except Exception as e: db.rollback() results["errors"] += 1 logger.error( f"[Royalroad] Failed for feed ID {feed_id} ({rss_feed}): {e}", exc_info=True, ) if results["errors"]: logger.warning(f"[FastAPI] Royalroad check finished with errors: {results}") raise HTTPException(status_code=500, detail=results) logger.info(f"[FastAPI] Royalroad check complete: {results}") return results async def start_servers(): logger.info("[Server] Starting main API (port 5000) and metrics server (port 9000)...") 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) server_main = uvicorn.Server(config_main) server_metrics = uvicorn.Server(config_metrics) await asyncio.gather(server_main.serve(), server_metrics.serve()) logger.info("[Server] Both servers have stopped.") if __name__ == "__main__": asyncio.run(start_servers())