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)