#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Root routes and static file serving for SPA in production. These were originally defined in the top-level app.py. Moving them into the package allows a clean WSGI entry (wsgi:app) without importing app.py. """ import os from pathlib import Path from datetime import datetime from flask import Blueprint, current_app, send_from_directory root_bp = Blueprint('root', __name__) def get_static_dir(): """取得靜態文件目錄(相對路徑)""" # 取得專案根目錄 project_root = Path(__file__).parent.parent static_dir = project_root / 'frontend' / 'dist' return str(static_dir) @root_bp.route('/') def index(): try: static_dir = get_static_dir() if Path(static_dir).exists(): return send_from_directory(static_dir, 'index.html') else: # Fallback API info when frontend is not present return { 'application': 'PANJIT Document Translator', 'version': '1.0.0', 'status': 'running', 'api_base_url': '/api/v1', 'note': 'Frontend files not found, serving API info' } except Exception: # Fallback API info when frontend is not present return { 'application': 'PANJIT Document Translator', 'version': '1.0.0', 'status': 'running', 'api_base_url': '/api/v1', 'note': 'Frontend files not found, serving API info' } @root_bp.route('/') def serve_static(path): try: static_dir = get_static_dir() if Path(static_dir).exists(): return send_from_directory(static_dir, path) else: # SPA fallback return send_from_directory(static_dir, 'index.html') except Exception: # SPA fallback return { 'error': 'File not found', 'path': path }, 404 @root_bp.route('/api') def api_info(): return { 'api_version': 'v1', 'base_url': '/api/v1', 'endpoints': { 'auth': '/api/v1/auth', 'files': '/api/v1/files', 'jobs': '/api/v1/jobs', 'admin': '/api/v1/admin', 'health': '/api/v1/health' }, 'documentation': 'Available endpoints provide RESTful API for document translation' } @root_bp.route('/api/health') def health_check(): # Keep a simple health endpoint here for compatibility return { 'status': 'healthy', 'timestamp': datetime.utcnow().isoformat(), 'service': 'PANJIT Document Translator API', 'version': '1.0.0' }, 200