-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
143 lines (123 loc) · 4.89 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from flask import Flask, request
from flask_restplus import Api
from flask_socketio import SocketIO
from database_migration.migrate import migrate_non_cli
import api_namespaces
import database_manager
import config
import patch
app = Flask(__name__)
authorizations = {
'KwikkerKey': {
'type': 'apiKey',
'in': 'header',
'name': 'TOKEN'
},
'KwikkerCode': {
'type': 'apiKey',
'in': 'header',
'name': 'CODE'
}
}
api = Api(app, authorizations=authorizations, doc='/api/doc', title='Kwikker API', version='1.0',
validate=True)
socketio = SocketIO(app)
create_model = api.model
secret_key = None
code = None
def initialize_database():
"""
Initializes the database. If the production configuration the database credentials are
obtained from app.config (set in config.py). Else, the the database credentials of the
developer are used.
*Returns:*
- *True*: If the database connection was successful.
- *False*: Otherwise. The response of the database connection attempt is also printed.
"""
db_name = app.config['DATABASE_NAME']
db_username = app.config['DATABASE_USERNAME']
db_password = app.config['DATABASE_PASSWORD']
db_host = app.config['DATABASE_HOST']
db_port = app.config['DATABASE_PORT']
migrations_db_name = app.config['MIGRATIONS_DATABASE_NAME']
if migrate_non_cli(_db_name=db_name,
_db_username=db_username,
_db_password=db_password,
_db_host=db_host,
_db_port=db_port,
_migrations_db_name=migrations_db_name,
_db_manager=database_manager.db_manager):
response = database_manager.db_manager.initialize_connection(db_name=db_name,
db_username=db_username,
db_password=db_password,
host=db_host,
port=db_port)
if response is None:
print('Connected to the database successfully.')
return True
else:
print('Could not connect to the database.')
print(response)
return False
else:
return False
def import_routes():
"""
Dummy function to import the modules containing the routes (endpoints) so that the api
documentation is generated on startup.
"""
import users_profiles.routes
import users_interactions.routes
import authentication_and_registration.routes
import kweeks.routes
import timelines_and_trends.routes
import notifications.routes
import direct_messages.routes
import media.routes
def initialize(env):
"""
Loads the app configuration from the config.py, registers the api namespaces,
and initializes the database.
*Parameters:*
- *env (string)*: The environment in which the server is running for configurations
*Returns:*
- *True*: If the database connection was successful.
- *False*: Otherwise. The response of the database connection attempt is also printed.
"""
# Initializing configuration
if env == 'production':
app.config.from_object(config.ProductionConfig)
elif env == 'production test':
app.config.from_object(config.ProductionTestingConfig)
elif env == 'development test':
app.config.from_object(config.TestingConfig)
app.config.from_pyfile('config_local.py')
else:
app.config.from_object(config.DevelopmentConfig)
app.config.from_pyfile('config_local.py')
global secret_key
global code
secret_key = app.config['SECRET_KEY']
code = app.config['CODE_KEY']
# Apply monkey patches
patch.patch_flask_restplus_fields()
api_namespaces.initialize_api_namespaces(api=api)
import_routes()
return initialize_database()
@app.after_request
def inject_cors_headers(response):
if 'Origin' in request.headers:
response.headers.add('Access-Control-Allow-Origin', request.headers.get('Origin'))
else:
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Credentials', 'true')
response.headers.add('Access-Control-Allow-Methods', 'DELETE, GET, HEAD, OPTIONS, POST, PUT, PATCH')
response.headers.add('Access-Control-Allow-Headers', 'Origin, Content-Type, User-Agent, Content-Range, Token, Code')
response.headers.add('Access-Control-Expose-Headers', 'DAV, content-length, Allow')
return response
def run(env):
"""
Attempts to initialize the app, and runs it if the initialization was successful.
"""
if initialize(env):
socketio.run(app, host='0.0.0.0')