Delete backend/app.py.bak
Browse files- backend/app.py.bak +0 -141
backend/app.py.bak
DELETED
|
@@ -1,141 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
import locale
|
| 4 |
-
from flask import Flask
|
| 5 |
-
from flask_cors import CORS
|
| 6 |
-
from flask_jwt_extended import JWTManager
|
| 7 |
-
from apscheduler.schedulers.background import BackgroundScheduler
|
| 8 |
-
import atexit
|
| 9 |
-
# Import for job handling
|
| 10 |
-
import uuid
|
| 11 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 12 |
-
|
| 13 |
-
from config import Config
|
| 14 |
-
from utils.database import init_supabase
|
| 15 |
-
from utils.cookies import setup_secure_cookies, configure_jwt_with_cookies
|
| 16 |
-
from scheduler.task_scheduler import init_scheduler
|
| 17 |
-
|
| 18 |
-
def setup_unicode_environment():
|
| 19 |
-
"""Setup Unicode environment for proper character handling."""
|
| 20 |
-
try:
|
| 21 |
-
# Set environment variables for UTF-8 support
|
| 22 |
-
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
| 23 |
-
os.environ['PYTHONUTF8'] = '1'
|
| 24 |
-
|
| 25 |
-
# Set locale to UTF-8 if available
|
| 26 |
-
try:
|
| 27 |
-
locale.setlocale(locale.LC_ALL, 'C.UTF-8')
|
| 28 |
-
except locale.Error:
|
| 29 |
-
try:
|
| 30 |
-
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
|
| 31 |
-
except locale.Error:
|
| 32 |
-
try:
|
| 33 |
-
locale.setlocale(locale.LC_ALL, '')
|
| 34 |
-
except locale.Error:
|
| 35 |
-
pass
|
| 36 |
-
|
| 37 |
-
# Set stdout/stderr encoding to UTF-8 if possible
|
| 38 |
-
if hasattr(sys.stdout, 'reconfigure'):
|
| 39 |
-
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
| 40 |
-
sys.stderr.reconfigure(encoding='utf-8', errors='replace')
|
| 41 |
-
|
| 42 |
-
# Log to app logger instead of print
|
| 43 |
-
if 'app' in globals():
|
| 44 |
-
app.logger.info("Unicode environment setup completed")
|
| 45 |
-
except Exception as e:
|
| 46 |
-
if 'app' in globals():
|
| 47 |
-
app.logger.warning(f"Unicode setup failed: {str(e)}")
|
| 48 |
-
|
| 49 |
-
def create_app():
|
| 50 |
-
"""Create and configure the Flask application."""
|
| 51 |
-
# Setup Unicode environment first
|
| 52 |
-
setup_unicode_environment()
|
| 53 |
-
|
| 54 |
-
app = Flask(__name__)
|
| 55 |
-
app.config.from_object(Config)
|
| 56 |
-
|
| 57 |
-
# Disable strict slashes to prevent redirects
|
| 58 |
-
app.url_map.strict_slashes = False
|
| 59 |
-
|
| 60 |
-
# Initialize CORS with specific configuration
|
| 61 |
-
CORS(app, resources={
|
| 62 |
-
r"/api/*": {
|
| 63 |
-
"origins": ["http://localhost:3000", "http://localhost:5000", "http://127.0.0.1:3000", "http://127.0.0.1:5000", "http://192.168.1.4:3000"],
|
| 64 |
-
"methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
| 65 |
-
"allow_headers": ["Content-Type", "Authorization"],
|
| 66 |
-
"supports_credentials": True
|
| 67 |
-
}
|
| 68 |
-
})
|
| 69 |
-
|
| 70 |
-
# Setup secure cookies
|
| 71 |
-
app = setup_secure_cookies(app)
|
| 72 |
-
|
| 73 |
-
# Initialize JWT with cookie support
|
| 74 |
-
jwt = configure_jwt_with_cookies(app)
|
| 75 |
-
|
| 76 |
-
# Initialize Supabase client
|
| 77 |
-
app.supabase = init_supabase(app.config['SUPABASE_URL'], app.config['SUPABASE_KEY'])
|
| 78 |
-
|
| 79 |
-
# Initialize a simple in-memory job store for tracking async tasks
|
| 80 |
-
# In production, you'd use a database or Redis for this
|
| 81 |
-
app.job_store = {}
|
| 82 |
-
|
| 83 |
-
# Initialize a ThreadPoolExecutor for running background tasks
|
| 84 |
-
# In production, you'd use a proper task queue like Celery
|
| 85 |
-
app.executor = ThreadPoolExecutor(max_workers=4)
|
| 86 |
-
|
| 87 |
-
# Initialize scheduler
|
| 88 |
-
if app.config['SCHEDULER_ENABLED']:
|
| 89 |
-
app.scheduler = BackgroundScheduler()
|
| 90 |
-
init_scheduler(app.scheduler, app.supabase)
|
| 91 |
-
app.scheduler.start()
|
| 92 |
-
|
| 93 |
-
# Shut down the scheduler when exiting the app
|
| 94 |
-
atexit.register(lambda: app.scheduler.shutdown())
|
| 95 |
-
|
| 96 |
-
# Register blueprints
|
| 97 |
-
from api.auth import auth_bp
|
| 98 |
-
from api.sources import sources_bp
|
| 99 |
-
from api.accounts import accounts_bp
|
| 100 |
-
from api.posts import posts_bp
|
| 101 |
-
from api.schedules import schedules_bp
|
| 102 |
-
|
| 103 |
-
app.register_blueprint(auth_bp, url_prefix='/api/auth')
|
| 104 |
-
app.register_blueprint(sources_bp, url_prefix='/api/sources')
|
| 105 |
-
app.register_blueprint(accounts_bp, url_prefix='/api/accounts')
|
| 106 |
-
app.register_blueprint(posts_bp, url_prefix='/api/posts')
|
| 107 |
-
app.register_blueprint(schedules_bp, url_prefix='/api/schedules')
|
| 108 |
-
|
| 109 |
-
# Health check endpoint
|
| 110 |
-
@app.route('/health')
|
| 111 |
-
def health_check():
|
| 112 |
-
return {'status': 'healthy', 'message': 'Lin backend is running'}, 200
|
| 113 |
-
|
| 114 |
-
# Add database connection check endpoint
|
| 115 |
-
@app.route('/api/health')
|
| 116 |
-
def api_health_check():
|
| 117 |
-
"""Enhanced health check that includes database connection."""
|
| 118 |
-
try:
|
| 119 |
-
from utils.database import check_database_connection
|
| 120 |
-
db_connected = check_database_connection(app.supabase)
|
| 121 |
-
return {
|
| 122 |
-
'status': 'healthy' if db_connected else 'degraded',
|
| 123 |
-
'database': 'connected' if db_connected else 'disconnected',
|
| 124 |
-
'message': 'Lin backend is running' if db_connected else 'Database connection issues'
|
| 125 |
-
}, 200 if db_connected else 503
|
| 126 |
-
except Exception as e:
|
| 127 |
-
return {
|
| 128 |
-
'status': 'unhealthy',
|
| 129 |
-
'database': 'error',
|
| 130 |
-
'message': f'Health check failed: {str(e)}'
|
| 131 |
-
}, 503
|
| 132 |
-
|
| 133 |
-
return app
|
| 134 |
-
|
| 135 |
-
if __name__ == '__main__':
|
| 136 |
-
app = create_app()
|
| 137 |
-
app.run(
|
| 138 |
-
host='0.0.0.0',
|
| 139 |
-
port=int(os.environ.get('PORT', 5000)),
|
| 140 |
-
debug=app.config['DEBUG']
|
| 141 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|