-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
116 lines (89 loc) · 2.85 KB
/
app.py
File metadata and controls
116 lines (89 loc) · 2.85 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
"""
This module initializes a Flask application, connects to a MongoDB database,
and defines routes with login-required decorators.
"""
from datetime import timedelta
import os
import sys
import signal
import threading
from dotenv import load_dotenv
from flask import Flask
from flask_caching import Cache
from flask_compress import Compress # type: ignore
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from core.configuration_settings import Config # noqa: E402
from core.database_mongo_manager import DatabaseMongoManager # noqa: E402
from core import handlers, shared # noqa: E402
DATABASE_MANAGER = None
DEADLINE_MANAGER = None
CONFIG_MANAGER = None
load_dotenv()
DATABASE = "cs3528_testing"
if shared.getenv("IS_TEST") == "True":
print("In test mode")
DATABASE = shared.getenv("MONGO_DB_TEST", "")
else:
print("In production mode")
DATABASE = shared.getenv("MONGO_DB_PROD", "")
DATABASE_MANAGER = DatabaseMongoManager(shared.getenv("MONGO_URI"), DATABASE)
tables = [
"users",
"students",
"opportunities",
"courses",
"skills",
"attempted_skills",
"modules",
"employers",
"deadline",
"config",
]
for table in tables:
DATABASE_MANAGER.add_table(table)
CONFIG_MANAGER = Config(DATABASE_MANAGER)
app = Flask(__name__)
PORT = int(shared.getenv("PORT", "5000"))
app.config["SECRET_KEY"] = shared.getenv("SECRET_KEY")
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024 # 16 MB
app.config["CACHE_TYPE"] = "SimpleCache"
app.config["CACHE_DEFAULT_TIMEOUT"] = 300
app.config["SESSION_COOKIE_SAMESITE"] = "Strict"
app.config["SESSION_COOKIE_SECURE"] = True
app.config["SESSION_COOKIE_HTTPONLY"] = True
app.permanent_session_lifetime = timedelta(minutes=30)
cache = Cache(app)
compress = Compress()
compress.init_app(app)
handlers.configure_routes(app, cache)
from core.deadline_manager import DeadlineManager # noqa: E402
DEADLINE_MANAGER = DeadlineManager()
def handle_kill_signal(_signum, _frame):
"""
Handle the kill signal to gracefully shut down the server.
"""
print("Kill signal received. Shutting down the server...")
DATABASE_MANAGER.close_connection()
sys.exit(0)
signal.signal(signal.SIGTERM, handle_kill_signal)
def run_app():
"""Run the Flask application."""
try:
app.run(port=PORT)
except KeyboardInterrupt:
DATABASE_MANAGER.close_connection()
print("Shutting down the server...")
except OSError:
DATABASE_MANAGER.close_connection()
print("Shutting down the server...")
except RuntimeError as e:
print(f"Runtime error: {e}")
except ValueError as e:
print(f"Value error: {e}")
finally:
DATABASE_MANAGER.close_connection()
print("Shutting down the server...")
if __name__ == "__main__":
app_thread = threading.Thread(target=run_app)
app_thread.start()
app_thread.join()