diff --git a/client/Constants.py b/client/Constants.py new file mode 100644 index 0000000000000000000000000000000000000000..b054fb97a6c30680f71f50354c9a75f773d6380b --- /dev/null +++ b/client/Constants.py @@ -0,0 +1 @@ +APP_NAME = 'RoadmapClient' diff --git a/client/Pipfile b/client/Pipfile index 139431f458c871c49c41ed685cda56235d382a9c..a00a2eba22e894178b3b5416a95b3d605349a850 100644 --- a/client/Pipfile +++ b/client/Pipfile @@ -3,6 +3,11 @@ url = "https://pypi.python.org/simple" verify_ssl = true name = "pypi" +[[source]] +url = "https://pypi.thecodedev.de" +verify_ssl = true +name = "TheCodeLabs" + [requires] python_version = "3.7" @@ -10,3 +15,5 @@ python_version = "3.7" flask = "==1.1.2" gevent = "==1.5.0" requests = "==2.23.0" +TheCodeLabs-BaseUtils = "==1.0.0" +TheCodeLabs-FlaskUtils = "==1.0.2" diff --git a/client/Pipfile.lock b/client/Pipfile.lock index 2fc5d05c590cdf0d277158681727cb80e5e136ba..3f58739fdca7f4d3bdd2088614322cbceb0beadf 100644 --- a/client/Pipfile.lock +++ b/client/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "dbb97f2c25b5f5b072a133a61c9a6916e36d02156fb4b8e187f292adc2bc8a6d" + "sha256": "7f0ccba88af4e372830e455cfe1038d236826bbe8f73f551f1644e55f5f328d8" }, "pipfile-spec": 6, "requires": { @@ -12,6 +12,11 @@ "name": "pypi", "url": "https://pypi.python.org/simple", "verify_ssl": true + }, + { + "name": "TheCodeLabs", + "url": "https://pypi.thecodedev.de", + "verify_ssl": true } ] }, @@ -66,10 +71,10 @@ }, "click": { "hashes": [ - "sha256:8a18b4ea89d8820c5d0c7da8a64b2c324b4dabb695804dbfea19b9be9d88c0cc", - "sha256:e345d143d80bf5ee7534056164e5e112ea5e22716bbb1ce727941f4c8b471b9a" + "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a", + "sha256:dacca89f4bfadd5de3d7489b7c8a566eee0d3676333fbb50030263894c38c0dc" ], - "version": "==7.1.1" + "version": "==7.1.2" }, "flask": { "hashes": [ @@ -210,6 +215,20 @@ "index": "pypi", "version": "==2.23.0" }, + "thecodelabs-baseutils": { + "hashes": [ + "sha256:daab1cf2480b3c1352ef94f78774d4c4c8759d91a6c50b113bcb1c73081263cb" + ], + "index": "pypi", + "version": "==1.0.0" + }, + "thecodelabs-flaskutils": { + "hashes": [ + "sha256:98833b87fd1b0b42a7ad6ac9de3b79676623cc21b0fabc940863412e8ee40961" + ], + "index": "pypi", + "version": "==1.0.2" + }, "urllib3": { "hashes": [ "sha256:3018294ebefce6572a474f0604c2021e33b3fd8006ecd11d62107a5d2a963527", diff --git a/client/RoadmapClient.py b/client/RoadmapClient.py index 56b5a1bff7ef70a078a63973d75584f8e49e740b..71a3672b426d2c83a1309fcd70437707f56cbb1e 100644 --- a/client/RoadmapClient.py +++ b/client/RoadmapClient.py @@ -1,94 +1,76 @@ -import json +import logging +import os from datetime import datetime import requests -from flask import Flask, render_template, redirect, jsonify -from gevent.pywsgi import WSGIServer +from TheCodeLabs_BaseUtils import DefaultLogger +from TheCodeLabs_FlaskUtils import FlaskBaseApp +from flask import render_template, redirect +import Constants from Localization import LOCALIZATION from UrlBuilder import UrlBuilder from blueprints import Roadmaps, Authentication, Milestones, Tasks, SubTasks -with open('version.json', 'r') as f: - VERSION = json.load(f) -VERSION = VERSION['version'] +LOGGER = DefaultLogger().create_logger_if_not_exists(Constants.APP_NAME) -with open('settings.json', 'r') as f: - SETTINGS = json.load(f) -app = Flask(__name__) -app.secret_key = SETTINGS['secret'] +class RoadmapClient(FlaskBaseApp): + DEFAULT_DATE = datetime(2000, 1, 1, 0, 0, 0) -DEFAULT_DATE = datetime(2000, 1, 1, 0, 0, 0) + def __init__(self, appName: str, rootDir: str, logger: logging.Logger, settingsPath: str): + super().__init__(appName, rootDir, logger, settingsPath=settingsPath) + self._urlBuilder = UrlBuilder(self._serverSettings['apiURL']) -URL_BUILDER = UrlBuilder(SETTINGS['apiURL']) + def _register_blueprints(self, app): + app.register_blueprint(Authentication.construct_blueprint(self._urlBuilder)) + app.register_blueprint(Roadmaps.construct_blueprint(self._urlBuilder)) + app.register_blueprint(Milestones.construct_blueprint(self._urlBuilder)) + app.register_blueprint(Tasks.construct_blueprint(self._urlBuilder)) + app.register_blueprint(SubTasks.construct_blueprint(self._urlBuilder)) -app.register_blueprint(Authentication.construct_blueprint(URL_BUILDER)) -app.register_blueprint(Roadmaps.construct_blueprint(URL_BUILDER)) -app.register_blueprint(Milestones.construct_blueprint(URL_BUILDER)) -app.register_blueprint(Tasks.construct_blueprint(URL_BUILDER)) -app.register_blueprint(SubTasks.construct_blueprint(URL_BUILDER)) + @app.route('/') + def overview(): + roadmaps = requests.get(self._urlBuilder.build_url('roadmaps')).json() + return render_template('overview.html', roadmaps=roadmaps) + @app.route('/roadmap/') + def roadmap(): + return redirect('/') -@app.route('/version', methods=['GET']) -def version(): - return jsonify(VERSION) + @app.route('/roadmap/<roadmapID>') + def roadmap_by_id(roadmapID): + success, response = self.__check_roadmap(roadmapID, self._urlBuilder) + if success: + return render_template('index.html', roadmap=response, localization=LOCALIZATION) + return response -@app.route('/') -def overview(): - roadmaps = requests.get(URL_BUILDER.build_url('roadmaps')).json() - return render_template('overview.html', roadmaps=roadmaps) + @app.route('/roadmap/<roadmapID>/fragment') + def roadmap_fragment_by_id(roadmapID): + success, response = self.__check_roadmap(roadmapID, self._urlBuilder) + if success: + return render_template('roadmapFragment.html', roadmap=response, localization=LOCALIZATION) + return response -@app.route('/roadmap/') -def roadmap(): - return redirect('/') + @staticmethod + def __check_roadmap(cls, roadmapID, urlBuilder): + try: + roadmapID = int(roadmapID) + except ValueError: + return False, render_template('error.html', message=LOCALIZATION['error_param_invalid']) + if roadmapID < 1: + return False, render_template('error.html', message=LOCALIZATION['error_param_invalid']) -@app.route('/roadmap/<roadmapID>') -def roadmap_by_id(roadmapID): - success, response = __check_roadmap(roadmapID) - if success: - return render_template('index.html', roadmap=response, localization=LOCALIZATION) + roadmap = requests.get(urlBuilder.build_url('roadmap', roadmapID, 'full')).json() + if roadmap is None: + return False, render_template('error.html', message=LOCALIZATION['error_roadmap_not_existing']) - return response - - -@app.route('/roadmap/<roadmapID>/fragment') -def roadmap_fragment_by_id(roadmapID): - success, response = __check_roadmap(roadmapID) - if success: - return render_template('roadmapFragment.html', roadmap=response, localization=LOCALIZATION) - - return response - - -def __check_roadmap(roadmapID): - try: - roadmapID = int(roadmapID) - except ValueError: - return False, render_template('error.html', message=LOCALIZATION['error_param_invalid']) - - if roadmapID < 1: - return False, render_template('error.html', message=LOCALIZATION['error_param_invalid']) - - roadmap = requests.get(URL_BUILDER.build_url('roadmap', roadmapID, 'full')).json() - if roadmap is None: - return False, render_template('error.html', message=LOCALIZATION['error_roadmap_not_existing']) - - return True, roadmap + return True, roadmap if __name__ == '__main__': - if SETTINGS['useSSL']: - http_server = WSGIServer((SETTINGS['listen'], - SETTINGS['port']), app, - keyfile=SETTINGS['keyfile'], - certfile=SETTINGS['certfile']) - else: - http_server = WSGIServer((SETTINGS['listen'], SETTINGS['port']), app) - - print('RoadmapClient {}({}) - Listening on {}:{}...'.format(VERSION['name'], VERSION['code'], - SETTINGS['listen'], SETTINGS['port'])) - http_server.serve_forever() + roadmapClient = RoadmapClient(Constants.APP_NAME, os.path.dirname(__file__), LOGGER, 'settings.json') + roadmapClient.start_server() diff --git a/client/settings-example.json b/client/settings-example.json index b80abed5852f14823fedf70df90b3c98e85c0389..bb13e6b76f95653694d931ac6eba7285f504a60a 100644 --- a/client/settings-example.json +++ b/client/settings-example.json @@ -1,9 +1,11 @@ { - "listen": "0.0.0.0", - "port": 11000, - "secret": "", - "useSSL": false, - "keyfile": "", - "certfile": "", - "apiURL": "" + "server": { + "listen": "0.0.0.0", + "port": 11000, + "secret": "", + "useSSL": false, + "keyfile": "", + "certfile": "", + "apiURL": "" + } } \ No newline at end of file