2013-09-20 15:55:44 +00:00
|
|
|
import logging
|
2014-05-01 23:44:28 +00:00
|
|
|
import logging.config
|
2014-05-02 01:19:52 +00:00
|
|
|
import uuid
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2014-07-07 21:06:53 +00:00
|
|
|
from peewee import Proxy
|
|
|
|
|
2013-09-30 20:14:48 +00:00
|
|
|
from app import app as application
|
2014-05-02 01:19:52 +00:00
|
|
|
from flask import request, Request
|
2014-05-02 17:43:57 +00:00
|
|
|
from util.names import urn_generator
|
2014-09-22 21:27:02 +00:00
|
|
|
from data.database import db as model_db, read_slave
|
2014-04-10 22:30:09 +00:00
|
|
|
|
2014-01-31 00:48:39 +00:00
|
|
|
# Turn off debug logging for boto
|
|
|
|
logging.getLogger('boto').setLevel(logging.CRITICAL)
|
2013-10-25 19:47:34 +00:00
|
|
|
|
2014-03-11 16:57:33 +00:00
|
|
|
from endpoints.api import api_bp
|
2013-12-30 22:05:27 +00:00
|
|
|
from endpoints.index import index
|
|
|
|
from endpoints.web import web
|
|
|
|
from endpoints.tags import tags
|
|
|
|
from endpoints.registry import registry
|
2014-09-16 04:18:57 +00:00
|
|
|
from endpoints.verbs import verbs
|
2013-12-30 22:05:27 +00:00
|
|
|
from endpoints.webhooks import webhooks
|
2014-02-06 18:36:32 +00:00
|
|
|
from endpoints.realtime import realtime
|
2014-02-18 20:50:15 +00:00
|
|
|
from endpoints.callbacks import callback
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2014-06-12 00:53:28 +00:00
|
|
|
from logentries import LogentriesHandler
|
|
|
|
|
2013-10-25 19:47:34 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2014-06-12 00:53:28 +00:00
|
|
|
|
|
|
|
werkzeug = logging.getLogger('werkzeug')
|
|
|
|
werkzeug.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
profile = logging.getLogger('profile')
|
|
|
|
profile.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
logentries_key = application.config.get('LOGENTRIES_KEY', None)
|
|
|
|
if logentries_key:
|
|
|
|
logger.debug('Initializing logentries with key: %s' % logentries_key)
|
|
|
|
werkzeug.addHandler(LogentriesHandler(logentries_key))
|
|
|
|
profile.addHandler(LogentriesHandler(logentries_key))
|
2013-10-25 19:47:34 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
application.register_blueprint(web)
|
2014-02-18 20:50:15 +00:00
|
|
|
application.register_blueprint(callback, url_prefix='/oauth2')
|
2013-12-30 22:05:27 +00:00
|
|
|
application.register_blueprint(index, url_prefix='/v1')
|
|
|
|
application.register_blueprint(tags, url_prefix='/v1')
|
|
|
|
application.register_blueprint(registry, url_prefix='/v1')
|
2014-09-19 16:54:52 +00:00
|
|
|
application.register_blueprint(verbs, url_prefix='/c1')
|
2014-03-11 16:57:33 +00:00
|
|
|
application.register_blueprint(api_bp, url_prefix='/api')
|
2013-12-30 22:05:27 +00:00
|
|
|
application.register_blueprint(webhooks, url_prefix='/webhooks')
|
2014-02-06 18:36:32 +00:00
|
|
|
application.register_blueprint(realtime, url_prefix='/realtime')
|
2013-12-30 22:05:27 +00:00
|
|
|
|
2014-05-02 01:19:52 +00:00
|
|
|
class RequestWithId(Request):
|
2014-05-02 17:43:57 +00:00
|
|
|
request_gen = staticmethod(urn_generator(['request']))
|
|
|
|
|
2014-05-02 01:19:52 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(RequestWithId, self).__init__(*args, **kwargs)
|
2014-05-02 17:43:57 +00:00
|
|
|
self.request_id = self.request_gen()
|
2014-05-02 01:19:52 +00:00
|
|
|
|
|
|
|
@application.before_request
|
|
|
|
def _request_start():
|
|
|
|
profile.debug('Starting request: %s', request.path)
|
|
|
|
|
|
|
|
|
|
|
|
@application.after_request
|
|
|
|
def _request_end(r):
|
|
|
|
profile.debug('Ending request: %s', request.path)
|
|
|
|
return r
|
|
|
|
|
|
|
|
class InjectingFilter(logging.Filter):
|
|
|
|
def filter(self, record):
|
2014-05-02 17:43:57 +00:00
|
|
|
record.msg = '[%s] %s' % (request.request_id, record.msg)
|
2014-05-02 01:19:52 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
profile.addFilter(InjectingFilter())
|
2014-01-31 01:57:40 +00:00
|
|
|
|
|
|
|
def close_db(exc):
|
2014-01-31 16:14:07 +00:00
|
|
|
db = model_db
|
2014-01-31 01:57:40 +00:00
|
|
|
if not db.is_closed():
|
|
|
|
logger.debug('Disconnecting from database.')
|
|
|
|
db.close()
|
|
|
|
|
2014-07-07 21:06:53 +00:00
|
|
|
if read_slave.obj is not None and not read_slave.is_closed():
|
|
|
|
logger.debug('Disconnecting from read slave.')
|
|
|
|
read_slave.close()
|
|
|
|
|
2014-01-31 01:57:40 +00:00
|
|
|
application.teardown_request(close_db)
|
2014-05-02 01:19:52 +00:00
|
|
|
application.request_class = RequestWithId
|
2014-01-31 01:57:40 +00:00
|
|
|
|
2013-10-01 03:54:12 +00:00
|
|
|
if __name__ == '__main__':
|
2014-05-18 21:19:14 +00:00
|
|
|
logging.config.fileConfig('conf/logging.conf', disable_existing_loggers=False)
|
2014-05-02 01:19:52 +00:00
|
|
|
application.run(port=5000, debug=True, threaded=True, host='0.0.0.0')
|