-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (53 loc) · 1.53 KB
/
app.py
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
"""
File: app.py
Author: Vianney Veremme
Date: 2024-11-11
Description: This file contains a simple Flask application with two routes.
"""
import datetime
from flask import Flask, jsonify, redirect
from flask_cors import CORS
from config import Config, limiter
from routes import register_routes
app = Flask(__name__)
app.config.from_object(Config)
CORS(app)
if app.config["TESTING"]:
limiter.enabled = False
@app.route("/", methods=["GET"])
def home():
return redirect("/time")
@app.route("/time", methods=["GET"])
def server_time():
return jsonify(server_time=datetime.datetime.now().isoformat())
# Register all routes
register_routes(app)
# Add a status field to all JSON responses
@app.after_request
def add_status(response):
if response.is_json:
original_data = response.get_json()
new_response = {
"success": response.status_code in range(200, 300),
"data": original_data if original_data != [] else None,
}
response.set_data(jsonify(new_response).data)
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):
return (
jsonify(
{
"error": "Too many requests",
"message": "Rate limit exceeded. Please try again later.",
"rate_limit": e.description,
}
),
429,
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)