forked from cryptic-game/cryptic-device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (48 loc) · 1.49 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
"""The app module, containing the app factory function."""
from flask import Flask
from flask_cors import CORS
from time import sleep
from database import db
import config
from models.network import Network
from models.device import Device
from models.auth import User
from models.auth import Session
from blueprints.device_blueprint import devices
from blueprints.network_blueprint import networks
def create_app() -> Flask:
"""
An application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/.
:return: The initialized flask app
"""
app = Flask("cryptic")
app.config["SQLALCHEMY_DATABASE_URI"] = config.get_database_uri()
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
if config.config["CROSS_ORIGIN"]:
CORS(app)
with app.app_context():
db.init_app(app=app)
while True:
try:
register_models()
break
except Exception as e:
print(e)
sleep(2)
register_blueprints(app)
return app
def register_models() -> None:
"""
This function registers all database models.
:return: None
"""
db.create_all()
db.session.commit()
def register_blueprints(app: Flask) -> None:
"""
This function registers all flask blueprints.
:param app: The "raw" flask app
:return: None
"""
app.register_blueprint(devices, url_prefix="/device")
app.register_blueprint(networks, url_prefix="/network")