diff --git a/config/config.yaml b/config/config.yaml index 042ccfd..03c8c9d 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,6 +1,7 @@ Server: host: 127.0.0.1 port: 8000 + scheme: http cors: False static_folder: frontend/build diff --git a/frontend/src/components/URLShortener/URLShortener.tsx b/frontend/src/components/URLShortener/URLShortener.tsx index 9f52852..98397d0 100644 --- a/frontend/src/components/URLShortener/URLShortener.tsx +++ b/frontend/src/components/URLShortener/URLShortener.tsx @@ -38,9 +38,8 @@ export default function URLShortener() { }) .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/shortenit/web.py b/shortenit/web.py index 50aa7f7..c9fdcac 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 @@ -62,6 +63,16 @@ class SiteHandler: 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,13 @@ 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)