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 test_meta_basic(): app = Application([('/meta', MetaApplication()), ('/<name?>', cookie_hello_world, render_basic)], middlewares=[SignedCookieMiddleware()]) cl = app.get_local_client() assert cl.get('/').status_code == 200 assert cl.get('/meta/').status_code == 200
def create_decked_out_app(): resources = {'start_time': time.time(), 'module_list': sys.modules.keys()} middlewares = [GetParamMiddleware(['name', 'date', 'session_id']), SignedCookieMiddleware(), SimpleContextProcessor('name')] routes = [('/', cookie_hello_world, render_basic), ('/debug', debug, render_basic), ('/modules/', see_modules, render_basic)] return Application(routes, resources, middlewares=middlewares)
def test_serve(): cookie_mw = SignedCookieMiddleware() app = Application([('/', cookie_hello_world, render_basic), ('/<name>/', cookie_hello_world, render_basic)], middlewares=[cookie_mw]) assert app.serve(_jk_just_testing=True, static_path=_CUR_DIR) cl = app.get_local_client() assert cl.get('/').status_code == 200 assert cl.get('/static/test_serve.py').status_code == 200
def test_serve(): cookie_mw = SignedCookieMiddleware() app = Application([('/', cookie_hello_world, render_basic), ('/<name>/', cookie_hello_world, render_basic)], middlewares=[cookie_mw]) yield ok_, app.serve(_jk_just_testing=True, static_path=_CUR_DIR) cl = Client(app, Response) yield eq_, cl.get('/').status_code, 200 yield eq_, cl.get('/_meta/').status_code, 200 yield eq_, cl.get('/static/test_serve.py').status_code, 200
def create_app(env_name='prod'): routes = [ ('/', home, render_basic), ('/admin', admin_landing, render_json), ('/admin/campaign', admin_landing, render_json), ('/admin/campaign/<campaign_id>', admin_camp_redirect, render_json), ('/admin/campaign/<campaign_id>/<camp_name>', admin_camp_dashboard, render_json), ('/admin/round', admin_landing, render_json), ('/admin/round/<round_id>', admin_round_redirect, render_json), ('/admin/round/<round_id>/<round_name>', admin_round_dashboard, render_json), ('/campaign', juror_landing, render_basic), ('/campaign/<campaign_id>', juror_camp_redirect, render_basic), ('/campaign/<campaign_id>/<camp_name>', juror_camp_dashboard, render_basic), ('/round', juror_landing, render_basic), ('/round/<round_id>', juror_round_redirect, render_basic), ('/round/<round_id>/<round_name>', juror_round_dashboard, render_basic), ('/login', login, render_basic), ('/logout', logout, render_basic), ('/complete_login', complete_login, render_basic) ] config_file_name = 'config.%s.yaml' % env_name config = yaml.load(open(config_file_name)) engine = create_engine(config.get('db_url', DEFAULT_DB_URL), echo=config.get('db_echo', False)) session_type = sessionmaker() session_type.configure(bind=engine) 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) middlewares = [scm_mw, DBSessionMiddleware(session_type), UserMiddleware()] 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=middlewares) return app
def test_cookie_expire(): cookie_mw = SignedCookieMiddleware(data_expiry=0) app = Application([('/', cookie_hello_world, render_basic), ('/<name>/', cookie_hello_world, render_basic)], middlewares=[cookie_mw]) ic = Client(app, BaseResponse) resp = ic.get('/') yield eq_, resp.status_code, 200 yield eq_, resp.data, 'Hello, world!' resp = ic.get('/Kurt/') yield eq_, resp.data, 'Hello, Kurt!' resp = ic.get('/') yield eq_, resp.data, 'Hello, world!' ic2 = Client(app, BaseResponse) resp = ic2.get('/') yield eq_, resp.data, 'Hello, world!'
def test_cookie_expire(): cookie_mw = SignedCookieMiddleware(expiry=0.1) app = Application([('/', cookie_hello_world, render_basic), ('/<name>/', cookie_hello_world, render_basic)], middlewares=[cookie_mw]) ic = app.get_local_client() resp = ic.get('/') assert resp.status_code == 200 assert resp.data == b'Hello, world!' resp = ic.get('/Kurt/') assert resp.data == b'Hello, Kurt!' time.sleep(0.11) resp = ic.get('/') assert resp.data == b'Hello, world!' ic2 = app.get_local_client() resp = ic2.get('/') assert resp.data == b'Hello, world!'
def test_cookie_mw(): cookie_mw = SignedCookieMiddleware(expiry=NEVER) _ = repr(cookie_mw) # coverage, lol app = Application([('/', cookie_hello_world, render_basic), ('/<name>/', cookie_hello_world, render_basic)], middlewares=[cookie_mw]) ic = app.get_local_client() resp = ic.get('/') assert resp.status_code == 200 assert resp.data == b'Hello, world!' resp = ic.get('/Kurt/') assert resp.data == b'Hello, Kurt!' resp = ic.get('/') assert resp.data == b'Hello, Kurt!' ic2 = app.get_local_client() resp = ic2.get('/') assert resp.data == b'Hello, world!'
def create_decked_out_app(): resources = {'start_time': time.time(), 'module_list': sys.modules.keys()} get_param_names = ['name', 'date', 'session_id', 'limit', 'expire_cookie'] middlewares = [ GetParamMiddleware(get_param_names), SignedCookieMiddleware(), SimpleContextProcessor('name') ] routes = [('/', cookie_hello_world, render_basic), ('/debug', debug, render_basic), ('/fizzbuzz', FizzBuzzer().fizzbuzz, render_basic), ('/modules', see_modules, render_basic), ('/fraiser', fraiser, render_basic), ('/obj/', create_obj_browser_app()), ('/webtop/', create_webtop_app())] return Application(routes, resources, middlewares=middlewares, error_handler=REPLErrorHandler())
def create_app(link_list_path=None, local_root=None, host_url=None, secret_key=None): link_list_path = link_list_path or _DEFAULT_LINKS_FILE_PATH link_map = LinkMap(link_list_path) local_static_app = None if local_root: local_static_app = StaticApplication(local_root) host_url = (host_url or 'localhost:5000').rstrip('/') + '/' full_host_url = 'http://' + host_url resources = { 'link_map': link_map, 'local_root': local_root, 'local_static_app': local_static_app, 'host_url': host_url, 'full_host_url': full_host_url } pdm = PostDataMiddleware({ 'target_url': unicode, 'target_file': unicode, 'alias': unicode, 'max_count': int, 'expiry_time': unicode }) submit_route = POST('/submit', add_entry, add_entry_render, middlewares=[pdm]) routes = [('/', home, 'home.html'), submit_route, ('/<alias>', use_entry)] scm = SignedCookieMiddleware(secret_key=secret_key) scp = SimpleContextProcessor('local_root', 'full_host_url') middlewares = [scm, scp] arf = AshesRenderFactory(_CUR_PATH, keep_whitespace=False) app = Application(routes, resources, middlewares, arf) return app
def create_app(): new_link_mw = PostDataMiddleware({ "target_url": str, "new_alias": str, "expiry_time": int, "max_count": int }) static_app = StaticApplication(STATIC_PATH) routes = [ ("/", home, "home.html"), POST("/submit", add_entry, middlewares=[new_link_mw]), ("/static", static_app), GET("/<alias>", use_entry), ] config_path = os.path.join(CUR_PATH, "erosion.ini") config = ConfigParser() config.read(config_path) host_url = config["erosion"]["host_url"].rstrip("/") + "/" db_path = config["erosion"]["db_path"] if not os.path.isabs(db_path): db_path = os.path.join(os.path.dirname(config_path), db_path) resources = {"host_url": host_url, "db": LinkDB(db_path)} cookie_secret = config["erosion"]["cookie_secret"] cookie_mw = SignedCookieMiddleware(secret_key=cookie_secret) render_factory = AshesRenderFactory(CUR_PATH) return Application( routes, resources=resources, middlewares=[cookie_mw], render_factory=render_factory, )
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