diff options
| author | Georgios Andreadis <info@gandreadis.com> | 2020-06-24 13:26:56 +0200 |
|---|---|---|
| committer | Georgios Andreadis <info@gandreadis.com> | 2020-06-24 13:26:56 +0200 |
| commit | db979d36d0b9693cc81ffa0bdd29364c6218fc95 (patch) | |
| tree | 269c21d599ecd190a87e51775ef533b0ea847b56 /main.py | |
| parent | c7f773b027019086153f0260b507c8fa173ee5e8 (diff) | |
Add working test setup
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 83 |
1 files changed, 38 insertions, 45 deletions
@@ -10,33 +10,29 @@ from oauth2client import client, crypt from flask_cors import CORS from opendc.models.user import User -from opendc.util import exceptions, rest, path_parser, database +from opendc.util import rest, path_parser, database +from opendc.util.exceptions import AuthorizationTokenError, RequestInitializationError -if len(sys.argv) < 2: - print("config file path not given as argument") - sys.exit(1) +TEST_MODE = "OPENDC_FLASK_TESTING" in os.environ -# Get keys from config file -with open(sys.argv[1]) as f: - KEYS = json.load(f) - -STATIC_ROOT = os.path.join(KEYS['ROOT_DIR'], 'opendc-frontend', 'build') - -database.init_connection_pool(user=KEYS['OPENDC_DB_USERNAME'], - password=KEYS['OPENDC_DB_PASSWORD'], - database=KEYS['OPENDC_DB'], - host='localhost', - port=27017) +if TEST_MODE: + STATIC_ROOT = os.curdir +else: + database.initialize_database(user=os.environ['OPENDC_DB_USERNAME'], + password=os.environ['OPENDC_DB_PASSWORD'], + database=os.environ['OPENDC_DB'], + host='localhost') + STATIC_ROOT = os.path.join(os.environ['OPENDC_ROOT_DIR'], 'opendc-frontend', 'build') FLASK_CORE_APP = Flask(__name__, static_url_path='', static_folder=STATIC_ROOT) -FLASK_CORE_APP.config['SECRET_KEY'] = KEYS['FLASK_SECRET'] -if 'localhost' in KEYS['SERVER_BASE_URL']: +FLASK_CORE_APP.config['SECRET_KEY'] = os.environ['OPENDC_FLASK_SECRET'] +if 'localhost' in os.environ['OPENDC_SERVER_BASE_URL']: CORS(FLASK_CORE_APP) compress = Compress() compress.init_app(FLASK_CORE_APP) -if 'localhost' in KEYS['SERVER_BASE_URL']: +if 'OPENDC_SERVER_BASE_URL' in os.environ or 'localhost' in os.environ['OPENDC_SERVER_BASE_URL']: SOCKET_IO_CORE = flask_socketio.SocketIO(FLASK_CORE_APP, cors_allowed_origins="*") else: SOCKET_IO_CORE = flask_socketio.SocketIO(FLASK_CORE_APP) @@ -57,9 +53,9 @@ def sign_in(): return 'No idtoken provided', 401 try: - idinfo = client.verify_id_token(token, KEYS['OAUTH_CLIENT_ID']) + idinfo = client.verify_id_token(token, os.environ['OPENDC_OAUTH_CLIENT_ID']) - if idinfo['aud'] != KEYS['OAUTH_CLIENT_ID']: + if idinfo['aud'] != os.environ['OPENDC_OAUTH_CLIENT_ID']: raise crypt.AppIdentityError('Unrecognized client.') if idinfo['iss'] not in ['accounts.google.com', 'https://accounts.google.com']: @@ -127,8 +123,7 @@ def api_call(version, endpoint_path): @FLASK_CORE_APP.route('/my-auth-token') def serve_web_server_test(): """Serve the web server test.""" - - return send_from_directory(os.path.join(KEYS['ROOT_DIR'], 'opendc-web-server', 'static'), 'index.html') + return send_from_directory(STATIC_ROOT, 'index.html') @FLASK_CORE_APP.route('/') @@ -144,50 +139,48 @@ def serve_index(simulation_id=None, experiment_id=None): @SOCKET_IO_CORE.on('request') def receive_message(message): """"Receive a SocketIO request""" + (req, res) = _process_message(message) - (request, response) = _process_message(message) - - print( - f'Socket:\t{request.method} to `/{request.path}` resulted in {response.status["code"]}: {response.status["description"]}' - ) + print(f'Socket:\t{req.method} to `/{req.path}` resulted in {res.status["code"]}: {res.status["description"]}') sys.stdout.flush() - flask_socketio.emit('response', response.to_JSON(), json=True) + flask_socketio.emit('res', res.to_JSON(), json=True) def _process_message(message): """Process a request message and return the response.""" try: - request = rest.Request(message) - response = request.process() + req = rest.Request(message) + res = req.process() - return (request, response) + return req, res - except exceptions.AuthorizationTokenError as e: - response = rest.Response(401, 'Authorization error') - response.id = message['id'] + except AuthorizationTokenError: + res = rest.Response(401, 'Authorization error') + res.id = message['id'] - except exceptions.RequestInitializationError as e: - response = rest.Response(400, str(e)) - response.id = message['id'] + except RequestInitializationError as e: + res = rest.Response(400, str(e)) + res.id = message['id'] if not 'method' in message: message['method'] = 'UNSPECIFIED' if not 'path' in message: message['path'] = 'UNSPECIFIED' - except Exception as e: - response = rest.Response(500, 'Internal server error') + except Exception: + res = rest.Response(500, 'Internal server error') if 'id' in message: - response.id = message['id'] + res.id = message['id'] traceback.print_exc() - request = rest.Request() - request.method = message['method'] - request.path = message['path'] + req = rest.Request() + req.method = message['method'] + req.path = message['path'] - return (request, response) + return req, res -SOCKET_IO_CORE.run(FLASK_CORE_APP, host='0.0.0.0', port=8081) +if __name__ == '__main__': + SOCKET_IO_CORE.run(FLASK_CORE_APP, host='0.0.0.0', port=8081) |
