From 26c3cc79d7d5683c79dfe312b8e7aa50ee2ce073 Mon Sep 17 00:00:00 2001 From: vikingowl Date: Mon, 4 Aug 2025 22:38:53 +0200 Subject: [PATCH] updated backend imports for clarity, enhanced sync logic, and expanded proxy routes in frontend --- backend/app/config.py | 2 +- backend/app/database.py | 2 +- backend/app/main.py | 24 ++++++++++++++++++------ frontend/vite.config.ts | 6 +++++- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/backend/app/config.py b/backend/app/config.py index 6d0f3ab..a5730f8 100644 --- a/backend/app/config.py +++ b/backend/app/config.py @@ -2,7 +2,7 @@ import logging import os from pathlib import Path -DB_PATH = Path(os.getenv("DB_NAME", "owlynews.sqlite3")) +DB_PATH = Path(os.getenv("DB_NAME", os.path.join(os.path.dirname(os.path.dirname(__file__)), "owlynews.sqlite3"))) OLLAMA_HOST = os.getenv("OLLAMA_HOST", "http://localhost:11434") MIN_CRON_HOURS = float(os.getenv("MIN_CRON_HOURS", 0.5)) DEFAULT_CRON_HOURS = float(os.getenv("CRON_HOURS", MIN_CRON_HOURS)) diff --git a/backend/app/database.py b/backend/app/database.py index 2d81a3f..6cd4e4d 100644 --- a/backend/app/database.py +++ b/backend/app/database.py @@ -5,7 +5,7 @@ from contextlib import contextmanager from pathlib import Path from typing import Iterator -from .config import ( +from backend.app.config import ( ARTICLE_FETCH_TIMEOUT, CRON_HOURS, DB_PATH, diff --git a/backend/app/main.py b/backend/app/main.py index 87ef9fc..003ab76 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -24,7 +24,7 @@ from fastapi import Depends, FastAPI, HTTPException, Response, status from fastapi.staticfiles import StaticFiles from starlette.responses import JSONResponse -from .config import ( +from backend.app.config import ( CRON_HOURS, LLM_MODEL, MIN_CRON_HOURS, @@ -34,8 +34,8 @@ from .config import ( frontend_path, logger, ) -from .database import get_db, get_db_write -from .models import ( +from backend.app.database import get_db, get_db_write +from backend.app.models import ( CronSettings, ErrorResponse, FeedData, @@ -44,7 +44,7 @@ from .models import ( SuccessResponse, TimestampResponse, ) -from .services import NewsFetcher +from backend.app.services import NewsFetcher app = FastAPI( title="Owly News Summariser", @@ -290,7 +290,7 @@ async def get_model_status(): @app.post("/sync", response_model=None) -async def manual_sync(db: sqlite3.Cursor = Depends(get_db)): +async def manual_sync(db: sqlite3.Cursor = Depends(get_db_write)): # Note: changed to get_db_write """ Manually trigger a feed synchronization. @@ -302,7 +302,14 @@ async def manual_sync(db: sqlite3.Cursor = Depends(get_db)): """ db.execute("SELECT val FROM meta WHERE key='last_sync'") row = db.fetchone() - last_sync_ts = int(row["val"]) + + if row is None: + # Initialize the last_sync key if it doesn't exist + import time + last_sync_ts = int(time.time()) - (SYNC_COOLDOWN_MINUTES * 60 + 1) # Set to a time that allows sync + db.execute("INSERT INTO meta (key, val) VALUES ('last_sync', ?)", (str(last_sync_ts),)) + else: + last_sync_ts = int(row["val"]) now = datetime.now(timezone.utc) last_sync_time = datetime.fromtimestamp(last_sync_ts, timezone.utc) @@ -314,13 +321,18 @@ async def manual_sync(db: sqlite3.Cursor = Depends(get_db)): try: task = asyncio.create_task(NewsFetcher.harvest_feeds()) + # Update the last_sync timestamp after triggering the sync + current_ts = int(time.time()) + db.execute("UPDATE meta SET val=? WHERE key='last_sync'", (str(current_ts),)) return {"status": "triggered", "task_id": id(task)} except Exception as e: + logger.error(f"❌ Failed to trigger sync: {e}") raise HTTPException( 500, f"Failed to trigger sync: {str(e)}" ) + @app.get("/meta/last-sync", response_model=TimestampResponse) async def get_last_sync(db: sqlite3.Cursor = Depends(get_db)): """ diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 6e81790..35c6513 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -23,7 +23,11 @@ export default defineConfig({ server: { proxy: { '/news': 'http://localhost:8000', - '/meta': 'http://localhost:8000' + '/meta': 'http://localhost:8000', + '/feeds': 'http://localhost:8000', + '/model': 'http://localhost:8000', + '/sync': 'http://localhost:8000', + '/settings': 'http://localhost:8000' } }, });