Esempio n. 1
0
async def display_welcome():
    global WELCOME_MESSAGE
    api_version = '0.1'
    # http://b8f7208d.ngrok.io/?configuration={%22api%22:{%22backendURL%22:%22https://30a33ee8.ngrok.io/api/0.1%22,%22timeout%22:%2215000%22}}#/

    format_url = lambda base, backend, version: f'{base}/?configuration={{"api":{{"backendURL":"{backend}/{version}","timeout":15000}}}}'
    WELCOME_MESSAGE += f"""
    Frontend locally available at:
        http://localhost:{cape_frontend_settings.CONFIG_SERVER['port']}"""
    if cape_frontend_settings.ACTIVATE_NGROK_LINUX:
        public_url_frontend = NgrokActivator.activate_ngrok_linux(
            cape_frontend_settings.CONFIG_SERVER['port'])
        backend_urls = [
            NgrokActivator.activate_ngrok_linux(urlparse(backend_url).port) +
            '/api' for idx, backend_url in enumerate(
                cape_frontend_settings.BACKENDS_API_URL)
        ]
        if public_url_frontend:
            WELCOME_MESSAGE += f"""
        Frontend publicly available at (powered by ngrok):
            {format_url(public_url_frontend,backend_urls[0] if backend_urls else cape_frontend_settings.BACKENDS_API_URL[0],api_version)}"""
        if backend_urls:
            WELCOME_MESSAGE += f"""
            Using publicly available backends at (powered by ngrok): {' '.join(backend_urls)}"""
    if cape_frontend_settings.WAIT_FOR_BACKENDS:
        WELCOME_MESSAGE += f"""
            Local backends at: {' '.join(cape_frontend_settings.BACKENDS_API_URL)}"""
    log(WELCOME_MESSAGE)
Esempio n. 2
0
def run(port: Union[None, int] = None):
    if port is not None:
        cape_frontend_settings.CONFIG_SERVER['port'] = int(port)
    log("Using port", cape_frontend_settings.CONFIG_SERVER['port'])
    wait_for_backend()
    app.config.LOGO = None
    app.add_task(display_welcome)
    app.run(**cape_frontend_settings.CONFIG_SERVER)
Esempio n. 3
0
async def _save_session(request, response):
    if _is_sanic_static(response):
        return
    if 'session' in request:
        log('setting cookie')
        response.cookies[_SESSION_COOKIE_NAME]: str = request['session']
        response.cookies[_SESSION_COOKIE_NAME][
            'expires'] = _SESSION_COOKIE_EXPIRES
        response.cookies[_SESSION_COOKIE_NAME]['httponly'] = True
    elif 'session' not in request and _SESSION_COOKIE_NAME in request.cookies:
        log('deleting cookie')
        response.cookies[_SESSION_COOKIE_NAME] = ''
        response.cookies[_SESSION_COOKIE_NAME]['expires'] = 0
        response.cookies[_SESSION_COOKIE_NAME]['max-age'] = 0
Esempio n. 4
0
def _500(request, exception):
    log('500', request=request, exc_info=True)
    if exception.__class__ is UserException:
        log("User exception: %s" % exception.message,
            request=request,
            exc_info=True)
        message = exception.message
        return jsonify({
            'success': False,
            'result': {
                'message': message
            }
        },
                       status=500,
                       headers=generate_cors_headers(request))
    return redirect('/500.html')
Esempio n. 5
0
def create_demo_user(api_url):
    log('Creating demo user')
    client = CapeClient(api_url)
    new_user_parameters = {
        'userId': cape_frontend_settings.DEMO_USER_LOGIN,
        'password': cape_frontend_settings.DEMO_USER_PASSWORD,
        'token': cape_frontend_settings.DEMO_USER_TOKEN,
        'superAdminToken': cape_frontend_settings.BACKEND_SUPER_ADMIN_TOKEN,
    }
    url = 'user/create-user?'
    for k, v in new_user_parameters.items():
        url += "%s=%s&" % (k, v)
    response = client._raw_api_call(url)
    assert response.status_code == 200, "Could not create demo user"
    client.login(cape_frontend_settings.DEMO_USER_LOGIN,
                 cape_frontend_settings.DEMO_USER_PASSWORD)
    log(f'Demo user created with token {cape_frontend_settings.DEMO_USER_TOKEN}'
        )
