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 create_app(object_name): install_logger() app = Flask(__name__) app.config.from_object(object_name) # Per request logging request_started.connect(log_request, app) template_rendered.connect(log_template, app) # CSRF protection csrf.init_app(app) routes.prefix = app.config.get('URL_PREFIX') routes.host = app.config.get('CLIENT_FACING_URL').strip('/') app.routes = routes from .main import create_module as main_create_module from .yourdetails import create_module as yourdetails_create_module from .verification import create_module as verification_create_module main_create_module(app) yourdetails_create_module(app) verification_create_module(app) return app
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 __init__(self, app): try: # check if the client has Flask-Login from flask.ext.login import current_user request_started.connect(audit_user, app) except: request_started.connect(audit_anon, app)
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 __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 __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 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 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 setup(app): # __enter__ request_started.connect(auth_onrequest, app) @app.context_processor def auth_context_processor(): try: return {'user': g.user } except AttributeError: return {} yield 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 init_app(self, app, context_generators={}, report_exceptions=False): """ Initialize honeybadger and listen for errors :param app: the Flask application object. :param context_generators: a dictionary with key the name of additional context property to add and value a callable that generates the actual value of the property. :param bool report_exceptions: whether to automatically report exceptions on requests or not. """ self.context_generators = context_generators self.report_exceptions = report_exceptions self.initialize_honeybadger(app.config) self._patch_generic_request_payload() self.skip_headers = set(csv_to_list(app.config.get('HONEYBADGER_EXCLUDE_HEADERS', DEFAULT_SKIP_HEADERS))) request_started.connect(self.setup_context, sender=app, weak=False) request_tearing_down.connect(self.reset_context, sender=app, weak=False) logger.info('Honeybadger Flask helper installed') if self.report_exceptions: logger.info('Enabling auto-reporting exceptions') got_request_exception.connect(self._handle_exception, sender=app, weak=False)
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 mdk_setup(app, timeout=None, mdk=None): """ Setup MDK integration with Flask. :param app: A Flask application instance. :param timeout: Default timeout in seconds to set for the MDK session. :param mdk: An optional ``mdk.MDK`` instance to use instead of creating a new one. It will not be started or stopped. :return: The ``mdk.MDK`` instance. """ if mdk is None: app.mdk = start() atexit.register(app.mdk.stop) else: app.mdk = mdk if timeout is not None: app.mdk.setDefaultDeadline(timeout) request_started.connect(_on_request_started, app) got_request_exception.connect(_on_request_exception, app) request_tearing_down.connect(_on_request_tearing_down, app) return app.mdk
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(): app = Flask(__name__, template_folder=u"templates") app.config.from_object(u'config') db.init_app(app) login_manager.init_app(app) login_manager.user_loader(get_user) register_routes(app) request_started.connect(request_started_handler, app) stripe.api_key = app.config.get(u"STRIPE_API_KEY") assets = Environment(app) js = Bundle(u"js/libs/react-0.10.0.js", u"js/libs/JSXTransformer-0.10.0.js", u"js/libs/proj4.js", u"js/libs/leaflet.js", u"js/libs/jquery-1.11.1.min.js", u"js/libs/bootstrap/dropdown.js", u"js/libs/bootstrap/transition.js", u"js/libs/bootstrap/collapse.js", output=u"gen/libs.js") assets.register(u'js', js) jsx = Bundle(u"js/app.js", output=u"gen/app_jsx.js") assets.register(u'jsx', jsx) css = Bundle(u"stylesheets/styles.css", u"stylesheets/libs/hint.css", u"stylesheets/libs/leaflet.css", output=u"gen/stylesheets.css") assets.register(u'css', css) return app
## UTILS def jsonify(data): """ We redefine the Flask jsonify function to be able to use list as primary structure """ return app.response_class(json.dumps(data, indent=None if request.is_xhr else 2), mimetype='application/json') def merge_params(sender, **extra): if request.json != None: from werkzeug.utils import CombinedMultiDict request.values = CombinedMultiDict([request.values, request.json]) request_started.connect(merge_params, app) def query_with_pagination(query, req): from math import ceil # Get the total count of objects count = query.count() # Query with pagination #objects = query.limit(req['page_limit']).offset(req['page_offset']).all() objects = query.all() # Create a result dictionary results = dict() results['objects'] = objects
import logging from logging import Formatter from logging.handlers import TimedRotatingFileHandler website_log = '%s/logs/website.log' % os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)) file_handler = TimedRotatingFileHandler(website_log, 'W0', 1, backupCount=7) file_handler.suffix = '%Y%m%d-%H%M' file_handler.setLevel(logging.INFO) file_handler.setFormatter(Formatter('%(asctime)s %(levelname)s: %(message)s')) app.logger.addHandler(file_handler) app.logger.setLevel(logging.INFO) from flask import request_started, got_request_exception from website.helpers import log_request, log_exception request_started.connect(log_request, app) got_request_exception.connect(log_exception, app) from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy(app) from flask.ext.login import LoginManager login_manager = LoginManager() login_manager.init_app(app) from flask_wtf.csrf import CsrfProtect CsrfProtect(app) import website.views from website.account.views import account
def connect_request(app): for func in [bind_ua]: request_started.connect(func, app)
# 获取环境变量 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 setup(self, config): self.configure(config) # At this point we have loaded all external config files: # SQLALCHEMY_DATABASE_URI is definitively fixed (it cannot be defined in # database AFAICT), and LOGGING_FILE cannot be set in DB settings. self.setup_logging() appcontext_pushed.connect(self.install_id_generator) if not self.testing: self.init_sentry() # time to load config bits from database: 'settings' # First init required stuff: db to make queries, and settings service extensions.db.init_app(self) settings_service.init_app(self) self.register_jinja_loaders(jinja2.PackageLoader("abilian.web")) self.init_assets() self.install_default_handlers() with self.app_context(): self.init_extensions() self.register_plugins() self.add_access_controller( "static", allow_access_for_roles(Anonymous), endpoint=True ) # debugtoolbar: this is needed to have it when not authenticated # on a private site. We cannot do this in init_debug_toolbar, # since auth service is not yet installed. self.add_access_controller( "debugtoolbar", allow_access_for_roles(Anonymous) ) self.add_access_controller( "_debug_toolbar.static", allow_access_for_roles(Anonymous), endpoint=True, ) # TODO: maybe reenable later # self.maybe_register_setup_wizard() self._finalize_assets_setup() # At this point all models should have been imported: time to configure # mappers. Normally Sqlalchemy does it when needed but mappers may be # configured inside sa.orm.class_mapper() which hides a # misconfiguration: if a mapper is misconfigured its exception is # swallowed by class_mapper(model) results in this laconic # (and misleading) message: "model is not mapped" sa.orm.configure_mappers() signals.components_registered.send(self) request_started.connect(self.setup_nav_and_breadcrumbs) init_hooks(self) # Initialize Abilian core services. # Must come after all entity classes have been declared. # Inherited from ServiceManager. Will need some configuration love # later. if not self.testing: with self.app_context(): self.start_services() setup(self)
return 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
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
if 'TEST_DATA_ROOT' in os.environ: TEST_DATA_ROOT = os.environ['TEST_DATA_ROOT'] STATIC_FOLDER = os.path.join(u'..', TEST_DATA_ROOT, u'assets') TEMPLATE_FOLDER = os.path.join(u'..', TEST_DATA_ROOT, u'templates') app = Flask(__name__, static_folder=os.path.join(u'..', TEST_DATA_ROOT, u'assets'), template_folder=os.path.join(u'..', TEST_DATA_ROOT, u'templates') ) else: app = Flask(__name__, static_folder='assets') app.config.from_pyfile('settings.py') app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True from website.site import Site site = Site() import website.route import website.filter # load cached resources on each request if they are outdated request_started.connect(site.load_menus, app) # flush request error messages after request request_tearing_down.connect(site.flush_errors, app)
from flask import Flask, render_template, request_started app = Flask(__name__) app.secret_key = 'YOUR_SECRET_HERE' app.config['SALT'] = 'YOUR_SALT_HERE' def setup_static(sender): if app.config['DEBUG']: from werkzeug import SharedDataMiddleware import os app.wsgi_app = SharedDataMiddleware(app.wsgi_app, { '/': os.path.join(os.path.dirname(__file__), 'static') }) request_started.connect(setup_static, app) @app.errorhandler(404) def page_not_found(error): return render_template('errors/404.html'), 404 from helpers import * from context_processors import * from hooks import * from flog.views.admin import admin from flog.views.blog import blog from flog.views.api import metaweblog, blogger app.register_module(admin) app.register_module(blog)
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
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)
# setup logging with open('%s/config/%s-logging.yaml' % (app.root_path, env)) as f: import yaml logging.config.dictConfig(yaml.load(f)) # attach the user id to logs from flask import request_started, session from logs import UserIdFilter def log_attach_user_id(sender, **extra): UserIdFilter.set_userid(session.get('user_id', '-')) request_started.connect(log_attach_user_id, app) # setup templates and haml from flask.ext.mako import MakoTemplates, _lookup, render_template import haml MakoTemplates(app) app.config['MAKO_PREPROCESSOR'] = haml.preprocessor app.config['MAKO_TRANSLATE_EXCEPTIONS'] = False app.config['MAKO_DEFAULT_FILTERS'] = ['decode.utf8'] # CSRF protection from flask_wtf.csrf import CsrfProtect CsrfProtect(app) # Database from flask.ext.sqlalchemy import SQLAlchemy
from dexter.authn import AnonymousUser login_manager.anonymous_user = AnonymousUser @login_manager.user_loader def load_user(userid): from .models import User user = User.query.get(userid) # don't allow disabled users if user and user.disabled: user = None return user # htpasswd-based basic auth for API access from flask.ext import htauth app.config['HTAUTH_HTPASSWD_PATH'] = './resources/nginx/htpasswd' app.config['HTAUTH_REALM'] = 'Dexter' htauth.HTAuth(app) # attach the user id to logs from flask import request_started, session from logs import UserIdFilter def log_attach_user_id(sender, **extra): UserIdFilter.set_userid(session.get('user_id', '-')) request_started.connect(log_attach_user_id, app)
''' MATCH (user:User {api_key: $api_key}) RETURN user ''', { 'api_key': token }).single() db = get_db() result = db.read_transaction(get_user_by_token, token) try: g.user = result['user'] except (KeyError, TypeError): abort(401, 'invalid authorization key') return request_started.connect(set_user, app) def login_required(f): @wraps(f) def wrapped(*args, **kwargs): auth_header = request.headers.get('Authorization') if not auth_header: return {'message': 'no authorization provided'}, 401 return f(*args, **kwargs) return wrapped class GenreModel(Schema): type = 'object'
def __init__(self, name=None, config=None, *args, **kwargs): kwargs.setdefault('instance_relative_config', True) name = name or __name__ # used by make_config to determine if we try to load config from instance / # environment variable /... self._ABILIAN_INIT_TESTING_FLAG = (getattr(config, 'TESTING', False) if config else False) Flask.__init__(self, name, *args, **kwargs) del self._ABILIAN_INIT_TESTING_FLAG self._setup_script_manager() appcontext_pushed.connect(self._install_id_generator) ServiceManager.__init__(self) PluginManager.__init__(self) self.default_view = ViewRegistry() self.js_api = dict() if config: self.config.from_object(config) # at this point we have loaded all external config files: # SQLALCHEMY_DATABASE_URI is definitively fixed (it cannot be defined in # database AFAICT), and LOGGING_FILE cannot be set in DB settings. self.setup_logging() configured = bool(self.config.get('SQLALCHEMY_DATABASE_URI')) self.config['CONFIGURED'] = configured if not self.testing: self.init_sentry() if not configured: # set fixed secret_key so that any unconfigured worker will use, so that # session can be used during setup even if multiple processes are # processing requests. self.config['SECRET_KEY'] = 'abilian_setup_key' # time to load config bits from database: 'settings' # First init required stuff: db to make queries, and settings service extensions.db.init_app(self) settings_service.init_app(self) if configured: with self.app_context(): try: settings = self.services['settings'].namespace('config').as_dict() except sa.exc.DatabaseError as exc: # we may get here if DB is not initialized and "settings" table is # missing. Command "initdb" must be run to initialize db, but first we # must pass app init if not self.testing: # durint tests this message will show up on every test, since db is # always recreated logging.error(exc.message) self.db.session.rollback() else: self.config.update(settings) if not self.config.get('FAVICO_URL'): self.config['FAVICO_URL'] = self.config.get('LOGO_URL') languages = self.config.get('BABEL_ACCEPT_LANGUAGES') if languages is None: languages = abilian.i18n.VALID_LANGUAGES_CODE else: languages = tuple(lang for lang in languages if lang in abilian.i18n.VALID_LANGUAGES_CODE) self.config['BABEL_ACCEPT_LANGUAGES'] = languages self._jinja_loaders = list() self.register_jinja_loaders( jinja2.PackageLoader('abilian.web', 'templates')) js_filters = (('closure_js',) if self.config.get('PRODUCTION', False) else None) self._assets_bundles = { 'css': {'options': dict(filters=('less', 'cssmin'), output='style-%(version)s.min.css',)}, 'js-top': {'options': dict(output='top-%(version)s.min.js', filters=js_filters,)}, 'js': {'options': dict(output='app-%(version)s.min.js', filters=js_filters)}, } # bundles for JS translations for lang in languages: code = 'js-i18n-' + lang filename = 'lang-' + lang + '-%(version)s.min.js' self._assets_bundles[code] = { 'options': dict(output=filename, filters=js_filters), } for http_error_code in (403, 404, 500): self.install_default_handler(http_error_code) with self.app_context(): self.init_extensions() self.register_plugins() self.add_access_controller('static', allow_access_for_roles(Anonymous), endpoint=True) # debugtoolbar: this is needed to have it when not authenticated on a # private site. We cannot do this in init_debug_toolbar, since auth # service is not yet installed self.add_access_controller('debugtoolbar', allow_access_for_roles(Anonymous),) self.add_access_controller('_debug_toolbar.static', allow_access_for_roles(Anonymous), endpoint=True) self.maybe_register_setup_wizard() self._finalize_assets_setup() # At this point all models should have been imported: time to configure # mappers. Normally Sqlalchemy does it when needed but mappers may be # configured inside sa.orm.class_mapper() which hides a misconfiguration: if # a mapper is misconfigured its exception is swallowed by # class_mapper(model) results in this laconic (and misleading) message: # "model is not mapped" sa.orm.configure_mappers() signals.components_registered.send(self) self.before_first_request(self._set_current_celery_app) self.before_first_request( lambda: signals.register_js_api.send(self) ) request_started.connect(self._setup_nav_and_breadcrumbs) # Initialize Abilian core services. # Must come after all entity classes have been declared. # Inherited from ServiceManager. Will need some configuration love later. if not self.config.get('TESTING', False): with self.app_context(): self.start_services()
def handle_exceptions(app): middleware = bugsnag.configure().internal_middleware middleware.before_notify(add_flask_request_to_notification) got_request_exception.connect(__log_exception, app) request_started.connect(__track_session, app)
db = SQLAlchemy(app) cors = CORS(app) Compress(app) #g.etag = datetime.now() def before_request(sender, **extra): '''Called on request reception to log request before cache kicks in''' if request.path[0:5] == "/api/": daily = DailyStat(request) db.session.add(daily) db.session.commit() request_started.connect(before_request, app) @app.after_request def after(response): ''' Add etag in response''' if hasattr(g, "etag") and g.etag != None: response.headers.add('etag', g.etag) return response @app.errorhandler(404) def internal_error(error): error_msg = {"msg": "Url %s does not exist" % request.url} resp = app.make_response(jsonify(error_msg)) resp.status_code = 404 return resp
return 'user created' signal_map = { 'insert': { User: user_created, }, } def signal_router(sender, changes): """ Heper method fo routing ORM signals based on it's kind and sender model """ for change in changes: obj, kind = change try: signal_map[kind][obj.__class__](obj) except KeyError as e: pass models_committed.connect(signal_router) def propagate_user(app): if 'uid' in session: g.user = User.get(session['uid']) else: g.user = User(email='*****@*****.**') request_started.connect(propagate_user)
def __init__(self, name=None, config=None, *args, **kwargs): kwargs.setdefault('instance_relative_config', True) name = name or __name__ # used by make_config to determine if we try to load config from instance / # environment variable /... self._ABILIAN_INIT_TESTING_FLAG = (getattr(config, 'TESTING', False) if config else False) Flask.__init__(self, name, *args, **kwargs) del self._ABILIAN_INIT_TESTING_FLAG self._setup_script_manager() appcontext_pushed.connect(self._install_id_generator) ServiceManager.__init__(self) PluginManager.__init__(self) self.default_view = ViewRegistry() self.js_api = dict() if config: self.config.from_object(config) # at this point we have loaded all external config files: # SQLALCHEMY_DATABASE_URI is definitively fixed (it cannot be defined in # database AFAICT), and LOGGING_FILE cannot be set in DB settings. self.setup_logging() configured = bool(self.config.get('SQLALCHEMY_DATABASE_URI')) self.config['CONFIGURED'] = configured if not self.testing: self.init_sentry() if not configured: # set fixed secret_key so that any unconfigured worker will use, so that # session can be used during setup even if multiple processes are # processing requests. self.config['SECRET_KEY'] = 'abilian_setup_key' # time to load config bits from database: 'settings' # First init required stuff: db to make queries, and settings service extensions.db.init_app(self) settings_service.init_app(self) if configured: with self.app_context(): try: settings = self.services['settings'].namespace( 'config').as_dict() except sa.exc.DatabaseError as exc: # we may get here if DB is not initialized and "settings" table is # missing. Command "initdb" must be run to initialize db, but first we # must pass app init if not self.testing: # durint tests this message will show up on every test, since db is # always recreated logging.error(exc.message) self.db.session.rollback() else: self.config.update(settings) if not self.config.get('FAVICO_URL'): self.config['FAVICO_URL'] = self.config.get('LOGO_URL') languages = self.config.get('BABEL_ACCEPT_LANGUAGES') if languages is None: languages = abilian.i18n.VALID_LANGUAGES_CODE else: languages = tuple(lang for lang in languages if lang in abilian.i18n.VALID_LANGUAGES_CODE) self.config['BABEL_ACCEPT_LANGUAGES'] = languages self._jinja_loaders = list() self.register_jinja_loaders( jinja2.PackageLoader('abilian.web', 'templates')) js_filters = (('closure_js', ) if self.config.get('PRODUCTION', False) else None) self._assets_bundles = { 'css': { 'options': dict(filters=('less', 'cssmin'), output='style-%(version)s.min.css') }, 'js-top': { 'options': dict(output='top-%(version)s.min.js', filters=js_filters) }, 'js': { 'options': dict(output='app-%(version)s.min.js', filters=js_filters) }, } # bundles for JS translations for lang in languages: code = 'js-i18n-' + lang filename = 'lang-' + lang + '-%(version)s.min.js' self._assets_bundles[code] = { 'options': dict(output=filename, filters=js_filters), } for http_error_code in (403, 404, 500): self.install_default_handler(http_error_code) with self.app_context(): self.init_extensions() self.register_plugins() self.add_access_controller('static', allow_access_for_roles(Anonymous), endpoint=True) # debugtoolbar: this is needed to have it when not authenticated on a # private site. We cannot do this in init_debug_toolbar, since auth # service is not yet installed self.add_access_controller('debugtoolbar', allow_access_for_roles(Anonymous)) self.add_access_controller('_debug_toolbar.static', allow_access_for_roles(Anonymous), endpoint=True) self.maybe_register_setup_wizard() self._finalize_assets_setup() # At this point all models should have been imported: time to configure # mappers. Normally Sqlalchemy does it when needed but mappers may be # configured inside sa.orm.class_mapper() which hides a misconfiguration: if # a mapper is misconfigured its exception is swallowed by # class_mapper(model) results in this laconic (and misleading) message: # "model is not mapped" sa.orm.configure_mappers() signals.components_registered.send(self) self.before_first_request(self._set_current_celery_app) self.before_first_request(lambda: signals.register_js_api.send(self)) request_started.connect(self._setup_nav_and_breadcrumbs) # Initialize Abilian core services. # Must come after all entity classes have been declared. # Inherited from ServiceManager. Will need some configuration love later. if not self.config.get('TESTING', False): with self.app_context(): self.start_services()
def unique_list(seq, max=None): seen = {} result = [] for item in seq: marker = item['slug'] if marker in seen: continue seen[marker] = 1 result.append(item) if max is not None and len(result) >= max: break return result ## Signals ## from flask import request_started from style import build_css def on_request(sender, **extra): build_css() request_started.connect(on_request, app) ## Main ## if __name__ == '__main__': app.run(debug = True)
# Config if app.config['DEBUG']: app.config.from_object('web.config.DevelopmentConfig') app.logger.info("Config: Development") else: app.config.from_object('web.config.ProductionConfig') app.logger.info("Config: Production") # Source: http://www.jeffff.com/serving-media-in-the-flask-local-dev-server:w def serve_static(sender): if app.config['DEBUG']: app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {'/': os.path.join(os.path.dirname(__file__), 'static')}) request_started.connect(serve_static, app) class RegexConverter(BaseConverter): def __init__(self, url_map, *items): super(RegexConverter, self).__init__(url_map) self.regex = items[0] app.url_map.converters['regex'] = RegexConverter @app.before_request def before_request(): session["debug"] = app.debug
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 handle_exceptions(app): bugsnag.before_notify(add_flask_request_to_notification) got_request_exception.connect(__log_exception, app) request_started.connect(__track_session, app)
def init_app(app): """ A Flask extension to keep user details updates in Redis and sync it periodically to the database (User.details). """ request_started.connect(update_user_active_at, app)
def __init__(self, name=None, config=None, *args, **kwargs): name = name or __name__ instance_path = os.environ.get("FLASK_INSTANCE_PATH") if instance_path: kwargs["instance_path"] = instance_path else: kwargs.setdefault("instance_relative_config", True) # used by make_config to determine if we try to load config from # instance / environment variable /... self._ABILIAN_INIT_TESTING_FLAG = (getattr(config, "TESTING", False) if config else False) Flask.__init__(self, name, *args, **kwargs) del self._ABILIAN_INIT_TESTING_FLAG self._setup_script_manager() appcontext_pushed.connect(self._install_id_generator) ServiceManager.__init__(self) PluginManager.__init__(self) JinjaManagerMixin.__init__(self) self.default_view = ViewRegistry() self.js_api = dict() self.configure(config) # At this point we have loaded all external config files: # SQLALCHEMY_DATABASE_URI is definitively fixed (it cannot be defined in # database AFAICT), and LOGGING_FILE cannot be set in DB settings. self.setup_logging() configured = bool(self.config.get("SQLALCHEMY_DATABASE_URI")) self.config["CONFIGURED"] = configured if not self.testing: self.init_sentry() if not configured: # set fixed secret_key so that any unconfigured worker will use, # so that session can be used during setup even if # multiple processes are processing requests. self.config["SECRET_KEY"] = "abilian_setup_key" # time to load config bits from database: 'settings' # First init required stuff: db to make queries, and settings service extensions.db.init_app(self) settings_service.init_app(self) if configured: with self.app_context(): try: settings = self.services["settings"] config = settings.namespace("config").as_dict() except sa.exc.DatabaseError as exc: # We may get here if DB is not initialized and "settings" # table is missing. Command "initdb" must be run to # initialize db, but first we must pass app init. if not self.testing: # durint tests this message will show up on every test, # since db is always recreated logging.error(exc) db.session.rollback() else: self.config.update(config) if not self.config.get("FAVICO_URL"): self.config["FAVICO_URL"] = self.config.get("LOGO_URL") self.register_jinja_loaders(jinja2.PackageLoader("abilian.web")) self.init_assets() self.install_default_handlers() with self.app_context(): self.init_extensions() self.register_plugins() self.add_access_controller("static", allow_access_for_roles(Anonymous), endpoint=True) # debugtoolbar: this is needed to have it when not authenticated # on a private site. We cannot do this in init_debug_toolbar, # since auth service is not yet installed. self.add_access_controller("debugtoolbar", allow_access_for_roles(Anonymous)) self.add_access_controller( "_debug_toolbar.static", allow_access_for_roles(Anonymous), endpoint=True, ) self.maybe_register_setup_wizard() self._finalize_assets_setup() # At this point all models should have been imported: time to configure # mappers. Normally Sqlalchemy does it when needed but mappers may be # configured inside sa.orm.class_mapper() which hides a # misconfiguration: if a mapper is misconfigured its exception is # swallowed by class_mapper(model) results in this laconic # (and misleading) message: "model is not mapped" sa.orm.configure_mappers() signals.components_registered.send(self) self.before_first_request(self._set_current_celery_app) self.before_first_request(lambda: signals.register_js_api.send(self)) request_started.connect(self._setup_nav_and_breadcrumbs) # Initialize Abilian core services. # Must come after all entity classes have been declared. # Inherited from ServiceManager. Will need some configuration love # later. if not self.config.get("TESTING", False): with self.app_context(): self.start_services() if os.environ.get("FLASK_VALIDATE_HTML"): # Workaround circular import from abilian.testing.validation import validate_response self.after_request(validate_response)
abort(401, 'invalid authorization format. Follow `Token <token>`') return token = match.group(1) db = get_db() results = db.run( ''' MATCH (user:User {api_key: {api_key}}) RETURN user ''', {'api_key': token} ) try: g.user = results.single()['user'] except ResultError: abort(401, 'invalid authorization key') return request_started.connect(set_user, app) def login_required(f): @wraps(f) def wrapped(*args, **kwargs): auth_header = request.headers.get('Authorization') if not auth_header: return {'message': 'no authorization provided'}, 401 return f(*args, **kwargs) return wrapped class GenreModel(Schema): type = 'object' properties = {
# Blueprints app.register_blueprint(words, url_prefix='/words') def _set_default_language(app): if 'app_language' not in session: language = Language.query.filter(Language.code=='en').first() if language is None: session['app_language'] = 1 session['language'] = 1 else: session['app_language'] = language.id session['language'] = language.id session['languages'] = dict((l.id, l.code) for l in Language.query.all()) request_started.connect(_set_default_language, app) def language_data(language_id): return Language.query.filter(Language.id==language_id).first() def latest_words(value, count=4): words = Word.query.filter(Word.language_id==int(value))\ .order_by('word.id DESC').limit(count) return words app.jinja_env.filters['latest_words'] = latest_words app.jinja_env.filters['language_data'] = language_data @app.context_processor def _template_ctx_languages(): return dict(
if __name__ == '__main__': port = DEFAULT_PORT ssl = False opts, args = getopt.getopt(sys.argv[1:], "", ["ssl", "debug", "help", "port="]) for o, a in opts: if o == "--help": usage() sys.exit(2) elif o == "--debug": app.debug = True elif o == "--ssl": ssl = True wsgiserver.CherryPyWSGIServer.ssl_certificate = "ssl/certs/server.pem" wsgiserver.CherryPyWSGIServer.ssl_private_key = "ssl/private/server.key" wsgiserver.CherryPyWSGIServer.ssl_module = "builtin" elif o == "--port": port = int(a) d = wsgiserver.WSGIPathInfoDispatcher({'/': app}) server = wsgiserver.CherryPyWSGIServer(('0.0.0.0', port), d, timeout=200) request_started.connect(consolelog, app) print "Serving %s on port %d %s" % ("HTTP" if not ssl else "HTTPS", port, "(debug enabled)" if app.debug else "") try: server.start() except KeyboardInterrupt: server.stop()