From d2fb9a55651f319867315d8ed2a5c569d87bffbc Mon Sep 17 00:00:00 2001 From: Elia el Lazkani Date: Tue, 24 Dec 2024 01:27:21 +0100 Subject: [PATCH] enh(#8): Refactoring code --- config/config.yaml | 2 +- scripts/run.sh | 2 +- shortenit/main.py | 3 +-- shortenit/web.py | 11 +++++++++-- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index e0f5b4e..41d9b5b 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,11 +1,11 @@ Server: host: 127.0.0.1 port: 8000 + static_folder: frontend/build Web: host: 127.0.0.1 port: 8000 - static_folder: frontend/build Database: username: foo diff --git a/scripts/run.sh b/scripts/run.sh index 2864606..d9faa87 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -3,4 +3,4 @@ cd frontend npm run build cd .. -poetry run shortenit +poetry run shortenit "$@" diff --git a/shortenit/main.py b/shortenit/main.py index 6a9a7f6..ed0a8c0 100644 --- a/shortenit/main.py +++ b/shortenit/main.py @@ -54,8 +54,7 @@ def main() -> typing.NoReturn: def shorten_it(config: dict, session: Session, data: str, ttl: int): - shortener_config = config.get("Shortener", None) - shortener = Shortener(session, shortener_config) + shortener = Shortener(session, config) identifier = shortener.generate_uuid() if identifier: try: diff --git a/shortenit/web.py b/shortenit/web.py index 1df3079..4709b38 100644 --- a/shortenit/web.py +++ b/shortenit/web.py @@ -22,6 +22,7 @@ class Web: self.app.run(host=self.host, port=self.port, debug=self.debug) def init(self): + server_config = self.handler.configuration.get("Server", None) self.app = Flask(__name__) self.setup_routes() CORS(self.app) @@ -56,6 +57,9 @@ class SiteHandler: {trafaret.Key("url"): trafaret.URL, trafaret.Key("timestamp"): trafaret.Int} ) + def _get_server_config(self): + return self.configuration.get("Server", None) + def shortenit(self): data = request.get_json() try: @@ -67,7 +71,10 @@ class SiteHandler: abort(400) try: short_url = self.shorten_url( - self.configuration, self.database, data["url"], data["timestamp"] + self.configuration.get("Shortener", None), + self.database, + data["url"], + data["timestamp"], ) except KeyError as e: self.logger.error(e) @@ -101,7 +108,7 @@ class SiteHandler: try: project_root = Path(__file__).parent.parent static_folder = ( - f"{project_root}/" + self.configuration.get("Web")["static_folder"] + f"{project_root}/" + self._get_server_config()["static_folder"] ) if check_file(static_folder + "/" + path): return send_from_directory(static_folder, path)