Spaces:
Sleeping
Sleeping
Update flask_app.py
Browse files- flask_app.py +23 -12
flask_app.py
CHANGED
|
@@ -8,7 +8,7 @@ from PIL import Image
|
|
| 8 |
from email_validator import validate_email, EmailNotValidError
|
| 9 |
|
| 10 |
from flask import Flask, request, jsonify, send_file, session, render_template_string, redirect, url_for, Response
|
| 11 |
-
from flask_session import Session
|
| 12 |
from gtts import gTTS
|
| 13 |
from groq import Groq
|
| 14 |
import google.generativeai as genai
|
|
@@ -36,21 +36,20 @@ from admin_module import admin_manager, admin_required
|
|
| 36 |
if 'app' not in globals():
|
| 37 |
app = Flask(__name__)
|
| 38 |
|
| 39 |
-
# Configuration for sessions -
|
| 40 |
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-key-change-in-production-hf-spaces')
|
| 41 |
-
app.config['SESSION_TYPE'] = 'filesystem'
|
| 42 |
-
app.config['SESSION_PERMANENT'] = True # Mudança para True
|
| 43 |
-
app.config['SESSION_USE_SIGNER'] = True
|
| 44 |
-
app.config['SESSION_KEY_PREFIX'] = 'englishhelper:'
|
| 45 |
app.config['PERMANENT_SESSION_LIFETIME'] = 86400 # 24 hours
|
| 46 |
-
app.config['SESSION_FILE_DIR'] = '/tmp/flask-session' # Diretório específico
|
| 47 |
-
app.config['SESSION_FILE_THRESHOLD'] = 500 # Max arquivos de sessão
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
|
| 51 |
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
Session
|
|
|
|
| 54 |
|
| 55 |
# Print session config for debugging
|
| 56 |
print(f"✅ Flask app inicializado - SECRET_KEY length: {len(app.config['SECRET_KEY'])}")
|
|
@@ -72,7 +71,19 @@ def debug_session():
|
|
| 72 |
if request.endpoint and 'admin' in request.endpoint:
|
| 73 |
print(f"🔍 Session Debug - Endpoint: {request.endpoint}")
|
| 74 |
print(f"🔍 Session Data: {dict(session)}")
|
| 75 |
-
print(f"🔍
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
# Database initialization (handled by app.py)
|
| 78 |
def initialize_database():
|
|
|
|
| 8 |
from email_validator import validate_email, EmailNotValidError
|
| 9 |
|
| 10 |
from flask import Flask, request, jsonify, send_file, session, render_template_string, redirect, url_for, Response
|
| 11 |
+
# from flask_session import Session # Removido para usar sessões nativas do Flask
|
| 12 |
from gtts import gTTS
|
| 13 |
from groq import Groq
|
| 14 |
import google.generativeai as genai
|
|
|
|
| 36 |
if 'app' not in globals():
|
| 37 |
app = Flask(__name__)
|
| 38 |
|
| 39 |
+
# Configuration for sessions - Simplificado para HF Spaces
|
| 40 |
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-key-change-in-production-hf-spaces')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
app.config['PERMANENT_SESSION_LIFETIME'] = 86400 # 24 hours
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Configurações específicas para HF Spaces
|
| 44 |
+
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
|
| 45 |
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
| 46 |
+
app.config['SESSION_COOKIE_SECURE'] = False # HF Spaces pode ter problemas com HTTPS interno
|
| 47 |
+
app.config['SESSION_COOKIE_HTTPONLY'] = True
|
| 48 |
+
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax' # Mais permissivo para HF Spaces
|
| 49 |
+
app.config['SESSION_COOKIE_NAME'] = 'englishhelper_session'
|
| 50 |
|
| 51 |
+
# Usar sessões nativas do Flask ao invés de Flask-Session
|
| 52 |
+
# Session(app) # Comentado para usar sessões nativas
|
| 53 |
|
| 54 |
# Print session config for debugging
|
| 55 |
print(f"✅ Flask app inicializado - SECRET_KEY length: {len(app.config['SECRET_KEY'])}")
|
|
|
|
| 71 |
if request.endpoint and 'admin' in request.endpoint:
|
| 72 |
print(f"🔍 Session Debug - Endpoint: {request.endpoint}")
|
| 73 |
print(f"🔍 Session Data: {dict(session)}")
|
| 74 |
+
print(f"🔍 All Cookies: {dict(request.cookies)}")
|
| 75 |
+
print(f"🔍 Session ID: {request.cookies.get('englishhelper_session', 'no-session')}")
|
| 76 |
+
print(f"🔍 User Agent: {request.headers.get('User-Agent', 'unknown')[:50]}...")
|
| 77 |
+
|
| 78 |
+
@app.after_request
|
| 79 |
+
def ensure_session_saved(response):
|
| 80 |
+
"""Garantir que a sessão seja salva"""
|
| 81 |
+
try:
|
| 82 |
+
if hasattr(session, 'accessed') and session.accessed:
|
| 83 |
+
session.permanent = True
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Session save error: {e}")
|
| 86 |
+
return response
|
| 87 |
|
| 88 |
# Database initialization (handled by app.py)
|
| 89 |
def initialize_database():
|