diff --git a/utils.py b/utils.py index 4db00f0..246f877 100644 --- a/utils.py +++ b/utils.py @@ -3,6 +3,7 @@ import os import time import enum +import falcon import requests from EDMCLogging import get_main_logger @@ -140,50 +141,14 @@ def _get_bearer() -> str: return bearer -html_table_generator = """ -// Thanks to https://stackoverflow.com/a/21065846 -var _table_ = document.createElement('table'), - _tr_ = document.createElement('tr'), - _th_ = document.createElement('th'), - _td_ = document.createElement('td'); +def build_index_page(app: falcon.App, index_path: str) -> None: + with open(index_path, 'w', encoding='utf-8') as index_file: # kinda documentation on main page + from falcon import inspect + app_info = inspect.inspect_app(app) + app_info_str = app_info.__str__().replace('\n', '
') -// Builds the HTML Table out of myList json data from Ivy restful service. -function buildHtmlTable(arr) { - var table = _table_.cloneNode(false), - columns = addAllColumnHeaders(arr, table); - for (var i = 0, maxi = arr.length; i < maxi; ++i) { - var tr = _tr_.cloneNode(false); - for (var j = 0, maxj = columns.length; j < maxj; ++j) { - var td = _td_.cloneNode(false); - cellValue = arr[i][columns[j]]; - td.appendChild(document.createTextNode(arr[i][columns[j]] || '')); - tr.appendChild(td); - } - table.appendChild(tr); - } - return table; -} + index_file.write(index_template.format(body=app_info_str)) -// Adds a header row to the table and returns the set of columns. -// Need to do union of keys from all records as some records may not contain -// all records -function addAllColumnHeaders(arr, table) { - var columnSet = [], - tr = _tr_.cloneNode(false); - for (var i = 0, l = arr.length; i < l; i++) { - for (var key in arr[i]) { - if (arr[i].hasOwnProperty(key) && columnSet.indexOf(key) === -1) { - columnSet.push(key); - var th = _th_.cloneNode(false); - th.appendChild(document.createTextNode(key)); - tr.appendChild(th); - } - } - } - table.appendChild(tr); - return columnSet; -} -""" activity_table_html_template = """ @@ -220,21 +185,13 @@ activity_table_html_template = """ """ -activity_table_html_styles = """ -table { - margin-bottom: 20px; - border: 1px solid #dddddd; - border-collapse: collapse; -} -table th { - font-weight: bold; - padding: 5px; - background: #efefef; - border: 1px solid #dddddd; -} -table td { - border: 1px solid #dddddd; - padding: 5px; - text-align:center; -} -""" +index_template = """ + + + + + + +

{body}

+ +"""[1:] diff --git a/web.py b/web.py index 708342b..db46ab1 100644 --- a/web.py +++ b/web.py @@ -1,6 +1,7 @@ import model import json import falcon +import os import utils @@ -86,14 +87,17 @@ app.add_route('/api/diff/{action_id}', ActivityDiff()) app.add_route('/leaderboard/{leaderboard}/platform/{platform}', ActivityHtml()) app.add_route('/diff/{action_id}', ActivityDiffHtml()) -app.add_route('/{var}', MainPage()) - app.add_route('/api/cache/{action}', Cache()) application = app # for uwsgi +index_file = os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'), 'index.html') + +utils.build_index_page(app, index_file) + if __name__ == '__main__': import waitress - import os - app.add_static_route('/js', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'js')) + + app.add_static_route('/js', os.path.join(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static'), 'js')) + app.add_static_route('/', os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static')) waitress.serve(app, host='127.0.0.1', port=9485)