-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (50 loc) · 1.9 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
import time
import os
import logging
import argparse
import yaml
import signal
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base() # this needs to be here (can't move)
from flask import Flask
app = Flask(__name__)
app.secret_key = 'This is a secret'
# Logging configuration
FORMAT = '[%(levelname)s] [ %(name)s ] %(message)s'
logging.basicConfig(level=logging.DEBUG, format=FORMAT)
logger = logging.getLogger(os.path.basename(__file__))
# determine where to run the app with optional command line argument
parser = argparse.ArgumentParser()
parser.add_argument("--test_locally", nargs='?', help="Run the app on local machine connecting to a database that is already created.")
args = parser.parse_args()
db_config_file = 'db_config.yml'
if args.test_locally:
with open('local_' + db_config_file, 'r') as yaml_file:
db_info = yaml.load(yaml_file)
else:
# running inside Docker container
with open(db_config_file, 'r') as yaml_file:
db_info = yaml.load(yaml_file)
db_username = db_info['username']
db_password = db_info['password']
db_host = db_info['host']
db_port = db_info['port']
from metis.database import MetisDatabase
# needs to be declared outside the if __name__ == '__main__'
metis_db = MetisDatabase(db_uri="postgresql://{user}:{password}@{db}:{db_port}".\
format(user=db_username, password=db_password, db=db_host, db_port=db_port), \
base=Base)
from metis import webapp # needs to be after the MetisDatabase stuff
if __name__ == "__main__":
def handler(signum, frame):
logger.info("SIGTERM received")
# close out connections
metis_db.close_connections()
logger.info("Cleanup complete")
signal.signal(signal.SIGTERM, handler)
# starting app (accessible externally through port 5000)
logger.info('Launching the web app.')
app.run(host='0.0.0.0', port=5000)
# locally close out connections
if args.test_locally:
metis_db.close_connections()