106 lines
No EOL
4.1 KiB
Python
106 lines
No EOL
4.1 KiB
Python
from flask import Flask
|
|
from flask_cors import CORS
|
|
from flask_jwt_extended import JWTManager
|
|
from dotenv import load_dotenv
|
|
import os
|
|
import tempfile
|
|
|
|
load_dotenv()
|
|
|
|
def setup_temp_directories():
|
|
"""Set up temporary directories for Flask/Werkzeug file handling."""
|
|
# Try to create a temp directory in the backend folder
|
|
backend_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
temp_dir = os.path.join(backend_dir, 'temp')
|
|
upload_dir = os.path.join(backend_dir, 'uploads')
|
|
|
|
# Create directories with proper permissions
|
|
for directory in [temp_dir, upload_dir]:
|
|
try:
|
|
os.makedirs(directory, exist_ok=True)
|
|
os.chmod(directory, 0o755)
|
|
|
|
# Test write permissions
|
|
test_file = os.path.join(directory, 'test_write')
|
|
with open(test_file, 'w') as f:
|
|
f.write('test')
|
|
os.remove(test_file)
|
|
print(f"✓ Directory {directory} is writable")
|
|
|
|
except (OSError, PermissionError) as e:
|
|
print(f"Warning: Cannot write to {directory}: {e}")
|
|
continue
|
|
|
|
# Set environment variables for Python's tempfile module
|
|
if os.path.isdir(temp_dir) and os.access(temp_dir, os.W_OK):
|
|
os.environ['TMPDIR'] = temp_dir
|
|
os.environ['TEMP'] = temp_dir
|
|
os.environ['TMP'] = temp_dir
|
|
tempfile.tempdir = temp_dir
|
|
print(f"✓ Set temp directory to: {temp_dir}")
|
|
|
|
return temp_dir, upload_dir
|
|
|
|
def create_app():
|
|
# Set up temp directories BEFORE creating Flask app
|
|
temp_dir, upload_dir = setup_temp_directories()
|
|
|
|
app = Flask(__name__)
|
|
|
|
# Setup custom logging configuration
|
|
try:
|
|
from logging_config import setup_logging, DEFAULT_LOG_LEVEL
|
|
setup_logging(os.environ.get('LOG_LEVEL', DEFAULT_LOG_LEVEL))
|
|
except ImportError:
|
|
pass # Fallback to default logging if logging_config is not available
|
|
|
|
# Configuration
|
|
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-secret-key')
|
|
app.config['JWT_SECRET_KEY'] = os.environ.get('JWT_SECRET_KEY', 'jwt-secret-key')
|
|
|
|
# Fix strict slashes - this prevents 308 redirects for trailing slashes
|
|
app.url_map.strict_slashes = False
|
|
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = 86400 # 24 hours
|
|
|
|
# Set longer timeouts for AI operations
|
|
app.config['TIMEOUT'] = 300 # 5 minutes
|
|
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB max upload
|
|
|
|
# Configure Flask/Werkzeug file upload settings
|
|
app.config['UPLOAD_FOLDER'] = upload_dir
|
|
app.config['UPLOAD_EXTENSIONS'] = ['.jpg', '.jpeg', '.png']
|
|
|
|
# Configure temp directory for Flask/Werkzeug
|
|
if temp_dir and os.path.isdir(temp_dir):
|
|
app.config['TEMP_FOLDER'] = temp_dir
|
|
print(f"✓ Flask configured with temp directory: {temp_dir}")
|
|
|
|
# Additional Werkzeug configuration for multipart form handling
|
|
app.config['MAX_CONTENT_PATH'] = None # Don't limit content path
|
|
|
|
# Configure Werkzeug to handle uploads without temp files for small files
|
|
app.config['MAX_FORM_MEMORY_SIZE'] = 16 * 1024 * 1024 # Keep small uploads in memory
|
|
|
|
# Initialize extensions
|
|
CORS(app, resources={r"/api/*": {"origins": "*"}})
|
|
jwt = JWTManager(app)
|
|
|
|
# Register blueprints
|
|
from app.routes.auth import auth_bp
|
|
from app.routes.personas import personas_bp
|
|
from app.routes.focus_groups import focus_groups_bp
|
|
from app.routes.ai_personas import ai_personas_bp
|
|
from app.routes.focus_group_ai import focus_group_ai_bp
|
|
|
|
app.register_blueprint(auth_bp, url_prefix='/api/auth')
|
|
app.register_blueprint(personas_bp, url_prefix='/api/personas')
|
|
app.register_blueprint(focus_groups_bp, url_prefix='/api/focus-groups')
|
|
app.register_blueprint(ai_personas_bp, url_prefix='/api/ai-personas')
|
|
app.register_blueprint(focus_group_ai_bp, url_prefix='/api/focus-group-ai')
|
|
|
|
# Health check endpoint
|
|
@app.route('/api/health', methods=['GET'])
|
|
def health_check():
|
|
return {'status': 'ok', 'message': 'Backend is running'}, 200
|
|
|
|
return app |