-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
96 lines (74 loc) · 2.42 KB
/
app.py
File metadata and controls
96 lines (74 loc) · 2.42 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
import argparse
import os
import traceback
from flask import Flask, jsonify, request
from flask_cors import CORS
from werkzeug.exceptions import HTTPException
from config.jwtoken import ACTIVE_KID_FILE
from config.logging import setup_logging
from config.ratelimit import limiter
from config.settings import Config
from routes import register_routes
from utility.database import extract_error_message
from utility.jwtoken.keys_rotation import rotate_keys
app = Flask(__name__)
app.config.from_object(Config)
CORS(app)
limiter.init_app(app)
# Set up logging
logger = setup_logging()
app.logger.handlers.clear()
for handler in logger.handlers:
app.logger.addHandler(handler)
app.logger.setLevel(logger.level)
if app.config["TESTING"]:
limiter.enabled = False
# Ensure the keys directory and active_kid.txt file exist
if not os.path.exists(ACTIVE_KID_FILE):
rotate_keys()
@app.route("/")
def home():
return jsonify(message="Hello there!")
# Register routes and error handlers
register_routes(app)
@app.after_request
def log_response(response):
sender = request.remote_addr
method = request.method
path = request.path
status_code = response.status_code
logger.info(f"{sender}: {method} {path} {status_code}")
return response
@app.after_request
def add_common_headers(response):
response.headers["X-Content-Type-Options"] = "nosniff"
return response
@app.errorhandler(429)
def ratelimit_error(e):
logger.warning(
f"Rate limit exceeded: {request.method} {request.path} - {e.description}"
)
return (
jsonify(
error="Too many requests",
message="Rate limit exceeded. Please try again later.",
rate_limit=e.description,
),
429,
)
@app.errorhandler(Exception)
def handle_exception(e):
if isinstance(e, HTTPException):
return e
trace = traceback.format_exc().splitlines()[-1]
logger.exception(f"An unhandled exception occurred: {trace}")
error_message = extract_error_message(str(e))
return jsonify(error="Internal Server Error", message=error_message), 500
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run the Flask application.")
parser.add_argument(
"--debug", action="store_true", help="Run the app in debug mode."
)
args = parser.parse_args()
logger.info("Starting Flask application...")
app.run(host="0.0.0.0", port=5000, debug=args.debug)