chore(#10): Delegates the task of setting the url to the backend API

* API now returns a full URL instead of a stub
* Frontend now returns the value from the API as is
* Configuration adds a scheme
This commit is contained in:
Elia el Lazkani 2024-12-25 15:29:32 +01:00
parent abbfbfbffb
commit 8dc4e41390
3 changed files with 16 additions and 4 deletions

View file

@ -1,6 +1,7 @@
Server:
host: 127.0.0.1
port: 8000
scheme: http
cors: False
static_folder: frontend/build

View file

@ -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);
}
});

View file

@ -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)