-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
executable file
·56 lines (41 loc) · 1.7 KB
/
server.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
import logging
import connexion
import config
from pkg import resolver
from config import USE_SQLITE, SQLALCHEMY_DATABASE_URI, POSTGRES_USER, POSTGRES_PASS, POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB, get_redacted_db_uri, print_sqlite_warning
from pkg.db.db import db, ma
from pkg.agent.rabbitpy_wrapper import rabbitpy_emitter as emitter
DEBUG = config.DEBUG
# set up Flask + Connexion app
connex_app = connexion.FlaskApp(__name__, debug=DEBUG)
app = connex_app.app
# set up internal configuration
app.config['SQLALCHEMY_DATABASE_URI'] = config.SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = config.SQLALCHEMY_TRACK_MODIFICATIONS
app.config['PROPAGATE_EXCEPTIONS'] = config.PROPAGATE_EXCEPTIONS
# configure database connection
if USE_SQLITE:
logging.info('Using SQLite: %s' % SQLALCHEMY_DATABASE_URI)
print_sqlite_warning()
else:
logging.info('Connecting to Postgres: %s' % (get_redacted_db_uri()))
SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://%s:%s@%s:%s/%s' \
% (POSTGRES_USER, POSTGRES_PASS, POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB)
# init and create database tables
@app.before_first_request
def create_tables():
db.create_all()
if __name__ == '__main__':
PORT = config.PORT
# configure Flask + Connexion app
resolver.load_swagger_spec(connex_app)
# connect app with sqlalchemy and marshmallow
db.init_app(app)
ma.init_app(app)
try:
# start the flask app on the specified port (default=5000)
logging.info("Serving API on port %d..." % PORT)
app.run(port=PORT, host='0.0.0.0', debug=DEBUG)
finally:
logging.warning('Shutting down all RabbitMQ executors...')
emitter.close()