summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorgios Andreadis <g.andreadis@student.tudelft.nl>2017-09-23 23:36:12 +0200
committerGeorgios Andreadis <g.andreadis@student.tudelft.nl>2017-09-23 23:36:12 +0200
commitafeae8c5cd413dfda8836e1fec6f3ed1b00ce6aa (patch)
tree87d8056dfdb8cd00d9557dd0d0b45b460bc3e185
parent3d086d18a2639a76c4739b6ca7ca06416356c706 (diff)
Serve new frontend and use MariaDB
-rw-r--r--main.py81
-rw-r--r--opendc/util/database.py2
2 files changed, 33 insertions, 50 deletions
diff --git a/main.py b/main.py
index 307b5356..7321f1e0 100644
--- a/main.py
+++ b/main.py
@@ -2,6 +2,8 @@
import json
import os
+import urllib2
+
import sys
import traceback
@@ -22,61 +24,22 @@ with open(sys.argv[1]) as file:
STATIC_ROOT = os.path.join(KEYS['ROOT_DIR'], 'opendc-frontend', 'build')
-database.init_connection_pool(user=KEYS['MYSQL_USER'], password=KEYS['MYSQL_PASSWORD'], \
+database.init_connection_pool(user=KEYS['MYSQL_USER'], password=KEYS['MYSQL_PASSWORD'],
database=KEYS['MYSQL_DATABASE'], host=KEYS['MYSQL_HOST'], port=KEYS['MYSQL_PORT'])
-FLASK_CORE_APP = Flask(__name__, static_url_path='')
+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, '404.html')
-
-@FLASK_CORE_APP.route('/')
-def serve_splash():
- """Serve the splash page on /"""
-
return send_from_directory(STATIC_ROOT, 'index.html')
-@FLASK_CORE_APP.route('/app')
-def serve_app():
- """Serve the app on /app."""
-
- return send_from_directory(STATIC_ROOT, 'app.html')
-
-@FLASK_CORE_APP.route('/profile')
-def serve_profile():
- """Serve profile page."""
-
- return send_from_directory(STATIC_ROOT, 'profile.html')
-
-@FLASK_CORE_APP.route('/projects')
-def serve_projects():
- """Serve the projects page."""
-
- return send_from_directory(STATIC_ROOT, 'projects.html')
-
-@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('/<path:folder>/<path:filepath>')
-def serve_static(folder, filepath):
- """Serve static files from the build directory"""
-
- if not folder in ['bower_components', 'img', 'scripts', 'styles']:
- abort(404)
-
- return send_from_directory(os.path.join(STATIC_ROOT, folder), filepath)
-
@FLASK_CORE_APP.route('/tokensignin', methods=['POST'])
def sign_in():
"""Authenticate a user with Google sign in"""
-
+
try:
token = request.form['idtoken']
except KeyError:
@@ -84,13 +47,18 @@ def sign_in():
try:
idinfo = client.verify_id_token(token, KEYS['OAUTH_CLIENT_ID'])
-
+
if idinfo['aud'] != KEYS['OAUTH_CLIENT_ID']:
raise crypt.AppIdentityError('Unrecognized client.')
-
+
if idinfo['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
raise crypt.AppIdentityError('Wrong issuer.')
-
+ except ValueError:
+ url = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={}".format(token)
+ req = urllib2.Request(url)
+ response = urllib2.urlopen(url=req, timeout=30)
+ res = response.read()
+ idinfo = json.loads(res)
except crypt.AppIdentityError, e:
return 'Did not successfully authenticate'
@@ -123,7 +91,7 @@ def api_call(version, endpoint_path):
body_parameters = json.loads(request.get_data())
except:
body_parameters = {}
-
+
# Create and call request
(req, response) = _process_message({
'id': 0,
@@ -149,10 +117,25 @@ 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>')
+@FLASK_CORE_APP.route('/simulations/<path:simulation_id>/experiments')
+@FLASK_CORE_APP.route('/simulations/<path:simulation_id>/experiments/<path:experiment_id>')
+@FLASK_CORE_APP.route('/profile')
+def serve_index():
+ return send_from_directory(STATIC_ROOT, 'index.html')
+
@SOCKET_IO_CORE.on('request')
def receive_message(message):
""""Receive a SocketIO request"""
-
+
(request, response) = _process_message(message)
print 'Socket:\t{} to `/{}` resulted in {}: {}'.format(
@@ -186,13 +169,13 @@ def _process_message(message):
message['method'] = 'UNSPECIFIED'
if not 'path' in message:
message['path'] = 'UNSPECIFIED'
-
+
except Exception as e:
response = rest.Response(500, 'Internal server error')
if 'id' in message:
response.id = message['id']
traceback.print_exc()
-
+
request = rest.Request()
request.method = message['method']
request.path = message['path']
diff --git a/opendc/util/database.py b/opendc/util/database.py
index 337f8fc7..32aa947c 100644
--- a/opendc/util/database.py
+++ b/opendc/util/database.py
@@ -14,7 +14,7 @@ CONNECTION_POOL = None
def init_connection_pool(user, password, database, host, port):
global CONNECTION_POOL
- CONNECTION_POOL = MySQLConnectionPool(pool_name = "opendcpool", pool_size = 5, \
+ CONNECTION_POOL = MySQLConnectionPool(pool_name = "opendcpool", pool_size = 5,
user=user, password=password, database=database, host=host, port=port)
def execute(statement, t):