Esempio n. 6
0
 def _install_ngrok():
     if NgrokActivator._installed:
         return
     log(f"Installing ngrok...")
     subprocess.check_call(
         [
             'wget', '-O', '/tmp/ngrok.zip',
             'https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip'
         ],
         stdout=open('/tmp/logfile.log', 'a'),
         stderr=open('/tmp/logfile.log', 'a'),
     )
     subprocess.check_call(
         ['unzip', '-d', '/tmp', '/tmp/ngrok.zip'],
         stdout=open('/tmp/logfile.log', 'a'),
         stderr=open('/tmp/logfile.log', 'a'),
     )
     NgrokActivator._installed = True
     log("Done")
Esempio n. 7
0
def wait_for_backend():
    if not cape_frontend_settings.WAIT_FOR_BACKENDS:
        return
    demo_user_created = not cape_frontend_settings.CREATE_DEMO_ACCOUNT_ON_INIT
    num_backends = len(cape_frontend_settings.BACKENDS_API_URL)
    backends_left = set(cape_frontend_settings.BACKENDS_API_URL)
    while backends_left:
        log(f"Frontend waiting for backends to initialize {len(backends_left)}/{num_backends}"
            )
        current_url = backends_left.pop()
        try:
            reachable = requests.get(current_url[:-4],
                                     timeout=1).status_code == 200
        except Exception:
            reachable = False
        if not reachable:
            log(f'Could not reach backend {current_url}, retrying ...')
            backends_left.add(current_url)
            time.sleep(1)
            continue
        if not demo_user_created:
            create_demo_user(current_url)
        client = CapeClient(current_url)
        client.login(cape_frontend_settings.DEMO_USER_LOGIN,
                     cape_frontend_settings.DEMO_USER_PASSWORD)
        client.answer(
            question="How many people were present ?",
            text=
            "Yesterday at the demonstration, 500 people assisted the event.")
    log("All backends started")
Esempio n. 8
0
 def wrapper(request, *args, **kw):
     status = 200
     try:
         result = decorated(request, *args, **kw)
     except UserException as user_exception:
         log("User exception in mock: %s" % user_exception.message,
             request=request,
             exc_info=True)
         status = 500
         result = {
             'success': False,
             'result': {
                 'message': user_exception.message
             }
         }
     except json.JSONDecodeError:
         log(ERROR_INVALID_JSON, request=request, exc_info=True)
         status = 500
         result = {
             'success': False,
             'result': {
                 'message': ERROR_INVALID_JSON
             }
         }
     except Exception:
         log("Exception in mock", request=request, exc_info=True)
         status = 500
         result = {'success': False, 'result': {'message': ERROR_TEXT}}
     if 'success' not in result:
         result = {'success': True, 'result': result}
     return jsonify(result,
                    status=status,
                    headers=generate_cors_headers(request))
Esempio n. 9
0
 def activate_ngrok_linux(port: int):
     if cape_frontend_settings.ACTIVATE_NGROK_LINUX:
         NgrokActivator._install_ngrok()
         log(f"Activating ngrok forwarding for {port}...")
         subprocess.Popen(['nohup', '/tmp/ngrok', 'http',
                           str(port)],
                          stdout=open('/tmp/logfile.log', 'a'),
                          stderr=open('/tmp/logfile.log', 'a'),
                          preexec_fn=os.setpgrp)
         log("Forwarding activated...")
         log("Waiting for ngrok to initialize...")
         time.sleep(3)
         log("Getting ngrok tunnel...")
         ngrok_local_server = f'http://127.0.0.1:{NgrokActivator.counter+4040}/api/tunnels'
         NgrokActivator.counter += 1
         return requests.get(
             ngrok_local_server).json()['tunnels'][-1]['public_url']
Esempio n. 10
0
def _404(request, exception):
    log('404', request=request, exc_info=True)
    return redirect('/404.html')