def _initialize():
import json
import os.path
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
config_path = os.environ.get('LIBRARIAN_CONFIG_PATH', 'server-config.json')
with open(config_path) as f:
config = json.load(f)
if 'SECRET_KEY' not in config:
print('cannot start server: must define the Flask "secret key" as the item '
'"SECRET_KEY" in "server-config.json"', file=sys.stderr)
sys.exit(1)
# TODO: configurable logging parameters will likely be helpful. We use UTC
# for timestamps using standard ISO-8601 formatting. The Python docs claim
# that 8601 is the default format but this does not appear to be true.
loglevel_cfg = config.get('log_level', 'info')
loglevel = _log_level_names.get(loglevel_cfg)
warn_loglevel = (loglevel is None)
if warn_loglevel:
loglevel = logging.INFO
logging.basicConfig(
level=loglevel,
format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y-%m-%dT%H:%M:%SZ'
)
import time
logging.getLogger('').handlers[0].formatter.converter = time.gmtime
logger = logging.getLogger('librarian')
if warn_loglevel:
logger.warn('unrecognized value %r for "log_level" config item', loglevel_cfg)
tf = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates')
app = Flask('librarian', template_folder=tf)
app.config.update(config)
db = SQLAlchemy(app)
return logger, app, db
评论列表
文章目录