-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
87 lines (71 loc) · 2.86 KB
/
app.py
File metadata and controls
87 lines (71 loc) · 2.86 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
from flask import Flask, render_template, request
from flask_cors import CORS
import logging
import os
import tempfile
import threading
import time
from routes.url_routes import init_routes
from routes.static_routes import init_static_routes
# Set up logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Get the absolute paths
STATIC_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')
TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
logger.info(f"Static directory path: {STATIC_DIR}")
logger.info(f"Template directory path: {TEMPLATE_DIR}")
app = Flask(__name__,
static_folder=STATIC_DIR,
template_folder=TEMPLATE_DIR)
CORS(app)
# Create a temporary directory for processing
TEMP_DIR = tempfile.mkdtemp()
logger.info(f"Created temporary directory: {TEMP_DIR}")
def cleanup_temp_file(filepath, delay=300):
"""Delete temporary file after specified delay"""
def delete_file():
time.sleep(delay)
try:
if os.path.exists(filepath):
os.remove(filepath)
logger.info(f"Deleted temporary file: {filepath}")
except Exception as e:
logger.error(f"Error deleting temporary file: {str(e)}")
thread = threading.Thread(target=delete_file)
thread.daemon = True
thread.start()
# Add cleanup_temp_file to app context
app.cleanup_temp_file = cleanup_temp_file
# Ensure directories exist
if not os.path.exists(STATIC_DIR):
os.makedirs(STATIC_DIR)
logger.info(f"Created static directory: {STATIC_DIR}")
if not os.path.exists(TEMPLATE_DIR):
os.makedirs(TEMPLATE_DIR)
logger.info(f"Created templates directory: {TEMPLATE_DIR}")
# Error handlers
@app.errorhandler(404)
def page_not_found(e):
error_message = "The requested page was not found. Please check the URL and try again."
return render_template('error.html', error_message=error_message), 404
@app.errorhandler(500)
def internal_error(e):
error_message = "An internal server error occurred. Please try again later."
return render_template('error.html', error_message=error_message), 500
@app.route('/error')
def error_page():
error_message = request.args.get('message', 'An error occurred while processing your request.')
return render_template('error.html', error_message=error_message)
# Initialize routes
init_static_routes(app, STATIC_DIR) # Initialize static file routes
init_routes(app, TEMP_DIR) # Initialize API routes
if __name__ == '__main__':
# List files in static directory
if os.path.exists(STATIC_DIR):
files = os.listdir(STATIC_DIR)
logger.info(f"Files in static directory: {files}")
if os.path.exists(TEMPLATE_DIR):
files = os.listdir(TEMPLATE_DIR)
logger.info(f"Files in templates directory: {files}")
app.run(host='0.0.0.0', debug=False, port=5000)