-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
92 lines (78 loc) · 2.7 KB
/
app.py
File metadata and controls
92 lines (78 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
PuzzleSolver AI - Main Application
A web application for matching puzzle pieces to their location in complete puzzles.
"""
import os
import logging
from flask import Flask, send_from_directory
from flask_cors import CORS
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('logs/app.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Create Flask app
app = Flask(__name__, static_folder='frontend/build', static_url_path='')
CORS(app)
# Configuration
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 # 10MB max file size
app.config['UPLOAD_FOLDER'] = 'temp'
app.config['PUZZLE_FOLDER'] = 'saved_puzzles'
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-secret-key-change-in-production')
# Ensure required directories exist
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['PUZZLE_FOLDER'], exist_ok=True)
os.makedirs('logs', exist_ok=True)
# Import routes after app creation to avoid circular imports
from src.api.routes import api_bp
# Register blueprints with prefix
app.register_blueprint(api_bp, url_prefix='/puzzle_solver/api')
# Serve React frontend
@app.route('/puzzle_solver/', defaults={'path': ''})
@app.route('/puzzle_solver/<path:path>')
def serve_frontend(path):
"""Serve the React frontend application."""
if path != "" and os.path.exists(os.path.join(app.static_folder, path)):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, 'index.html')
# Root redirect
@app.route('/')
def root():
"""Redirect root to puzzle_solver."""
from flask import redirect
return redirect('/puzzle_solver/')
# Error handlers
@app.errorhandler(404)
def not_found(error):
"""Handle 404 errors."""
return {'success': False, 'error': 'Resource not found'}, 404
@app.errorhandler(500)
def internal_error(error):
"""Handle 500 errors."""
logger.error(f"Internal server error: {error}")
return {'success': False, 'error': 'Internal server error'}, 500
@app.errorhandler(413)
def request_entity_too_large(error):
"""Handle file too large errors."""
return {
'success': False,
'error': 'File too large',
'details': 'Maximum file size is 10MB'
}, 413
if __name__ == '__main__':
logger.info("Starting PuzzleSolver AI application...")
logger.info(f"Puzzle storage: {app.config['PUZZLE_FOLDER']}")
logger.info(f"Temporary storage: {app.config['UPLOAD_FOLDER']}")
# Run the application
app.run(
host='0.0.0.0',
port=8000,
debug=False,
threaded=True
)