This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
105 lines (91 loc) · 3.02 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
import os
import logging
import sys
from urllib.parse import urlparse
from flask import Flask
from flask_babel import Babel
from flask_sqlalchemy_session import flask_scoped_session
from core.app_server import (
ErrorHandler,
)
from core.config import Configuration
from core.log import LogConfiguration
from core.model import SessionManager
app = Flask(__name__)
app._db = None
app.debug = None
babel = Babel(app)
from controller import MetadataWrangler
@app.before_first_request
def initialize_database(autoinitialize=True):
db_url = Configuration.database_url()
if autoinitialize:
SessionManager.initialize(db_url)
session_factory = SessionManager.sessionmaker(db_url)
_db = flask_scoped_session(session_factory, app)
app._db = _db
Configuration.load(_db)
testing = 'TESTING' in os.environ
log_level = LogConfiguration.initialize(_db, testing=testing)
if app.debug is None:
debug = log_level == 'DEBUG'
app.debug = debug
else:
debug = app.debug
app.config['DEBUG'] = debug
_db.commit()
app.log = logging.getLogger("Metadata web app")
app.log.info("Application debug mode: %r", app.debug)
for logger in logging.getLogger().handlers:
app.log.info("Logs are going to %r", logger)
# Register an error handler that logs exceptions through the
# normal logging process and tries to turn them into Problem
# Detail Documents.
h = ErrorHandler(app, app.config['DEBUG'])
@app.errorhandler(Exception)
def exception_handler(exception):
return h.handle(exception)
@app.before_first_request
def initialize_metadata_wrangler():
if getattr(app, 'wrangler', None) is None:
try:
app.wrangler = MetadataWrangler(app._db)
except Exception as e:
logging.error(
"Error instantiating metadata wrangler!", exc_info=e
)
raise e
# Make sure that any changes to the database (as might happen
# on initial setup) are committed before continuing.
app.wrangler._db.commit()
@app.teardown_request
def shutdown_session(exception):
if (hasattr(app, '_db')
and app._db):
if exception:
app._db.rollback()
else:
app._db.commit()
from routes import *
def run(self, url=None, debug=False):
base_url = url or 'http://localhost:5500/'
scheme, netloc, path, parameters, query, fragment = urlparse(base_url)
if ':' in netloc:
host, port = netloc.split(':')
port = int(port)
else:
host = netloc
port = 80
app.debug = debug
# Workaround for a "Resource temporarily unavailable" error when
# running in debug mode with the global socket timeout set by isbnlib
if app.debug:
import socket
socket.setdefaulttimeout(None)
logging.info("Starting app on %s:%s", host, port)
app.run(debug=debug, host=host, port=port)
if __name__ == '__main__':
url = None
if len(sys.argv) > 1:
url = sys.argv[1]
run(url, debug=True)