-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathapp.py
More file actions
136 lines (108 loc) · 4.04 KB
/
app.py
File metadata and controls
136 lines (108 loc) · 4.04 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
Book Translator Application
===========================
Flask application factory and main entry point.
"""
import os
from flask import Flask, send_from_directory
from flask_cors import CORS
from book_translator.api.middleware import add_rate_limit_headers
from book_translator.api.routes import (
create_files_blueprint,
create_health_blueprint,
create_logs_blueprint,
create_models_blueprint,
create_translation_blueprint,
)
from book_translator.config import config
from book_translator.database.connection import get_database
from book_translator.utils.logging import debug_print, get_logger
def create_app(testing: bool = False) -> Flask:
"""
Application factory for Flask app.
Args:
testing: If True, configure for testing
Returns:
Configured Flask application
"""
app = Flask(
__name__, static_folder=config.paths.static_folder, static_url_path="/static"
)
# Configuration
app.config.update(
SECRET_KEY=config.server.secret_key,
MAX_CONTENT_LENGTH=config.file.max_file_size_bytes,
JSON_SORT_KEYS=False,
TESTING=testing,
)
# CORS configuration
cors_origins = config.server.cors_origins
if testing:
cors_origins = ["*"]
CORS(
app, resources={r"/api/*": {"origins": cors_origins}}, supports_credentials=True
)
# Initialize database
if not testing:
get_database()
# Register blueprints
app.register_blueprint(create_translation_blueprint())
app.register_blueprint(create_models_blueprint())
app.register_blueprint(create_health_blueprint())
app.register_blueprint(create_files_blueprint())
app.register_blueprint(create_logs_blueprint())
# Add middleware
app.after_request(add_rate_limit_headers)
# Error handlers
@app.errorhandler(400)
def bad_request(e):
return {"error": "Bad request", "details": str(e)}, 400
@app.errorhandler(404)
def not_found(e):
return {"error": "Resource not found"}, 404
@app.errorhandler(413)
def file_too_large(e):
max_mb = config.file.max_file_size_mb
return {"error": f"File too large. Maximum size is {max_mb}MB"}, 413
@app.errorhandler(429)
def rate_limited(e):
return {"error": "Rate limit exceeded"}, 429
@app.errorhandler(500)
def internal_error(e):
logger = get_logger().api_logger
logger.error(f"Internal error: {e}")
return {"error": "Internal server error"}, 500
# Serve frontend
@app.route("/")
def index():
return send_from_directory(config.paths.static_folder, "index.html")
# Log startup
logger = get_logger()
logger.api_logger.info(
f"Book Translator started on {config.server.host}:{config.server.port}"
)
if config.logging.verbose_debug:
debug_print("🚀 Application initialized", "INFO", "APP")
return app
def run_server():
"""Run the Flask development server."""
app = create_app()
print(
f"""
╔══════════════════════════════════════════════════════════════╗
║ 📚 Book Translator v2.0 ║
╠══════════════════════════════════════════════════════════════╣
║ Server: http://{config.server.host}:{config.server.port:<5} ║
║ Model: {config.ollama.default_model:<50} ║
║ Debug: {'Enabled' if config.server.debug else 'Disabled':<50} ║
╚══════════════════════════════════════════════════════════════╝
"""
)
app.run(
host=config.server.host,
port=config.server.port,
debug=config.server.debug,
threaded=True,
)
if __name__ == "__main__":
run_server()