From c058835f731b799f8c3abb0194b657e66a7a72ed Mon Sep 17 00:00:00 2001 From: Elia el Lazkani Date: Mon, 23 Dec 2024 02:15:43 +0100 Subject: [PATCH 1/3] chore(#8): Initial migration into the new frontend UI --- config/config.yaml | 1 + docs/conf.py | 25 ++-- .../components/URLShortener/URLShortener.tsx | 4 +- shortenit/templates/index.html | 121 ------------------ shortenit/web.py | 48 +++++-- 5 files changed, 55 insertions(+), 144 deletions(-) delete mode 100644 shortenit/templates/index.html diff --git a/config/config.yaml b/config/config.yaml index 93bfec7..09fa0e5 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -6,6 +6,7 @@ Server: Web: host: 127.0.0.1 port: 8000 + static_folder: frontend/build Database: username: foo diff --git a/docs/conf.py b/docs/conf.py index 0a415e5..aa77d65 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -6,29 +6,28 @@ # -- Project information ----------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information -project = 'shortenit' -copyright = '2024, Elia el Lazkani' -author = 'Elia el Lazkani' -release = '0.0.0' +project = "shortenit" +copyright = "2024, Elia el Lazkani" +author = "Elia el Lazkani" +release = "0.0.0" # -- General configuration --------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration extensions = [ - 'sphinx.ext.autodoc', - 'sphinx.ext.todo', - 'sphinx.ext.coverage', - 'sphinx.ext.viewcode', - 'sphinx_autodoc_typehints', + "sphinx.ext.autodoc", + "sphinx.ext.todo", + "sphinx.ext.coverage", + "sphinx.ext.viewcode", + "sphinx_autodoc_typehints", ] -templates_path = ['_templates'] +templates_path = ["_templates"] exclude_patterns = [] - # -- Options for HTML output ------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output -html_theme = 'alabaster' -html_static_path = ['_static'] +html_theme = "alabaster" +html_static_path = ["_static"] diff --git a/frontend/src/components/URLShortener/URLShortener.tsx b/frontend/src/components/URLShortener/URLShortener.tsx index a56dae2..492a964 100644 --- a/frontend/src/components/URLShortener/URLShortener.tsx +++ b/frontend/src/components/URLShortener/URLShortener.tsx @@ -32,14 +32,14 @@ export default function () { // Send the POST request to the backend await axios - .post("http://127.0.0.1:8000/shortenit", { + .post("http://127.0.0.1:8000/api/v1/shorten", { url: url, timestamp: timestamp, }) .then((response) => { if (response) { const code: string = response.data.url; - const fullShortenedUrl: string = `http://127.0.0.1:8000/${code}`; + const fullShortenedUrl: string = `http://127.0.0.1:8000/r/${code}`; setShortenedUrl(fullShortenedUrl); setShowInput(true); } diff --git a/shortenit/templates/index.html b/shortenit/templates/index.html deleted file mode 100644 index 0ba5307..0000000 --- a/shortenit/templates/index.html +++ /dev/null @@ -1,121 +0,0 @@ - - - - - - - - - - - ShortenIt - - - - - - - - - - - - - - - - - - -
-
-
-

Shorten It!

-
-
- - -
-
-
-
- -
-
- -
-
-
-
- - - -
-
-
-
- -
-
-
- -
-
-
-
- - - - - diff --git a/shortenit/web.py b/shortenit/web.py index 25a8030..3f662bc 100644 --- a/shortenit/web.py +++ b/shortenit/web.py @@ -1,10 +1,11 @@ import logging -import os from pathlib import Path -from flask_cors import CORS import trafaret -from flask import Flask, abort, redirect, render_template, request +from flask import Flask, abort, redirect, request, send_from_directory +from flask_cors import CORS + +from .common import check_file class Web: @@ -28,12 +29,21 @@ class Web: CORS(self.app) def setup_routes(self): - self.app.add_url_rule("/", "/", self.handler.index, methods=["GET"]) self.app.add_url_rule( - "/shortenit", "/shortenit", self.handler.shortenit, methods=["POST"] + "/", "/", self.handler.index, methods=["GET"], defaults={"path": ""} + ) + self.app.add_url_rule("/", "/", self.handler.index, methods=["GET"]) + self.app.add_url_rule( + "/static/css/", "css", self.handler.css, methods=["GET"] ) self.app.add_url_rule( - "/", "/identifier", self.handler.short_redirect, methods=["GET"] + "/static/js/", "js", self.handler.js, methods=["GET"] + ) + self.app.add_url_rule( + "/api/v1/shorten", "shorten", self.handler.shortenit, methods=["POST"] + ) + self.app.add_url_rule( + "/r/", "redirect", self.handler.short_redirect, methods=["GET"] ) @@ -75,7 +85,29 @@ class SiteHandler: abort(404) return redirect(url) - def index(self): - return render_template("index.html") + def index(self, path): + if path != "": + return self._fetch_from_directory(path) + else: + return self._fetch_from_directory("index.html") + def css(self, path): + path = "static/css/" + path + return self._fetch_from_directory(path) + def js(self, path): + path = "static/js/" + path + return self._fetch_from_directory(path) + + def _fetch_from_directory(self, path): + try: + project_root = Path(__file__).parent.parent + static_folder = ( + f"{project_root}/" + self.configuration.get("Web")["static_folder"] + ) + if check_file(static_folder + "/" + path): + return send_from_directory(static_folder, path) + else: + abort(404) + except: + abort(500) -- 2.45.2 From 000729a925119d0ec5e37214b508d77ae44e4a53 Mon Sep 17 00:00:00 2001 From: Elia el Lazkani Date: Mon, 23 Dec 2024 23:55:09 +0100 Subject: [PATCH 2/3] chore(#8): Adds scripts to install, build and run the project --- README.md | 13 ++++++++++--- scripts/install.sh | 7 +++++++ scripts/run.sh | 6 ++++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100755 scripts/install.sh create mode 100755 scripts/run.sh diff --git a/README.md b/README.md index d65723a..1a75f30 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,18 @@ Shortenit is a tool to shorten urls. **NOTE**: This is a very early draft project. Contributions are welcome. +## Install + +To install `shortenit` and all of its dependencies for development, run the following script. + +``` shell +$ scripts/install.sh +``` + ## Running -To run `shortenit`, edit the configuration file found in [config/config.yaml](config/config.yaml) then run the following commands. +To run `shortenit` for development, edit the configuration file found in [config/config.yaml](config/config.yaml) then run the following script. ```text -$ pip install -e . -$ shortenit +$ scripts/run.sh ``` diff --git a/scripts/install.sh b/scripts/install.sh new file mode 100755 index 0000000..0ad942e --- /dev/null +++ b/scripts/install.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +cd frontend +npm install + +cd .. +poetry install diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100755 index 0000000..2864606 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +cd frontend +npm run build +cd .. +poetry run shortenit -- 2.45.2 From a925c87872777ab5702949f54ad2dc0163d53a7f Mon Sep 17 00:00:00 2001 From: Elia el Lazkani Date: Tue, 24 Dec 2024 01:27:21 +0100 Subject: [PATCH 3/3] 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 09fa0e5..042ccfd 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -2,11 +2,11 @@ Server: host: 127.0.0.1 port: 8000 cors: False + 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 b3258a1..1963860 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 3f662bc..50aa7f7 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() if server_config and server_config.get("cors", False): @@ -58,6 +59,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: @@ -69,7 +73,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) @@ -103,7 +110,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) -- 2.45.2