def __init__(self): """Constructor for the server wrapper.""" #self._app = Flask(__name__) # imports the named package, in this case this file self.__load_config() self._app = Flask(__name__.split(".")[-1], template_folder=self.template_folder) self._app.mongo = db_sync_manager #PyMongo(self._app) self._app.db = "felix_mro" if self.mro_enabled else "felix_ro" # Added in order to be able to execute "before_request" method app = self._app # Setup debugging for app cDebug = self.general_section.get("debug") if cDebug: # log all actions on the XML-RPC interface def log_request(sender, **extra): logger.info(">>> REQUEST %s:\n%s" % (request.path, request.data)) request_started.connect(log_request, self._app) def log_response(sender, response, **extra): logger.info(">>> RESPONSE %s:\n%s" % (response.status, response.data)) request_finished.connect(log_response, self._app) @app.before_request def before_request(): # "Attach" objects within the "g" object. This is passed to each view method g.mongo = self._app.mongo
def _connect_signals(): rs_counter = Counter("web_request_started", "HTTP requests received") request_started.connect(lambda s, *a, **e: rs_counter.inc(), weak=False) rf_counter = Counter("web_request_finished", "HTTP requests successfully handled") request_finished.connect(lambda s, *a, **e: rf_counter.inc(), weak=False) re_counter = Counter("web_request_exception", "Failed HTTP requests") got_request_exception.connect(lambda s, *a, **e: re_counter.inc(), weak=False)
def __init__(self): """Constructor for the server wrapper.""" #self._app = Flask(__name__) # imports the named package, in this case this file # Imports the named module (package includes "." and this is not nice with PyMongo) self.config = ConfParser("flask.conf") self.general_section = self.config.get("general") self.template_folder = self.general_section.get("template_folder") self.fcgi_section = self.config.get("fcgi") self.certificates_section = self.config.get("certificates") self._app = Flask(__name__.split(".")[-1], template_folder = self.template_folder) self._mongo = PyMongo(self._app) # Added in order to be able to execute "before_request" method app = self._app # Setup debugging for app cDebug = self.general_section.get("debug") if cDebug: # log all actions on the XML-RPC interface def log_request(sender, **extra): logger.info(">>> REQUEST %s:\n%s" % (request.path, request.data)) request_started.connect(log_request, self._app) def log_response(sender, response, **extra): logger.info(">>> RESPONSE %s:\n%s" % (response.status, response.data)) request_finished.connect(log_response, self._app) @app.before_request def before_request(): # "Attach" objects within the "g" object. This is passed to each view method g.mongo = self._mongo
def __init__(self): """Constructor for the server wrapper.""" #self._app = Flask(__name__) # imports the named package, in this case this file # Imports the named module (package includes "." and this is not nice with PyMongo) self.config = ConfParser("flask.conf") self.general_section = self.config.get("general") self.template_folder = self.general_section.get("template_folder") self.fcgi_section = self.config.get("fcgi") self.certificates_section = self.config.get("certificates") self._app = Flask(__name__.split(".")[-1], template_folder=self.template_folder) self._mongo = PyMongo(self._app) # Added in order to be able to execute "before_request" method app = self._app # Setup debugging for app cDebug = self.general_section.get("debug") if cDebug: # log all actions on the XML-RPC interface def log_request(sender, **extra): logger.info(">>> REQUEST %s:\n%s" % (request.path, request.data)) request_started.connect(log_request, self._app) def log_response(sender, response, **extra): logger.info(">>> RESPONSE %s:\n%s" % (response.status, response.data)) request_finished.connect(log_response, self._app) @app.before_request def before_request(): # "Attach" objects within the "g" object. This is passed to each view method g.mongo = self._mongo
def setup_signals(cls, app): app.before_first_request(before_first_request_handler) request_started.connect(request_started_handler, app) app.before_request(before_request_handler) got_request_exception.connect(got_request_exception_handler, app) request_finished.connect(request_finished_handler, app) app.teardown_request(teardown_request_handler)
def create_app(): # initialize app app = Flask(__name__) # configure app app.config.from_object('config.' + app.config['ENV'].title() + 'Config') # configure loggings dictConfig( yaml.safe_load(open('logging_config.yaml', 'r'))[app.config['ENV']]) logging.getLogger('sqlalchemy').propagate = False # initialize db # db.init_app(app) # initialize cache cache.init_app(app) # register blueprints app.register_blueprint(apiv1) # after request global app.after_request(_app_after_request) # connect signals request_started.connect(_request_started_handler, app) request_finished.connect(_request_finished_handler, app) return app
def full_dispatch_request_with_instana(wrapped, instance, argv, kwargs): if not hasattr(instance, '_stan_wuz_here'): logger.debug("Applying flask before/after instrumentation funcs") setattr(instance, "_stan_wuz_here", True) got_request_exception.connect(log_exception_with_instana, instance) request_started.connect(request_started_with_instana, instance) request_finished.connect(request_finished_with_instana, instance) return wrapped(*argv, **kwargs)
def full_dispatch_request_with_instana(wrapped, instance, argv, kwargs): got_request_exception.connect(log_exception_with_signoz, instance) request_started.connect(request_started_with_signoz, instance) request_finished.connect(request_finished_with_signoz, instance) instance.teardown_request(teardown_request_with_signoz) return wrapped(*argv, **kwargs)
def init_app(bp, app): print(repr("hello")) app.config['MAX_CONTENT_LENGTH'] = 64 * 1024 * 1024 api = AdaptedApi(bp) serializer = itsdangerous.JSONWebSignatureSerializer(app.config['API_KEY']) login_manager = LoginManager() login_manager.init_app(app) request_finished.connect(when_request_finished, app) @app.errorhandler(404) def page_not_found(e): return jsonify({'error': 'unknown endpoint: %s' % request.base_url}), 404 @app.errorhandler(ApiError) def apierror(e): db.session.rollback() logger.info(e) response = jsonify(e.to_user_json()) response.status_code = e.status_code logger.debug(repr(e.to_user_json())) return response @login_manager.request_loader def load_user_from_request(request): from sidr import models user = None auth_payload = request.headers.get('Authorization') if not auth_payload and request.args.get('auth'): auth_payload = request.args.get('auth') if auth_payload: try: auth_payload = auth_payload.replace('Basic ', '', 1) payload = serializer.loads(auth_payload) user = models.User.get(payload['id']) if user is None: raise AuthError('Unknown user') if payload['signature'] != user.signature: raise AuthError('Object signature is broken') elif user.status != const.STATUS_ACTIVE: raise AuthError('User is not active') else: return user except itsdangerous.SignatureExpired: raise AuthError('Signature expired') except itsdangerous.BadSignature: raise AuthError('Bad Signature') return None return api
def create_app(sqlalchemy_uri=None): from flask import Flask, request_started, request, request_finished, g from flask_bootstrap import Bootstrap from flask_dogpile_cache import DogpileCache import os app = Flask(__name__) app.config.from_object('APITaxi_front.default_settings') if 'APITAXI_CONFIG_FILE' in os.environ: app.config.from_envvar('APITAXI_CONFIG_FILE') if not 'ENV' in app.config: app.logger.error('ENV is needed in the configuration') return None if app.config['ENV'] not in ('PROD', 'STAGING', 'DEV'): app.logger.error("""Here are the possible values for conf['ENV']: ('PROD', 'STAGING', 'DEV') your's is: {}""".format(app.config['env'])) return None #Load configuration from environment variables for k in app.config.keys(): if not k in os.environ: continue app.config[k] = os.environ[k] if sqlalchemy_uri: app.config['SQLALCHEMY_DATABASE_URI'] = sqlalchemy_uri from APITaxi_models import db db.init_app(app) from . import backoffice backoffice.init_app(app) from . import documentation documentation.init_app(app) Bootstrap(app) from APITaxi_utils.version import check_version, add_version_header request_started.connect(check_version, app) request_finished.connect(add_version_header, app) from flask_uploads import configure_uploads from .backoffice.extensions import images configure_uploads(app, (images)) from APITaxi_utils.login_manager import init_app as init_login_manager from .backoffice.forms.login import LoginForm from .extensions import user_datastore init_login_manager(app, user_datastore, LoginForm) from . import demo demo.create_app(app) from APITaxi_models import security user_datastore.init_app(db, security.User, security.CachedUser, security.Role) cache = DogpileCache() cache.init_app(app) return app
def bind(app, endpoint=None): if not endpoint: endpoint = Endpoint(app.name) events.endpoint = endpoint log.info('Attaching zipkin to Flask signals') request_started.connect(events.pre_request, app) request_finished.connect(events.pre_response, app) log.info('zipkin signals attached')
def create_app(): current_app = Flask(__name__) config_secret_key(current_app) config_log(current_app) config_errorhandler(current_app) config_cache(current_app) config_routes(current_app) request_started.connect(log_request, current_app) request_finished.connect(log_response, current_app) return current_app
def init_app(self, app): assert self.STATE_KEY not in app.extensions app.extensions[self.STATE_KEY] = HealthTester(self.configs) app.before_request(self.before_request) request_finished.connect(self.handle_request_finished, app) request_tearing_down.connect(self.handle_request_tearing_down, app) got_request_exception.connect(self.handle_got_request_exception, app) from huskar_api.models.signals import session_load_user_failed session_load_user_failed.connect(self.handle_load_user_failed)
def init_app(bp, app): print(repr("hello")) app.config['MAX_CONTENT_LENGTH'] = 64 * 1024 * 1024 api = AdaptedApi(bp) serializer = itsdangerous.JSONWebSignatureSerializer(app.config['API_KEY']) login_manager = LoginManager() login_manager.init_app(app) request_finished.connect(when_request_finished, app) @app.errorhandler(404) def page_not_found(e): return jsonify({'error': 'unknown endpoint: %s' % request.base_url}), 404 @app.errorhandler(ApiError) def apierror(e): db.session.rollback() logger.info(e) response = jsonify(e.to_user_json()) response.status_code = e.status_code logger.debug(repr(e.to_user_json())) return response @login_manager.request_loader def load_user_from_request(request): from sidr import models user = None auth_payload = request.headers.get('Authorization') if not auth_payload and request.args.get('auth'): auth_payload = request.args.get('auth') if auth_payload: try: auth_payload = auth_payload.replace('Basic ', '', 1) payload = serializer.loads(auth_payload) user = models.User.get(payload['id']) if user is None: raise AuthError('Unknown user') if payload['signature'] != user.signature: raise AuthError('Object signature is broken') elif user.status != const.STATUS_ACTIVE: raise AuthError('User is not active') else: return user except itsdangerous.SignatureExpired: raise AuthError('Signature expired') except itsdangerous.BadSignature: raise AuthError('Bad Signature') return None return api
def init_flask_signals(_app): from fw.db.sql_base import db as sqldb def finish_db_transaction(sender, **extra): sqldb.session.commit() def rollback(sender, **extra): sqldb.session.rollback() from flask import request_finished, got_request_exception request_finished.connect(finish_db_transaction, _app) got_request_exception.connect(rollback, _app)
def connect_signals(app): appcontext_popped.connect(handle_appcontext_popped) appcontext_pushed.connect(handle_appcontext_pushed) appcontext_tearing_down.connect(handle_appcontext_tearing_down) before_render_template.connect(handle_before_render_template, app) got_request_exception.connect(handle_got_request_exception, app) request_finished.connect(handle_request_finished, app) request_started.connect(handle_request_started, sender=app, weak=True) request_tearing_down.connect(handle_request_tearing_down, app, False) template_rendered.connect(handle_template_rendered, sender=app)
def __init__(self): """Constructur for the server wrapper.""" self._app = Flask(__name__) # imports the named package, in this case this file # Setup debugging for app config = pm.getService("config") cDebug = config.get("flask.debug") if cDebug: # log all actions on the XML-RPC interface def log_request(sender, **extra): logger.info(">>> REQUEST %s:\n%s" % (request.path, request.data)) request_started.connect(log_request, self._app) def log_response(sender, response, **extra): logger.info(">>> RESPONSE %s:\n%s" % (response.status, response.data)) request_finished.connect(log_response, self._app)
def create_app(sqlalchemy_uri=None): from .extensions import (db, redis_store, regions, configure_uploads, documents, images) app = Flask(__name__) app.config.from_object('APITaxi.default_settings') if 'APITAXI_CONFIG_FILE' in os.environ: app.config.from_envvar('APITAXI_CONFIG_FILE') if not 'ENV' in app.config: app.logger.error('ENV is needed in the configuration') return None if app.config['ENV'] not in ('PROD', 'STAGING', 'DEV'): app.logger.error("""Here are the possible values for conf['ENV']: ('PROD', 'STAGING', 'DEV') your's is: {}""".format(app.config['env'])) return None #Load configuration from environment variables for k in app.config.keys(): if not k in os.environ: continue app.config[k] = os.environ[k] if sqlalchemy_uri: app.config['SQLALCHEMY_DATABASE_URI'] = sqlalchemy_uri db.init_app(app) redis_store.init_app(app) redis_store.connection_pool.get_connection(0).can_read() from . import backoffice backoffice.init_app(app) from . import api api.init_app(app) from . import documentation documentation.init_app(app) Bootstrap(app) request_started.connect(check_version, app) request_finished.connect(add_version_header, app) configure_uploads(app, (documents, images)) from .utils.login_manager import init_app as init_login_manager init_login_manager(app) from . import demo demo.create_app(app) for region in regions.values(): if not region.is_configured: region.configure(app.config['DOGPILE_CACHE_BACKEND']) from . import tasks tasks.init_app(app) models_committed.connect_via(app)(commit_signal) return app
def subscribe_logging_helpers(app): """ Configure functions that log in response to Flask signals emitted by app. Flask signal function invoked ------------ ------------------- request_started log_request template_rendered log_template_rendered request_finished log_response got_request_exception log_exception """ request_started.connect(log_request, app) template_rendered.connect(log_template_rendered, app) request_finished.connect(log_response, app) got_request_exception.connect(log_exception, app)
def _register_middleware(app): from wonder.common import sqs sqs.init_app(app) def _force_status(response): try: status_code = int(request.headers['X-HTTP-Status-Override']) except (KeyError, ValueError): pass else: response.headers['X-HTTP-Status-Override'] = response.status response.status_code = status_code return response app.after_request(_force_status) request_finished.connect(_dont_set_public_cookie, app)
def subscribe_logging_helpers(app): """ Configure functions that log in response to Flask signals emitted by app. Flask signal function invoked ------------ ------------------- request_started log_request template_rendered log_template_rendered request_finished log_response got_request_exception log_exception """ request_started.connect(log_request, app) template_rendered.connect(log_template_rendered, app) request_finished.connect(log_response, app) got_request_exception.connect(log_exception, app)
def use_storm(app): """Setup Storm.""" app.after_request(close_connection) #アプリが例外を投げるとafter_request呼ばれないので #処理の開始前にもコネクションのロールバックをさせることで #デッドロックを防止する。念のため、シグナルもキャッチして同様に処理する。 app.before_request(close_connection) from flask import request_finished, got_request_exception def request_finished_handler(sender, response): close_connection(response) request_finished.connect(request_finished_handler, app) def got_request_exception_handler(sender, exception): close_connection() got_request_exception.connect(got_request_exception_handler, app)
def use_storm(app): """Setup Storm.""" app.after_request(close_connection) #アプリが例外を投げるとafter_request呼ばれないので #処理の開始前にもコネクションのロールバックをさせることで #デッドロックを防止する。念のため、シグナルもキャッチして同様に処理する。 app.before_request(close_connection) from flask import request_finished, got_request_exception def request_finished_handler(sender, response): close_connection(response) request_finished.connect(request_finished_handler, app) def got_request_exception_handler(sender, exception): close_connection() got_request_exception.connect(got_request_exception_handler, app)
def init_app(self, app): app.config.setdefault('STATSD_RATE', 1) app.config.setdefault('SITE_ID', app.name) app.config.setdefault('STATSD_TAGS', False) app.config.setdefault('STATSD_FORM_LOG', True) app.extensions['statsd'] = self app.extensions['statsd_core'] = StatsClient( host=app.config.setdefault('STATSD_HOST', '127.0.0.1'), port=app.config.setdefault('STATSD_PORT', 8125), prefix=None, maxudpsize=app.config.setdefault('STATSD_MAXUDPSIZE', 512), ipv6=app.config.setdefault('STATSD_IPV6', False), ) if app.config.setdefault('STATSD_REQUEST_LOG', True): # Use signals because they are called before and after all other request # processors, allowing us to capture (nearly) all time taken for processing request_started.connect(self._request_started, app) request_finished.connect(self._request_finished, app)
def __init__(self, flask_app, application_name, sentry, url='/metrics'): self.process = os.getpid() - os.getppid() flask_app.add_url_rule(url, view_func=metrics, methods=['GET']) self.sentry = sentry sentry.init_app(flask_app) # flask_app.config["RAVEN_IGNORE_EXCEPTIONS"] = [AppError] self.wsgi_app = ProxyFix(flask_app.wsgi_app) self.req_counter = Counter('%s_requests_total' % application_name, 'Total request counts', ['method', 'endpoint', 'process']) self.err_counter = Counter('%s_error_total' % application_name, 'Total error counts', ['method', 'endpoint', 'process']) self.resp_latency = Histogram( '%s_response_latency_millisecond' % application_name, 'Response latency (millisecond)', ['method', 'endpoint', 'process'], buckets=(10, 20, 30, 50, 80, 100, 200, 300, 500, 1000, 2000, 3000)) request_started.connect(self._log_request, flask_app) request_finished.connect(self._log_response, flask_app)
def create_app(config_name): """ Creates the Flask Application object :param config_name: configuration name - dev, test etc :return: Flask object """ flask_app = Flask(__name__) log_ = MyLogger() flask_app.config.from_object(config_by_name[config_name]) flask_app.logger.setLevel(log_.info) flask_app.logger.addHandler(log_.logHandler) # this is for json formatted logging. Not working properly. # json_logging.ENABLE_JSON_LOGGING = True # json_logging.init_flask() # json_logging.init_request_instrument(flask_app) flask_app.register_blueprint(zoo_blueprint) request_started.connect(log_request, flask_app) request_finished.connect(log_response, flask_app) return flask_app
def __init__(self): """Constructor for the server wrapper.""" #self._app = Flask(__name__) # imports the named package, in this case this file self.__load_config() self._app = Flask(__name__.split(".")[-1], template_folder = self.template_folder) self._app.mongo = db_sync_manager #PyMongo(self._app) self._app.db = "felix_mro" if self.mro_enabled else "felix_ro" # Added in order to be able to execute "before_request" method app = self._app # Setup debugging for app cDebug = self.general_section.get("debug") if cDebug: # log all actions on the XML-RPC interface def log_request(sender, **extra): logger.info(">>> REQUEST %s:\n%s" % (request.path, request.data)) request_started.connect(log_request, self._app) def log_response(sender, response, **extra): logger.info(">>> RESPONSE %s:\n%s" % (response.status, response.data)) request_finished.connect(log_response, self._app) @app.before_request def before_request(): # "Attach" objects within the "g" object. This is passed to each view method g.mongo = self._app.mongo
def create_app(sqlalchemy_uri=None): from .extensions import redis_store, user_datastore app = Flask(__name__) app.config.from_object('APITaxi.default_settings') if 'APITAXI_CONFIG_FILE' in os.environ: app.config.from_envvar('APITAXI_CONFIG_FILE') if not 'ENV' in app.config: app.logger.error('ENV is needed in the configuration') return None if app.config['ENV'] not in ('PROD', 'STAGING', 'DEV'): app.logger.error("""Here are the possible values for conf['ENV']: ('PROD', 'STAGING', 'DEV') your's is: {}""".format(app.config['env'])) return None #Load configuration from environment variables for k in app.config.keys(): if not k in os.environ: continue app.config[k] = os.environ[k] if sqlalchemy_uri: app.config['SQLALCHEMY_DATABASE_URI'] = sqlalchemy_uri from APITaxi_models import db db.init_app(app) redis_store.init_app(app) redis_store.connection_pool.get_connection(0).can_read() from . import api api.init_app(app) from APITaxi_utils.version import check_version, add_version_header request_started.connect(check_version, app) request_finished.connect(add_version_header, app) from flask.ext.uploads import configure_uploads from .api.extensions import documents configure_uploads(app, (documents,)) from APITaxi_utils.login_manager import init_app as init_login_manager init_login_manager(app, user_datastore, None) from . import tasks tasks.init_app(app) from APITaxi_models import security user_datastore.init_app(db, security.User, security.CachedUser, security.Role) cache = DogpileCache() cache.init_app(app) @app.before_first_request def warm_up_redis(): not_available = set() available = set() cur = db.session.connection().connection.cursor() cur.execute(""" SELECT taxi.id AS taxi_id, vd.status, vd.added_by FROM taxi LEFT OUTER JOIN vehicle ON vehicle.id = taxi.vehicle_id LEFT OUTER JOIN vehicle_description AS vd ON vehicle.id = vd.vehicle_id """) users = {u.id: u.email for u in security.User.query.all()} for taxi_id, status, added_by in cur.fetchall(): user = users.get(added_by) taxi_id_operator = "{}:{}".format(taxi_id, user) if status == 'free': available.add(taxi_id_operator) else: not_available.add(taxi_id_operator) to_remove = list() cursor, keys = redis_store.sscan(app.config['REDIS_NOT_AVAILABLE'], 0) keys = set(keys) while cursor != 0: to_remove.extend(keys.intersection(available)) not_available.difference_update(keys) cursor, keys = redis_store.sscan(app.config['REDIS_NOT_AVAILABLE'], cursor) keys = set(keys) if len(to_remove) > 0: redis_store.srem(app.config['REDIS_NOT_AVAILABLE'], to_remove) if len(not_available) > 0: redis_store.sadd(app.config['REDIS_NOT_AVAILABLE'], *not_available) from APITaxi_models.hail import HailLog def delete_redis_keys(response): from flask import g if not hasattr(g, 'keys_to_delete'): return response redis_store.delete(*g.keys_to_delete) return response app.after_request_funcs.setdefault(None, []).append( HailLog.after_request(redis_store) ) app.after_request_funcs.setdefault(None, []).append( delete_redis_keys ) return app
sid = online_users.get(unicode(msg_data.get('to_id'))) #print online_users #print sid if sid is not None: sio.send(msg_data, json=True, room=sid) return u'消息发送成功' else: return u'用户不在线' def request_finished_subscriber(*args, **kwargs): # print args, kwargs print 'request_finished_signal' request_finished.connect(request_finished_subscriber, app) @template_rendered.connect def template_rendered_subscriber(sender, template, context, **extra): # print template pass @sio.on('connect') def on_connect(): """ socket.io 连接事件 """ online_users[session.get('user').get('id')] = request.sid send('connect')
# 获取环境变量 env = os.getenv('env') if env: env = env.lower() app = Flask(__name__) conf = getenvconf(env) app.config['SESSION_TYPE'] = 'filesystem' app.config['SESSION_FILE_DIR'] = conf.SESSION_DIR app.config.from_object(conf) app.mysqldb = init_mysqldb(conf.MYSQL_MAIN) app.mmwddb = init_mmwddb(conf.MYSQL_MMWD) app.neardb = init_mmwddb(conf.MYSQL_NEAR) app.mongodb = init_mongo(conf.MONGODB_HOST_R, conf.MONGODB_PORT_R) app.psql = init_postgre(conf.PSQL_DB, conf.PSQL_USER, conf.PSQL_PW, conf.PSQL_HOST) app.mongodb_for_write = init_mongo(conf.MONGODB_HOST_W, conf.MONGODB_PORT_W) app.deliveryhandler = DeliveryHandler(app_code=conf.DELIVERY_APP_CODE, server_key=conf.DELIVERY_SERVER_KEY, host=conf.DELIVERY_HOST, port=conf.DELIVERY_PORT) from urls import urlsmap urlsmap(app) # 支持Gunicorn app.wsgi_app = ProxyFix(app.wsgi_app) Session(app) socketio = SocketIO(app) #from socketevent import test from socketevent import showtime from utils.qfrequest import send_remote_log_tail, handle_before_request request_started.connect(handle_before_request, app) request_finished.connect(send_remote_log_tail, app)
def create_app(sqlalchemy_uri=None): from .extensions import redis_store, redis_store_saved, user_datastore app = Flask(__name__) app.config.from_object('APITaxi.default_settings') if 'APITAXI_CONFIG_FILE' in os.environ: app.config.from_envvar('APITAXI_CONFIG_FILE') if not 'ENV' in app.config: app.logger.error('ENV is needed in the configuration') return None if app.config['ENV'] not in ('PROD', 'STAGING', 'DEV'): app.logger.error("""Here are the possible values for conf['ENV']: ('PROD', 'STAGING', 'DEV') your's is: {}""".format(app.config['env'])) return None #Load configuration from environment variables for k in list(app.config.keys()): if not k in os.environ: continue app.config[k] = os.environ[k] if sqlalchemy_uri: app.config['SQLALCHEMY_DATABASE_URI'] = sqlalchemy_uri from APITaxi_models import db, security, HailLog db.init_app(app) redis_store.init_app(app) redis_store.connection_pool.get_connection(0).can_read() redis_store_saved.init_app(app) redis_store_saved.connection_pool.get_connection(0).can_read() from . import api api.init_app(app) from APITaxi_utils.version import check_version, add_version_header request_started.connect(check_version, app) request_finished.connect(add_version_header, app) from flask_uploads import configure_uploads from .api.extensions import documents configure_uploads(app, (documents, )) from APITaxi_utils.login_manager import init_app as init_login_manager init_login_manager(app, user_datastore, None) from . import tasks tasks.init_app(app) user_datastore.init_app(db, security.User, security.Role) @app.before_first_request def warm_up_redis(): from APITaxi.commands.warm_up_redis import warm_up_redis_func warm_up_redis_func(app, db, security.User, redis_store) def delete_redis_keys(response): from flask import g if not hasattr(g, 'keys_to_delete'): return response redis_store.delete(*g.keys_to_delete) return response app.after_request_funcs.setdefault(None, []).append( HailLog.after_request(redis_store_saved)) app.after_request_funcs.setdefault(None, []).append(delete_redis_keys) return app
def signal(app): request_started.connect(log_request, app) request_finished.connect(log_response, app) got_request_exception.connect(log_exception, app)
def create_app(config_name): app = Flask(__name__) app.config.from_object(config[config_name]) config[config_name].init_app(app) # excel.init_excel(app) # bootstrap.init_app(app) mail.init_app(app) moment.init_app(app) db.init_app(app) login_manager.init_app(app) # pagedown.init_app(app) configure_uploads( app, (uploaded_papers, uploaded_papers_with_docx, uploaded_temppapers)) # JWT jwt = JWT(app, authenticate, identity) # celery.conf.update(app.config) # class ContextTask(celery.Task): # def __call__(self, *args, **kwargs): # with app.app_context(): # return self.run(*args, **kwargs) # celery.Task = ContextTask # toolbar.init_app(app) # db_admin.init_app(app) # convert unicode to string # app.jinja_env.filters['split'] = str.split # app.jinja_env.filters['str'] = str # app.jinja_env.filters['date_thedaybefore'] = format_date_thedaybefore # app.jinja_env.filters['date'] = format_date # app.jinja_env.filters['unix_time'] = time.mktime # app.jinja_env.filters['product_has_sold'] = product_has_sold # add test equalto which is included in 2.8 # app.jinja_env.tests['equalto'] = lambda value, other: value == other # import function # app.jinja_env.globals.update( # check_date=check_date, # generate_navigation_bar=generate_navigation_bar, # timestamp=timestamp) # flask admin # admin = Admin(app, name='', template_mode='bootstrap3') # from .modules.admin.views import UserView, PaperView, AuthorView, \ # ConferenceView # with warnings.catch_warnings(): # warnings.filterwarnings( # 'ignore', 'Fields missing from ruleset', UserWarning) # admin.add_view(UserView(db.session, name='Users', # endpoint='admin_user')) # admin.add_view(PaperView(db.session, name='Papers', # endpoint='admin_paper')) # admin.add_view(AuthorView(db.session, name='Author', # endpoint='admin_author')) # admin.add_view(ConferenceView(db.session, name='Conference', # endpoint='admin_conference')) # inject stripe key # with app.app_context(): # stripe.api_key = app.config['STRIPE_SECRET_KEY'] @app.context_processor def processor(): return { 'PERMANENT_SESSION_LIFETIME_MS': (app.permanent_session_lifetime.seconds * 1000), 'generate_timeout_token': generate_timeout_token, 'debug': app.debug, } if config_name == 'production': pass else: @app.context_processor def override_url_for(): return dict(url_for=dated_url_for) def dated_url_for(endpoint, **values): if endpoint == 'static': filename = values.get('filename', None) if filename: file_path = os.path.join(app.root_path, endpoint, filename) values['q'] = int(os.stat(file_path).st_mtime) return url_for(endpoint, **values) @app.before_request def record_request(): g.request_start_time = time.time() # log all the request and response from .utils.request_log import add_request request_finished.connect(add_request, app) # Add current_app.permanent_session_lifetime converted to milliseconds # to context. The config variable PERMANENT_SESSION_LIFETIME is not # used because it could be either a timedelta object or an integer # representing seconds. # current_endpoint is string representing current endpoint #from .modules.main import main as main_blueprint #app.register_blueprint(main_blueprint) #from .modules.tasks import tasks as tasks_blueprint #app.register_blueprint(tasks_blueprint) from .modules.auth import auth as auth_blueprint app.register_blueprint(auth_blueprint, url_prefix='/auth') #from .modules.conf_admin import conf_admin as conf_admin_blueprint #app.register_blueprint(conf_admin_blueprint, url_prefix='/conf_admin') # from modules.db_admin import db_admin as db_admin_module # db_admin.add_view(db_admin_module) #from .modules.submission import submission as submission_blueprint #app.register_blueprint(submission_blueprint, url_prefix='/submission') #from .modules.review import review as review_blueprint #app.register_blueprint(review_blueprint, url_prefix='/review') #from .modules.paper import paper as paper_blueprint #app.register_blueprint(paper_blueprint, url_prefix='/paper') #from .modules.conference import conference as conference_blueprint #app.register_blueprint(conference_blueprint, url_prefix='/conference') #from .modules.website import website as website_blueprint #app.register_blueprint(website_blueprint, url_prefix='/website') from .api import api as api_blueprint app.register_blueprint(api_blueprint, url_prefix='/api') return app
stats_db.incr(view.__name__ + '/hits') def observe_request_finished(sender, response, **extra): try: view = current_app.view_functions[request.endpoint] except KeyError: return stats_db.incr('%s/responses/%d' % (view.__name__, response.status_code)) stats_db.incr('%s/responses_size' % view.__name__, len(response.get_data())) if stats_db: from flask import request_started, request_finished request_started.connect(observe_request_started) request_finished.connect(observe_request_finished) def get_stats(): stats = dict(version=potstats2.__version__) if stats_db: for key in stats_db.keys('*'): key = key.decode() parts = key.split('/') insert_into = stats for part in parts[:-1]: try: insert_into = insert_into[part] except KeyError: # Hint: Python multissignment is right-to-left insert_into[part] = insert_into = {}
def __init__(self, flask_app, project_name, statsd_client): self.wsgi_app = flask_app.wsgi_app self.project_name = project_name self.statsd_client = statsd_client request_started.connect(self._log_request, flask_app) request_finished.connect(self._log_response, flask_app)
flash(msg, 'success') flask_user.signals.user_confirmed_email.connect(handle_trip_plan_migration, app) return flask_user.views.confirm_email(token) # User has finished the registration form but not yet confirmed their email. @app.route('/registration_complete') def registration_complete(): return render_template('flask_user/registration_complete.html') user_db_adapter = flask_user.SQLAlchemyAdapter(db, user_module.User) user_manager = flask_user.UserManager(user_db_adapter, app, register_form=user_module.TCRegisterForm, register_view_function=register, login_view_function=login, confirm_email_view_function=confirm_email) def log_response(send, response, **kwargs): request_logging.log_request(request, response, g.get('session_info')) request_finished.connect(log_response, app) @app.route('/event') def event(): request_logging.log_interaction(request, g.get('session_info'), request.args.get('name'), request.args.get('location'), request.args.get('value')) return '', 204 if __name__ == '__main__': app.debug = constants.DEBUG app.run()
def create_app(sqlalchemy_uri=None): from .extensions import redis_store, redis_store_haillog, user_datastore app = Flask(__name__) app.config.from_object('APITaxi.default_settings') if 'APITAXI_CONFIG_FILE' in os.environ: app.config.from_envvar('APITAXI_CONFIG_FILE') if not 'ENV' in app.config: app.logger.error('ENV is needed in the configuration') return None if app.config['ENV'] not in ('PROD', 'STAGING', 'DEV'): app.logger.error("""Here are the possible values for conf['ENV']: ('PROD', 'STAGING', 'DEV') your's is: {}""".format(app.config['env'])) return None #Load configuration from environment variables for k in app.config.keys(): if not k in os.environ: continue app.config[k] = os.environ[k] if sqlalchemy_uri: app.config['SQLALCHEMY_DATABASE_URI'] = sqlalchemy_uri from APITaxi_models import db db.init_app(app) redis_store.init_app(app) redis_store.connection_pool.get_connection(0).can_read() redis_store_haillog.init_app(app) redis_store_haillog.connection_pool.get_connection(0).can_read() from . import api api.init_app(app) from APITaxi_utils.version import check_version, add_version_header request_started.connect(check_version, app) request_finished.connect(add_version_header, app) from flask_uploads import configure_uploads from .api.extensions import documents configure_uploads(app, (documents,)) from APITaxi_utils.login_manager import init_app as init_login_manager init_login_manager(app, user_datastore, None) from . import tasks tasks.init_app(app) from APITaxi_models import security user_datastore.init_app(db, security.User, security.CachedUser, security.Role) cache = DogpileCache() cache.init_app(app) @app.before_first_request def warm_up_redis(): from APITaxi.commands.warm_up_redis import warm_up_redis_func warm_up_redis_func(app, db, security.User, redis_store) from APITaxi_models.hail import HailLog def delete_redis_keys(response): from flask import g if not hasattr(g, 'keys_to_delete'): return response redis_store.delete(*g.keys_to_delete) return response app.after_request_funcs.setdefault(None, []).append( HailLog.after_request(redis_store_haillog) ) app.after_request_funcs.setdefault(None, []).append( delete_redis_keys ) return app
app = Flask(__name__) conf = getenvconf(env) app.config['SESSION_TYPE'] = 'filesystem' app.config['SESSION_FILE_DIR'] = conf.SESSION_DIR app.config.from_object(conf) app.mysqldb = init_mysqldb(conf.MYSQL_MAIN) app.mmwddb = init_mmwddb(conf.MYSQL_MMWD) app.neardb = init_mmwddb(conf.MYSQL_NEAR) app.mongodb = init_mongo(conf.MONGODB_HOST_R, conf.MONGODB_PORT_R) app.psql = init_postgre(conf.PSQL_DB, conf.PSQL_USER, conf.PSQL_PW, conf.PSQL_HOST) app.mongodb_for_write = init_mongo(conf.MONGODB_HOST_W, conf.MONGODB_PORT_W) app.deliveryhandler = DeliveryHandler(app_code=conf.DELIVERY_APP_CODE, server_key=conf.DELIVERY_SERVER_KEY, host=conf.DELIVERY_HOST, port=conf.DELIVERY_PORT) from urls import urlsmap urlsmap(app) # 支持Gunicorn app.wsgi_app = ProxyFix(app.wsgi_app) Session(app) socketio = SocketIO(app) #from socketevent import test from socketevent import showtime from utils.qfrequest import send_remote_log_tail, handle_before_request request_started.connect(handle_before_request, app) request_finished.connect(send_remote_log_tail, app)
app.logger.setLevel(10) # get fmt string from config if app.config.get("LOG_FORMAT"): log_fmt_str = app.config["LOG_FORMAT"] else: log_fmt_str = "[{levelname}] {asctime} [{name}] {msg}" log_fmt = logging.Formatter(fmt=log_fmt_str, style="{") app.logger.handlers[0].setFormatter(log_fmt) # file logging log_path = Path(app.config["APP_LOGDIR"], app.config["APP_LOGFILE"]) if not log_path.exists(): log_path.touch() fhandler = logging.handlers.RotatingFileHandler(str(log_path), maxBytes=48600, backupCount=3, delay=True) fhandler.setLevel(10) fhandler.setFormatter(log_fmt) if not app.debug: # Only log to file if not in debug i.e. dev mode app.logger.addHandler(fhandler) app.logger.info("Log file path: {}{}".format(Fore.CYAN, log_path)) # add template globals to blueprints admin_bp.add_app_template_global(get_auth_token, name="get_auth_token") admin_bp.add_app_template_global(get_all_series, name="all_series") request_finished.connect(no_cookie)
def init_app(self, app, sessionstore=None, register_blueprint=True): """Flask application initialization. The following actions are executed: #. Initialize the configuration. #. Monkey-patch Flask-Security. #. Create the user datastore. #. Create the sessionstore. #. Initialize the extension, the forms to register users and confirms their emails, the CLI and, if ``ACCOUNTS_USE_CELERY`` is ``True``, register a celery task to send emails. #. Override Flask-Security's default login view function. #. Warn if inconsistent configuration is detected :param app: The Flask application. :param sessionstore: store for sessions. Passed to ``flask-kvsession``. If ``None`` then Redis is configured. (Default: ``None``) :param register_blueprint: If ``True``, the application registers the blueprints. (Default: ``True``) """ self.init_config(app) # Monkey-patch Flask-Security InvenioAccounts.monkey_patch_flask_security() # Create user datastore if not self.datastore: self.datastore = SessionAwareSQLAlchemyUserDatastore( db, User, Role) if app.config["ACCOUNTS_SESSION_ACTIVITY_ENABLED"]: self._enable_session_activity(app=app) # Initialize extension. _register_blueprint = app.config.get("ACCOUNTS_REGISTER_BLUEPRINT") if _register_blueprint is not None: register_blueprint = _register_blueprint state = self.security.init_app(app, datastore=self.datastore, register_blueprint=register_blueprint) # Override Flask-Security's default login view function new_login_view = obj_or_import_string( app.config.get("ACCOUNTS_LOGIN_VIEW_FUNCTION")) if new_login_view is not None: app.view_functions["security.login"] = new_login_view self.register_anonymous_identity_loader(state) app.extensions["security"].register_form = register_form_factory( app.extensions["security"].register_form, app) app.extensions[ "security"].confirm_register_form = confirm_register_form_factory( app.extensions["security"].confirm_register_form, app) app.extensions["security"].login_form = login_form_factory( app.extensions["security"].login_form, app) if app.config["ACCOUNTS_USE_CELERY"]: from invenio_accounts.tasks import send_security_email @state.send_mail_task def delay_security_email(msg): send_security_email.delay(msg.__dict__) # Register context processor if app.config["ACCOUNTS_JWT_DOM_TOKEN"]: from invenio_accounts.context_processors.jwt import jwt_proccessor app.context_processor(jwt_proccessor) # Register signal receiver if app.config.get("ACCOUNTS_USERINFO_HEADERS"): request_finished.connect(set_session_info, app) # Set Session KV store session_kvstore_factory = obj_or_import_string( app.config["ACCOUNTS_SESSION_STORE_FACTORY"]) session_kvstore = session_kvstore_factory(app) self.kvsession_extension = KVSessionExtension(session_kvstore, app) self.check_configuration_consistency(app) app.extensions["invenio-accounts"] = self
def init_app(self, app, sessionstore=None, register_blueprint=True): """Flask application initialization. The following actions are executed: #. Initialize the configuration. #. Monkey-patch Flask-Security. #. Create the user datastore. #. Create the sessionstore. #. Initialize the extension, the forms to register users and confirms their emails, the CLI and, if ``ACCOUNTS_USE_CELERY`` is ``True``, register a celery task to send emails. :param app: The Flask application. :param sessionstore: store for sessions. Passed to ``flask-kvsession``. If ``None`` then Redis is configured. (Default: ``None``) :param register_blueprint: If ``True``, the application registers the blueprints. (Default: ``True``) """ self.init_config(app) # Monkey-patch Flask-Security InvenioAccounts.monkey_patch_flask_security() # Create user datastore if not self.datastore: self.datastore = SessionAwareSQLAlchemyUserDatastore( db, User, Role) if app.config['ACCOUNTS_SESSION_ACTIVITY_ENABLED']: self._enable_session_activity(app=app) # Initialize extension. _register_blueprint = app.config.get('ACCOUNTS_REGISTER_BLUEPRINT') if _register_blueprint is not None: register_blueprint = _register_blueprint state = self.security.init_app(app, datastore=self.datastore, register_blueprint=register_blueprint) self.register_anonymous_identity_loader(state) app.extensions['security'].register_form = register_form_factory( app.extensions['security'].register_form, app) app.extensions['security'].confirm_register_form = \ confirm_register_form_factory( app.extensions['security'].confirm_register_form, app ) app.extensions['security'].login_form = login_form_factory( app.extensions['security'].login_form, app) if app.config['ACCOUNTS_USE_CELERY']: from invenio_accounts.tasks import send_security_email @state.send_mail_task def delay_security_email(msg): send_security_email.delay(msg.__dict__) # Register context processor if app.config['ACCOUNTS_JWT_DOM_TOKEN']: from invenio_accounts.context_processors.jwt import \ jwt_proccessor app.context_processor(jwt_proccessor) # Register signal receiver if app.config.get('ACCOUNTS_USERINFO_HEADERS'): request_finished.connect(set_session_info, app) app.extensions['invenio-accounts'] = self
proto = request.environ.get('SERVER_PROTOCOL') msg = request.method + ' ' + request.path + ' ' + proto code = str(code) if code[0] == '1': # 1xx - Informational msg = color(msg, attrs=['bold']) if code[0] == '2': # 2xx - Success msg = color(msg, color='white') elif code == '304': # 304 - Resource Not Modified msg = color(msg, color='cyan') elif code[0] == '3': # 3xx - Redirection msg = color(msg, color='green') elif code == '404': # 404 - Resource Not Found msg = color(msg, color='yellow') elif code[0] == '4': # 4xx - Client Error msg = color(msg, color='red', attrs=['bold']) else: # 5xx, or any other response msg = color(msg, color='magenta', attrs=['bold']) logger.info('%s - - [%s] "%s" %s', request.remote_addr, log_date_time_string(), msg, code) def log_response(sender, response, **extra): log_request(response.status_code) app.config.from_object('config.default') database.init_app(app) setup_error_mail(app) request_finished.connect(log_response, app) app.wsgi_app = ProxyFix(app.wsgi_app, x_host=1)
def __init__(self, flask_app, project_name, statsd_client): self.wsgi_app = flask_app.wsgi_app self.project_name = project_name self.statsd_client = statsd_client request_started.connect(self._log_request, flask_app) request_finished.connect(self._log_response, flask_app)
from flask import Flask, jsonify, g, request_finished from flask.signals import signals_available if not signals_available: raise RuntimeError("pip install blinker") app = Flask(__name__) def finished(sender, response, **extra): print('About to send a Response') print(response) request_finished.connect(finished) @app.route('/api') def my_microservice(): return jsonify({'Hello': 'World'}) if __name__ == '__main__': app.run()
''' initialize app defined by blueprint in apis.py ''' from .apis import blueprint from main import app_factory from flask import (Flask, request_finished) from database import Session import config app = Flask(__name__) app = app_factory(config.Config, config.project_name) app.register_blueprint(blueprint) # remove session when connection completed def remove_session(sender, response, **extra): Session.remove() request_finished.connect(remove_session, app)