def create_app(): static_app = StaticApplication(STATIC_PATH) routes = [ StaticFileRoute('/', STATIC_PATH + '/index.html'), ('/', static_app), ('/home', home, render_basic), ('/login', login), ('/logout', logout), ('/complete_login', complete_login), ('/api', send_to_wd_api, render_basic), ('/meta', MetaApplication()) ] config_file_name = 'config.local.yaml' config_file_path = os.path.join(os.path.dirname(CUR_PATH), config_file_name) config = yaml.load(open(config_file_path)) cookie_secret = config['cookie_secret'] root_path = config.get('root_path', '/') scm_mw = SignedCookieMiddleware(secret_key=cookie_secret, path=root_path) scm_mw.data_expiry = NEVER consumer_token = ConsumerToken(config['oauth_consumer_token'], config['oauth_secret_token']) resources = { 'config': config, 'consumer_token': consumer_token, 'root_path': root_path } app = Application(routes, resources, middlewares=[scm_mw]) return app
def create_app(): static_app = StaticApplication(STATIC_PATH) def fe_app_route(path, ignore_trailing=True): # added to support the removal of the '#' in Angular URLs target = STATIC_PATH + '\\index.html' if ignore_trailing: path = path + '/<_ignored*>' return StaticFileRoute(path, target) routes = [ fe_app_route('/', ignore_trailing=False), # TODO: necessary? ('/', static_app), ('/home', home, render_basic), ('/login', login), ('/logout', logout), ('/complete_login', complete_login), ('/api', send_to_wiki_api, render_basic), ('/commons', send_to_commons_api, render_basic), ('/wikidata', send_to_wikidata_api, render_basic), ('/test', send_to_test_api, render_basic), ('/meta', MetaApplication()), fe_app_route('/list'), fe_app_route('/map'), fe_app_route('/object'), fe_app_route('/games'), ] config_file_name = 'config.local.yaml' config_file_path = os.path.join(CUR_PATH, config_file_name) config = yaml.load(open(config_file_path)) cookie_secret = config['cookie_secret'] root_path = config.get('root_path', '/') scm_mw = SignedCookieMiddleware(secret_key=cookie_secret, path=root_path) scm_mw.data_expiry = NEVER consumer_token = ConsumerToken(config['oauth_consumer_token'], config['oauth_secret_token']) resources = { 'config': config, 'consumer_token': consumer_token, 'root_path': root_path } app = Application(routes, resources, middlewares=[scm_mw]) return app
def create_app(env_name='prod'): # rendering is handled by MessageMiddleware ui_routes = (PUBLIC_UI_ROUTES + JUROR_UI_ROUTES + ADMIN_UI_ROUTES + META_UI_ROUTES) api_routes = (PUBLIC_API_ROUTES + JUROR_API_ROUTES + ADMIN_API_ROUTES + META_API_ROUTES) print '== creating WSGI app using env name: %s' % (env_name, ) config_file_name = 'config.%s.yaml' % env_name config_file_path = os.path.join(PROJ_PATH, config_file_name) print '== loading config file: %s' % (config_file_path, ) config = yaml.load(open(config_file_path)) logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.WARN) engine = create_engine(config.get('db_url', DEFAULT_DB_URL), pool_recycle=60) session_type = sessionmaker() session_type.configure(bind=engine) tmp_rdb_session = session_type() schema_errors = get_schema_errors(Base, tmp_rdb_session) if not schema_errors: print '++ schema validated ok' else: for err in schema_errors: print '!! ', err print '!! recreate the database and update the code, then try again' sys.exit(2) # create maintainer users if they don't exist yet musers = bootstrap_maintainers(tmp_rdb_session) if musers: print '++ created new users for maintainers: %r' % (musers, ) new_series = ensure_series(tmp_rdb_session) if new_series: print '++ created new series: %r' % new_series tmp_rdb_session.commit() engine.echo = config.get('db_echo', False) if not config.get('db_disable_ping'): event.listen(engine, 'engine_connect', ping_connection) renderer = AshesRenderFactory(TEMPLATES_PATH) cookie_secret = config['cookie_secret'] assert cookie_secret root_path = config.get('root_path', '/') scm_secure = env_name == 'prod' # https only in prod scm_mw = SignedCookieMiddleware(secret_key=cookie_secret, path=root_path, http_only=True, secure=scm_secure) if not scm_secure: scm_mw.data_expiry = NEVER def get_engine(): engine = create_engine(config.get('db_url', DEFAULT_DB_URL), pool_recycle=60) engine.echo = config.get('db_echo', False) if not config.get('db_disable_ping'): event.listen(engine, 'engine_connect', ping_connection) return engine blank_session_type = sessionmaker() middlewares = [ TimingMiddleware(), scm_mw, DBSessionMiddleware(blank_session_type, get_engine), UserMiddleware() ] api_log_path = config.get('api_log_path') if api_log_path: log_mw = LoggingMiddleware(api_log_path) middlewares.insert(0, log_mw) # hack config['api_exc_log_path'] = getattr(log_mw, 'exc_log_path', None) replay_log_path = config.get('replay_log_path') if replay_log_path: replay_log_mw = ReplayLogMiddleware(replay_log_path) middlewares.append(replay_log_mw) consumer_token = ConsumerToken(config['oauth_consumer_token'], config['oauth_secret_token']) resources = { 'config': config, 'consumer_token': consumer_token, 'root_path': root_path, 'ashes_renderer': renderer } api_app = Application(api_routes, resources, middlewares=[MessageMiddleware()] + middlewares, render_factory=render_basic) ui_app = Application(ui_routes, resources, middlewares=[MessageMiddleware(use_ashes=True)] + middlewares, render_factory=renderer) static_app = StaticApplication(STATIC_PATH) root_app = Application([ StaticFileRoute('/', STATIC_PATH + '/index.html'), ('/', static_app), ('/', ui_app), ('/v1/', api_app), ('/meta', MetaApplication()) ]) return root_app
def create_app(): static_app = StaticApplication(STATIC_PATH) def fe_app_route(path, ignore_trailing=True): # added to support the removal of the '#' in Angular URLs target = STATIC_PATH + '/index.html' if ignore_trailing: path = path + '/<_ignored*>' return StaticFileRoute(path, target) routes = [ fe_app_route('/', ignore_trailing=False), # TODO: necessary? ('/', static_app), ('/home', home, render_basic), ('/login', login), ('/logout', logout), ('/complete_login', complete_login), ('/api', send_to_wiki_api, render_basic), ('/commons', send_to_commons_api, render_basic), ('/wikidata', send_to_wikidata_api, render_basic), ('/test', send_to_test_api, render_basic), ('/meta', MetaApplication()), fe_app_route('/list'), fe_app_route('/map'), fe_app_route('/object'), fe_app_route('/games'), fe_app_route('/ireland'), fe_app_route('/poland'), fe_app_route('/germany'), fe_app_route('/uk'), fe_app_route('/sweden'), fe_app_route('/france'), fe_app_route('/us'), ] config_file_name = 'config.hatnote.yaml' config_file_path = os.path.join(PROJ_PATH, config_file_name) config = yaml.load(open(config_file_path)) cookie_secret = config['cookie_secret'] root_path = config.get('root_path', '/') # secure=True means cookie is HTTPS only # http_only=True means cookie is not accessible to javascript # TODO: make http_only contingent on whether or not we're in prod # (local doesn't do https) scm_mw = SignedCookieMiddleware(secret_key=cookie_secret, secure=True, http_only=True, path=root_path) scm_mw.data_expiry = NEVER consumer_token = ConsumerToken(config['oauth_consumer_token'], config['oauth_secret_token']) resources = { 'config': config, 'consumer_token': consumer_token, 'root_path': root_path } app = Application(routes, resources, middlewares=[scm_mw]) return app
def create_app(env_name='prod', config=None): # rendering is handled by MessageMiddleware ui_routes = (PUBLIC_UI_ROUTES + JUROR_UI_ROUTES + ADMIN_UI_ROUTES + META_UI_ROUTES) api_routes = (PUBLIC_API_ROUTES + JUROR_API_ROUTES + ADMIN_API_ROUTES + META_API_ROUTES) print '== creating WSGI app using env name: %s' % (env_name, ) logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.WARN) if config is None: config = load_env_config(env_name=env_name) print '== loaded config file: %s' % (config['__file__'], ) engine = create_engine(config.get('db_url', DEFAULT_DB_URL), pool_recycle=60) session_type = sessionmaker() session_type.configure(bind=engine) tmp_rdb_session = session_type() schema_errors = get_schema_errors(Base, tmp_rdb_session) if not schema_errors: print '++ schema validated ok' else: for err in schema_errors: print '!! ', err print '!! recreate the database and update the code, then try again' sys.exit(2) # create maintainer users if they don't exist yet musers = bootstrap_maintainers(tmp_rdb_session) if musers: print '++ created new users for maintainers: %r' % (musers, ) new_series = ensure_series(tmp_rdb_session) if new_series: print '++ created new series: %r' % new_series tmp_rdb_session.commit() engine.echo = config.get('db_echo', False) if not config.get('db_disable_ping'): event.listen(engine, 'engine_connect', ping_connection) renderer = AshesRenderFactory(TEMPLATES_PATH) cookie_secret = config['cookie_secret'] assert cookie_secret root_path = config.get('root_path', '/') scm_secure = env_name == 'prod' # https only in prod scm_mw = SignedCookieMiddleware(secret_key=cookie_secret, path=root_path, http_only=True, secure=scm_secure) if not scm_secure: scm_mw.data_expiry = NEVER def get_engine(): engine = create_engine(config.get('db_url', DEFAULT_DB_URL), pool_recycle=60) engine.echo = config.get('db_echo', False) if not config.get('db_disable_ping'): event.listen(engine, 'engine_connect', ping_connection) return engine blank_session_type = sessionmaker() middlewares = [ TimingMiddleware(), UserIPMiddleware(), scm_mw, DBSessionMiddleware(blank_session_type, get_engine), UserMiddleware() ] api_log_path = config.get('api_log_path', 'montage_api.log') if api_log_path: log_mw = LoggingMiddleware(api_log_path) middlewares.insert(0, log_mw) # hack config['api_exc_log_path'] = getattr(log_mw, 'exc_log_path', None) replay_log_path = config.get('replay_log_path') if replay_log_path: replay_log_mw = ReplayLogMiddleware(replay_log_path) middlewares.append(replay_log_mw) consumer_token = ConsumerToken(config['oauth_consumer_token'], config['oauth_secret_token']) resources = { 'config': config, 'consumer_token': consumer_token, 'root_path': root_path, 'ashes_renderer': renderer } debug_errors = bool(os.getenv('MONTAGE_PDB', False)) or config['__env__'] == 'devtest' api_app = Application( api_routes, resources, middlewares=[MessageMiddleware(debug_errors=debug_errors)] + middlewares, render_factory=render_basic) ui_app = Application( ui_routes, resources, middlewares=[ MessageMiddleware(debug_errors=debug_errors, use_ashes=True) ] + middlewares, render_factory=renderer) static_app = StaticApplication(STATIC_PATH) root_mws = [HTTPCacheMiddleware(use_etags=True)] if not debug_errors: # don't need sentry if you've got pdb, etc. sentry_sdk.init( environment=config['__env__'], request_bodies='medium', dsn="https://[email protected]/3532775") root_mws.append(SentryMiddleware()) root_app = Application([ StaticFileRoute('/', STATIC_PATH + '/index.html'), StaticFileRoute('/a/', STATIC_PATH + '/a/index.html'), ('/', static_app), ('/', ui_app), ('/v1/', api_app), ('/meta', MetaApplication()) ], resources={'config': config}, middlewares=root_mws) return root_app