diff options
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 23 |
1 files changed, 15 insertions, 8 deletions
@@ -2,13 +2,12 @@ import json import os -import urllib2 - import sys import traceback +import urllib2 -from flask import Flask, abort, request, send_from_directory, session, jsonify import flask_socketio +from flask import Flask, request, send_from_directory, jsonify from oauth2client import client, crypt from opendc.models.user import User @@ -19,23 +18,25 @@ if len(sys.argv) < 2: sys.exit(1) # Get keys from config file -with open(sys.argv[1]) as file: - KEYS = json.load(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['MYSQL_USER'], password=KEYS['MYSQL_PASSWORD'], - database=KEYS['MYSQL_DATABASE'], host=KEYS['MYSQL_HOST'], port=KEYS['MYSQL_PORT']) + database=KEYS['MYSQL_DATABASE'], host=KEYS['MYSQL_HOST'], port=KEYS['MYSQL_PORT']) FLASK_CORE_APP = Flask(__name__, static_url_path='', static_folder=STATIC_ROOT) FLASK_CORE_APP.config['SECREY_KEY'] = KEYS['FLASK_SECRET'] SOCKET_IO_CORE = flask_socketio.SocketIO(FLASK_CORE_APP) + @FLASK_CORE_APP.errorhandler(404) def page_not_found(e): return send_from_directory(STATIC_ROOT, 'index.html') + @FLASK_CORE_APP.route('/tokensignin', methods=['POST']) def sign_in(): """Authenticate a user with Google sign in""" @@ -73,7 +74,8 @@ def sign_in(): return jsonify(**data) -@FLASK_CORE_APP.route('/api/<string:version>/<path:endpoint_path>', methods = ['GET', 'POST', 'PUT', 'DELETE']) + +@FLASK_CORE_APP.route('/api/<string:version>/<path:endpoint_path>', methods=['GET', 'POST', 'PUT', 'DELETE']) def api_call(version, endpoint_path): """Call an API endpoint directly over HTTP""" @@ -117,12 +119,14 @@ def api_call(version, endpoint_path): flask_response.status_code = response.status['code'] return flask_response + @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') + @FLASK_CORE_APP.route('/') @FLASK_CORE_APP.route('/simulations') @FLASK_CORE_APP.route('/simulations/<path:simulation_id>') @@ -132,6 +136,7 @@ def serve_web_server_test(): def serve_index(): return send_from_directory(STATIC_ROOT, 'index.html') + @SOCKET_IO_CORE.on('request') def receive_message(message): """"Receive a SocketIO request""" @@ -148,11 +153,12 @@ def receive_message(message): flask_socketio.emit('response', response.to_JSON(), json=True) + def _process_message(message): """Process a request message and return the response.""" try: - request = rest.Request(message) + request = rest.Request(message) response = request.process() return (request, response) @@ -182,4 +188,5 @@ def _process_message(message): return (request, response) + SOCKET_IO_CORE.run(FLASK_CORE_APP, host='0.0.0.0', port=8081) |
