-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
118 lines (93 loc) · 3.6 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
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
# ======= Libs imports =========
import atexit
import logging
import psycopg2
from decouple import Config, RepositoryEnv
# ======= Flask imports ========
from flask_cors import CORS
from flask import Flask, jsonify,request
from flask_limiter import Limiter
from flask_compress import Compress
from flask_limiter.util import get_remote_address
from flask_jwt_extended import JWTManager, jwt_required, create_access_token, get_jwt_identity
# ======== Routes imports =======
from routes.auth.auth import auth_routes
from routes.topic.topic import topic_routes
from routes.explore.explore import explore_routes
from routes.profile.profile import profile_routes
from routes.notification.notification import notification_routes
# ========= File imports ========
from routes.auth.auth_db import AuthDb
# .env
config = Config(RepositoryEnv(".env"))
secret_key =config('SECRET_KEY')
port = config('PORT')
USERNAME = config('USERNAME')
PORTNUMBER = config('PORTNUMBER')
HOSTNAME = config('HOSTNAME')
DATABASE = config('DATABASE')
PASSWORD = config('PASSWORD')
URL = config("URL")
# ======================= App config ========================
app = Flask(__name__)
# logger
app.logger.setLevel(logging.INFO)
# rate limit
limiter = Limiter(get_remote_address, app=app, default_limits=["200 per day", "50 per hour"])
CORS(app)
# res compress
compress = Compress()
compress.init_app(app)
# ==================== JWT Configuration =====================
app.config['JWT_SECRET_KEY'] = secret_key
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = 10 * 24 * 60 * 60
jwt = JWTManager(app)
# ==================== Logging ==============================
file_handler = logging.FileHandler('flask.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(message)s - IP: %(client_ip)s')
file_handler.setFormatter(formatter)
app.logger.addHandler(file_handler)
# custom log filter to include client IP address
class ClientIPFilter(logging.Filter):
def filter(self, record):
if hasattr(request, 'remote_addr'):
record.client_ip = request.remote_addr
else:
record.client_ip = 'Unknown'
return True
app.logger.addFilter(ClientIPFilter())
# =============== Databse Connection ===============
def connect_to_db():
try:
print("\n💻 Connecting to database ...\n")
connection = psycopg2.connect(URL)
print("✅ Connected to database 💻 ...\n")
return connection
except (Exception,psycopg2.Error) as error:
print("\n❌ Error while connecting to database\n")
print(error)
connection = connect_to_db()
authDbObj = AuthDb(connection)
# =================== ROUTES START =========================
# Log incoming requests before handling them
# @app.before_request
# def log_request():
# app.logger.info(f"Incoming request: {request.method} {request.path}")
@app.route('/checkServer', methods=["GET"])
def check():
return jsonify({"message":"Server is running"})
app.register_blueprint(auth_routes(connection,limiter), url_prefix='/')
app.register_blueprint(explore_routes(connection,limiter), url_prefix='/')
app.register_blueprint(topic_routes(connection,limiter), url_prefix='/topic')
app.register_blueprint(profile_routes(connection,limiter), url_prefix='/profile')
app.register_blueprint(notification_routes(connection,limiter), url_prefix='/')
@app.errorhandler(404)
def error(e):
return jsonify({"msg": "Wrong Route"}), 404
# =================== ROUTES END ============================
def cleanup():
print("\n❌ Closed database connection...\n")
connection.close()
atexit.register(cleanup)
if __name__ == '__main__':
app.run(debug=True, port=port,)