def create_app(*, settings: Settings=None): settings = settings or Settings() setup_logging() secret_key = base64.urlsafe_b64decode(settings.auth_key) app = web.Application(middlewares=( error_middleware, session_middleware(EncryptedCookieStorage(secret_key, cookie_name='mithra')), auth_middleware, )) app['settings'] = settings ctx = dict( COMMIT=os.getenv('COMMIT', '-'), RELEASE_DATE=os.getenv('RELEASE_DATE', '-'), SERVER_NAME=os.getenv('SERVER_NAME', '-'), ) index_html = (THIS_DIR / 'index.html').read_text() for key, value in ctx.items(): index_html = re.sub(r'\{\{ ?%s ?\}\}' % key, escape(value), index_html) app['index_html'] = index_html app.on_startup.append(startup) app.on_cleanup.append(cleanup) setup_routes(app) return app
def main(): asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) logging_client = setup_logging() try: locale.setlocale(locale.LC_ALL, 'en_GB.utf8') except locale.Error: pass try: settings = Settings() try: _, command, *args = sys.argv except ValueError: logger.info( 'no command provided, options are: "reset_database", "patch", "worker" or "web"' ) return 1 if command == 'reset_database': logger.info('running reset_database...') reset_database(settings) elif command == 'patch': logger.info('running patch...') live = '--live' in args if live: args.remove('--live') return run_patch(settings, live, args[0] if args else None) elif command == 'web': logger.info('running web server...') from web.main import create_app app = create_app(settings=settings) web.run_app(app, port=settings.port, shutdown_timeout=6, access_log=None, print=lambda *args: None) elif command == 'worker': logger.info('running worker...') RunWorkerProcess('shared/worker.py', 'Worker') else: logger.error(f'unknown command "{command}"') return 1 finally: loop = asyncio.get_event_loop() if logging_client and not loop.is_closed(): transport = logging_client.remote.get_transport() transport and loop.run_until_complete(transport.close())
def create_app(*, settings: Settings = None, logging_client=None): logging_client = logging_client or setup_logging() settings = settings or Settings() app = web.Application(logger=None, middlewares=(pg_middleware, user_middleware, csrf_middleware)) app.update( settings=settings, auth_fernet=fernet.Fernet(settings.auth_key), dummy_password_hash=mk_password(settings.dummy_password, settings), logging_client=logging_client, ) app.on_startup.append(startup) app.on_cleanup.append(cleanup) app.add_routes([ web.get(r'/', index, name='index'), web.get(r'/sitemap.xml', sitemap, name='sitemap'), web.post(r'/ses-webhook/', ses_webhook, name='ses-webhook'), web.get(r'/cat/{category}/', category_public, name='category'), # event admin web.get(r'/events/categories/', event_categories, name='event-categories'), *EventBread.routes(r'/events/'), web.get(r'/events/search/', event_search, name='event-search'), web.post(r'/events/{id:\d+}/set-status/', SetEventStatus.view(), name='event-set-status'), web.post(r'/events/{id:\d+}/set-image/new/', set_event_image_new, name='event-set-image-new'), web.post(r'/events/{id:\d+}/set-image/existing/', set_event_image_existing, name='event-set-image-existing'), web.post(r'/events/{id:\d+}/set-image/secondary/', set_event_secondary_image, name='event-set-image-secondary'), web.post( r'/events/{id:\d+}/remove-image/secondary/', remove_event_secondary_image, name='event-remove-image-secondary', ), web.post( r'/events/{id:\d+}/set-image/description/', set_event_description_image, name='event-set-image-description', ), web.post( r'/events/{id:\d+}/remove-image/description/', remove_event_description_image, name='event-remove-image-description', ), web.post(r'/events/{id:\d+}/clone/', EventClone.view(), name='event-clone'), web.get(r'/events/{id:\d+}/tickets/', event_tickets, name='event-tickets'), web.post(r'/events/{id:\d+}/tickets/{tid:\d+}/cancel/', CancelTickets.view(), name='event-tickets-cancel'), web.get(r'/events/{id:\d+}/tickets/export.csv', event_tickets_export, name='event-tickets-export'), web.get(r'/events/{id:\d+}/donations/export.csv', event_donations_export, name='event-donations-export'), web.get(r'/events/{id:\d+}/ticket-types/', event_ticket_types, name='event-ticket-types'), web.post(r'/events/{id:\d+}/ticket-types/update/', SetTicketTypes.view(), name='update-event-ticket-types'), web.post(r'/events/{id:\d+}/reserve/', ReserveTickets.view(), name='event-reserve-tickets'), web.post(r'/events/{id:\d+}/updates/send/', EventUpdate.view(), name='event-send-update'), web.get(r'/events/{id:\d+}/updates/list/', event_updates_sent, name='event-updates-sent'), web.post(r'/events/{id:\d+}/switch-highlight/', switch_highlight, name='event-switch-highlight'), web.post(r'/events/{id:\d+}/waiting-list/add/', waiting_list_add, name='event-waiting-list-add'), web.get( r'/events/{id:\d+}/waiting-list/remove/{user_id:\d+}/', waiting_list_remove, name='event-waiting-list-remove', ), # event public views web.post(r'/events/book-free/', BookFreeTickets.view(), name='event-book-tickets'), web.post(r'/events/cancel-reservation/', CancelReservedTickets.view(), name='event-cancel-reservation'), web.get(r'/events/{category}/{event}/', event_get, name='event-get-public'), web.get(r'/events/{category}/{event}/booking-info/', booking_info, name='event-booking-info-public'), web.get(r'/events/{category}/{event}/donating-info/', donating_info, name='event-donating-info-public'), web.get(r'/events/{category}/{event}/{sig}/', event_get, name='event-get-private'), web.get(r'/events/{category}/{event}/{sig}/booking-info/', booking_info, name='event-booking-info-private'), web.get(r'/events/{category}/{event}/{sig}/donating-info/', donating_info, name='event-donating-info-private'), # stripe views web.post(r'/stripe/webhook/', stripe_webhook, name='stripe-webhook'), web.get( r'/stripe/payment-method-details/{payment_method}/', get_payment_method_details, name='payment-method-details', ), web.post(r'/login/', login, name='login'), web.get(r'/login/captcha/', login_captcha_required, name='login-captcha-required'), web.post(r'/login/{site:(google|facebook)}/', login_with, name='login-google-facebook'), web.post(r'/auth-token/', authenticate_token, name='auth-token'), web.post(r'/reset-password/', reset_password_request, name='reset-password-request'), web.post(r'/set-password/', set_password, name='set-password'), web.post(r'/logout/', logout, name='logout'), web.post(r'/signup/guest/{site:(google|facebook|email)}/', guest_signup, name='signup-guest'), web.post(r'/signup/host/{site:(google|facebook|email)}/', host_signup, name='signup-host'), web.get(r'/unsubscribe/{id:\d+}/', unsubscribe, name='unsubscribe'), *CompanyBread.routes(r'/companies/'), web.post(r'/companies/upload/{field:(image|logo)}/', company_upload, name='company-upload'), web.post(r'/companies/footer-links/set/', company_set_footer_link, name='company-footer-links'), web.post(r'/categories/{cat_id:\d+}/add-image/', category_add_image, name='categories-add-image'), web.get(r'/categories/{cat_id:\d+}/images/', category_images, name='categories-images'), web.post(r'/categories/{cat_id:\d+}/images/set-default/', category_set_image, name='categories-set-image'), web.post(r'/categories/{cat_id:\d+}/images/delete/', category_delete_image, name='categories-delete-image'), *CategoryBread.routes(r'/categories/'), *UserBread.routes(r'/users/'), *UserSelfBread.routes(r'/account/', name='account'), web.get(r'/users/search/', user_search, name='user-search'), web.get(r'/users/{pk:\d+}/actions/', user_actions, name='user-actions'), web.get(r'/users/{pk:\d+}/tickets/', user_tickets, name='user-tickets'), web.post(r'/users/{pk:\d+}/switch-status/', switch_user_status, name='user-switch-status'), web.get( r'/export/{type:(events|categories|users|tickets|donations)}.csv', export, name='export'), web.get(r'/email-defs/', email_def_browse, name='email-defs-browse'), web.get(r'/email-defs/{trigger}/', email_def_retrieve, name='email-defs-retrieve'), web.post(r'/email-defs/{trigger}/edit/', email_def_edit, name='email-defs-edit'), web.post(r'/email-defs/{trigger}/clear/', clear_email_def, name='email-defs-clear'), # donations *DonationOptionBread.routes(r'/donation-options/', name='donation-options'), web.get(r'/categories/{cat_id:\d+}/donation-options/', donation_options, name='donation-options'), web.post(r'/donation-options/{pk:\d+}/upload-image/', donation_image_upload, name='donation-image-upload'), web.get(r'/donation-options/{pk:\d+}/donations/', opt_donations, name='donation-opt-donations'), web.post( r'/donation-options/{don_opt_id:\d+}/prepare/{event_id:\d+}/', donation_after_prepare, name='donation-after-prepare', ), web.post(r'/donation-prepare/{tt_id:\d+}/', PrepareDirectDonation.view(), name='donation-direct-prepare'), web.post(r'/donation/{action_id:\d+}/gift-aid/', DonationGiftAid.view(), name='donation-gift-aid'), ]) wrapper_app = web.Application( client_max_size=settings.max_request_size, middlewares=( session_middleware( EncryptedCookieStorage(settings.auth_key, cookie_name='nosht')), error_middleware, ), logger=None, ) wrapper_app.update( settings=settings, main_app=app, ) static_dir = settings.custom_static_dir or (Path(__file__).parent / '../../js/build').resolve() assert static_dir.exists( ), f'js static directory "{static_dir}" does not exists' logger.debug('serving static files "%s"', static_dir) wrapper_app.update( static_dir=static_dir, csp_headers=get_csp_headers(settings), ) wrapper_app.add_subapp(r'/api/', app) wrapper_app.add_routes( [web.get(r'/{path:.*}', static_handler, name='static')]) return wrapper_app
def check(): loop = asyncio.get_event_loop() exit_code = loop.run_until_complete(_check('http://127.0.0.1:8000/api/')) if exit_code: exit(exit_code) def download_from_intercom(settings, force=False): loop = asyncio.get_event_loop() loop.run_until_complete(_download_from_intercom(settings, force)) if __name__ == '__main__': asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) setup_logging() settings = Settings() try: _, command, *args = sys.argv except ValueError: print( 'no command provided, options are: "check", "reset_database", "patch", "download_from_intercom" or "web"' ) sys.exit(1) if command == 'check': print('running check...') check() elif command == 'reset_database': print('running reset_database...') reset_database(settings)
def create_app(*, settings: Settings=None, logging_client=None): logging_client = logging_client or setup_logging() settings = settings or Settings() app = web.Application(middlewares=( session_middleware(EncryptedCookieStorage(settings.auth_key, cookie_name='nosht')), pg_middleware, host_middleware, )) app.update( settings=settings, auth_fernet=fernet.Fernet(settings.auth_key), dummy_password_hash=mk_password(settings.dummy_password, settings), logging_client=logging_client, ) app.on_startup.append(startup) app.on_cleanup.append(cleanup) app.add_routes([ web.get('/', index, name='index'), web.post('/categories/{cat_id:\d+}/add-image/', category_add_image, name='categories-add-image'), web.get('/categories/{cat_id:\d+}/images/', category_images, name='categories-images'), web.post('/categories/{cat_id:\d+}/set-default/', category_default_image, name='categories-set-default'), web.post('/categories/{cat_id:\d+}/delete/', category_delete_image, name='categories-delete'), *CategoryBread.routes('/categories/'), web.get('/cat/{category}/', category_public, name='category'), web.get('/events/categories/', event_categories, name='event-categories'), *EventBread.routes('/events/'), web.post('/events/{id:\d+}/set-status/', SetEventStatus.view(), name='event-set-status'), web.get('/events/{id:\d+}/booking-info/', booking_info, name='event-booking-info'), web.get('/events/{id:\d+}/tickets/', event_tickets, name='event-tickets'), web.post('/events/{id:\d+}/reserve/', ReserveTickets.view(), name='event-reserve-tickets'), web.post('/events/buy/', BuyTickets.view(), name='event-buy-tickets'), web.post('/events/cancel-reservation/', CancelReservedTickets.view(), name='event-cancel-reservation'), web.get('/events/{category}/{event}/', event_public, name='event-get'), web.post('/login/', login, name='login'), web.post('/login/{site:(google|facebook)}/', login_with, name='login-google-facebook'), web.post('/auth-token/', authenticate_token, name='auth-token'), web.post('/set-password/', set_password, name='set-password'), web.post('/logout/', logout, name='logout'), web.post('/login/guest/{site:(google|facebook|email)}/', guest_signin, name='login-guest'), web.post('/signup/{site:(google|facebook|email)}/', host_signup, name='signup-host'), web.get('/unsubscribe/{id:\d+}/', unsubscribe, name='unsubscribe'), *UserBread.routes('/users/'), ]) wrapper_app = web.Application( client_max_size=settings.max_request_size, middlewares=(error_middleware,), ) wrapper_app.update( settings=settings, main_app=app, ) this_dir = Path(__file__).parent static_dir = (this_dir / '../../js/build').resolve() assert static_dir.exists(), f'js static directory "{static_dir}" does not exists' logger.debug('serving static files "%s"', static_dir) wrapper_app['static_dir'] = static_dir wrapper_app.add_subapp('/api/', app) wrapper_app.add_routes([ web.get('/{path:.*}', static_handler, name='static'), ]) return wrapper_app