diff --git a/config/config.yaml b/config/config.yaml index 042ccfd..e868f41 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,13 +1,10 @@ Server: host: 127.0.0.1 port: 8000 + scheme: http cors: False static_folder: frontend/build -Web: - host: 127.0.0.1 - port: 8000 - Database: username: foo password: bar diff --git a/frontend/src/components/URLShortener/URLShortener.tsx b/frontend/src/components/URLShortener/URLShortener.tsx index 492a964..7aa75e1 100644 --- a/frontend/src/components/URLShortener/URLShortener.tsx +++ b/frontend/src/components/URLShortener/URLShortener.tsx @@ -3,8 +3,9 @@ import "./URLShortener.css"; import axios from "axios"; import { ToastContainer, toast } from "react-toastify"; import "react-toastify/dist/ReactToastify.css"; +import Config from "../../config" -export default function () { +export default function URLShortener() { const [url, setUrl] = useState(""); const [shortenedUrl, setShortenedUrl] = useState(""); const [showInput, setShowInput] = useState(false); @@ -28,19 +29,20 @@ export default function () { } try { - const timestamp = Math.floor(Date.now() / 1000); + const config = Config() + const api_endpoint = "/api/v1/shorten"; + + const api_url = `${config.url}${api_endpoint}`; // Send the POST request to the backend await axios - .post("http://127.0.0.1:8000/api/v1/shorten", { - url: url, - timestamp: timestamp, + .post(api_url , { + url: url }) .then((response) => { if (response) { - const code: string = response.data.url; - const fullShortenedUrl: string = `http://127.0.0.1:8000/r/${code}`; - setShortenedUrl(fullShortenedUrl); + const shortUrl: string = response.data.url; + setShortenedUrl(shortUrl); setShowInput(true); } }); diff --git a/frontend/src/config.json b/frontend/src/config.json new file mode 100644 index 0000000..aba9fd4 --- /dev/null +++ b/frontend/src/config.json @@ -0,0 +1,12 @@ +{ + "frontend": { + "scheme": "http", + "host": "127.0.0.1", + "port": "8000" + }, + "api": { + "scheme": "http", + "host": "127.0.0.1", + "port": "8000" + } +} diff --git a/frontend/src/config.tsx b/frontend/src/config.tsx new file mode 100644 index 0000000..49984db --- /dev/null +++ b/frontend/src/config.tsx @@ -0,0 +1,24 @@ +import configuration from './config.json'; + +class APIConfig { + scheme: string; + host: string; + port: string; + url: string; + + constructor(scheme: string, host: string, port: string) { + this.scheme = scheme; + this.host = host; + this.port = port; + this.url = `${scheme}://${host}:${port}` + } +} + +export default function Config() { + const scheme = configuration.api.scheme; + const host = configuration.api.host; + const port = configuration.api.port; + + const apiConfig = new APIConfig(scheme, host, port); + return apiConfig +} diff --git a/shortenit/main.py b/shortenit/main.py index 1963860..7b8bc2b 100644 --- a/shortenit/main.py +++ b/shortenit/main.py @@ -1,10 +1,8 @@ #!/usr/bin/env python3 import argparse -import asyncio import logging import pathlib import sys -import time import typing from sqlalchemy import create_engine, exc @@ -53,7 +51,7 @@ def main() -> typing.NoReturn: sys.exit(0) -def shorten_it(config: dict, session: Session, data: str, ttl: int): +def shorten_it(config: dict, session: Session, data: str, ttl: int = 0): shortener = Shortener(session, config) identifier = shortener.generate_uuid() if identifier: diff --git a/shortenit/models/objects.py b/shortenit/models/objects.py index e7fc16c..716c107 100644 --- a/shortenit/models/objects.py +++ b/shortenit/models/objects.py @@ -21,7 +21,7 @@ class Pointer(base.Base): __tablename__ = "pointers" id: Mapped[int] = mapped_column(primary_key=True, index=True, init=False) data: Mapped[str] = mapped_column(index=True) - ttl: Mapped[str] + ttl: Mapped[int] link_id: Mapped[int] = mapped_column(ForeignKey("links.id")) link: Mapped["Link"] = relationship(back_populates="pointers") timestamp: Mapped[datetime] = mapped_column(default=func.now()) diff --git a/shortenit/web.py b/shortenit/web.py index 50aa7f7..f4920ab 100644 --- a/shortenit/web.py +++ b/shortenit/web.py @@ -1,5 +1,6 @@ import logging from pathlib import Path +from urllib.parse import urlunparse import trafaret from flask import Flask, abort, redirect, request, send_from_directory @@ -56,12 +57,22 @@ class SiteHandler: self.shorten_url = shorten_url self.lenghten_url = lenghten_url self.shortenit_load_format = trafaret.Dict( - {trafaret.Key("url"): trafaret.URL, trafaret.Key("timestamp"): trafaret.Int} + {trafaret.Key("url"): trafaret.URL} ) def _get_server_config(self): return self.configuration.get("Server", None) + def _get_host(self): + host = self._get_server_config()["host"] + port = self._get_server_config()["port"] + scheme = self._get_server_config()["scheme"] + return scheme, host, port + + def _get_url(self, stub): + scheme, host, port = self._get_host() + return urlunparse((scheme, f"{host}:{port}", f"/r/{stub}", "", "", "")) + def shortenit(self): data = request.get_json() try: @@ -72,12 +83,12 @@ class SiteHandler: self.logger.error(e) abort(400) try: - short_url = self.shorten_url( + stub = self.shorten_url( self.configuration.get("Shortener", None), self.database, data["url"], - data["timestamp"], ) + short_url = self._get_url(stub) except KeyError as e: self.logger.error(e) abort(400)