Spaces:
Running
Running
| """ | |
| VNEWS Clean Backend - serves static/index_v2.html directly. | |
| No injection layers. All APIs from existing modules preserved. | |
| Comments feature REMOVED per user request. | |
| """ | |
| import sys, os | |
| # Import the full chain which registers all API endpoints on the FastAPI app | |
| from app_main import app, _search_all, _clean | |
| # Now override the root '/' to serve our clean frontend | |
| from fastapi import Query, Request | |
| from fastapi.responses import HTMLResponse, FileResponse, JSONResponse | |
| from fastapi.staticfiles import StaticFiles | |
| import os | |
| # Remove old '/' route | |
| app.router.routes = [r for r in app.router.routes if not ( | |
| getattr(r, 'path', None) == '/' and 'GET' in getattr(r, 'methods', set()) | |
| )] | |
| # Remove comment endpoints (user requested removal) | |
| app.router.routes = [r for r in app.router.routes if not ( | |
| getattr(r, 'path', None) in ('/api/short/comments', '/api/short/comment') | |
| )] | |
| # Mount static files | |
| STATIC_DIR = os.path.join(os.path.dirname(__file__), 'static') | |
| app.mount('/static', StaticFiles(directory=STATIC_DIR), name='static') | |
| async def serve_index(): | |
| """Serve the clean v2 frontend - single HTML file, no injection.""" | |
| index_path = os.path.join(STATIC_DIR, 'index_v2.html') | |
| if os.path.exists(index_path): | |
| return FileResponse(index_path, media_type='text/html') | |
| return HTMLResponse('<h1>VNEWS</h1><p>index_v2.html not found</p>', status_code=500) | |
| # Keep /api/hashtag/sources using direct search (not Google News) | |
| # This was already overridden in app_main.py with _search_all | |
| # Just make sure it's accessible | |
| # Storage status endpoint | |
| def storage_status(): | |
| """Check if persistent storage is enabled.""" | |
| data_dir = '/data' | |
| persistent = os.path.isdir(data_dir) and os.access(data_dir, os.W_OK) | |
| return JSONResponse({'persistent': persistent, 'path': data_dir}) | |
| # Categories for the tab bar | |
| def get_categories(): | |
| """Return category list for frontend tab bar.""" | |
| return JSONResponse([]) # Categories moved into News tab, homepage shows media content | |
| # Share page | |
| async def share_page(url: str = '', title: str = '', img: str = ''): | |
| """OG share page for social media.""" | |
| html = f'''<!DOCTYPE html><html><head> | |
| <meta property="og:title" content="{_clean(title)}"> | |
| <meta property="og:url" content="{_clean(url)}"> | |
| <meta property="og:image" content="{_clean(img)}"> | |
| <meta property="og:type" content="article"> | |
| <meta property="og:site_name" content="VNEWS"> | |
| <meta http-equiv="refresh" content="0;url={_clean(url) or '/'}"> | |
| </head><body>Redirecting...</body></html>''' | |
| return HTMLResponse(html) | |