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:
parent
abbfbfbffb
commit
8dc4e41390
3 changed files with 16 additions and 4 deletions
|
@ -1,6 +1,7 @@
|
||||||
Server:
|
Server:
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: 8000
|
port: 8000
|
||||||
|
scheme: http
|
||||||
cors: False
|
cors: False
|
||||||
static_folder: frontend/build
|
static_folder: frontend/build
|
||||||
|
|
||||||
|
|
|
@ -38,9 +38,8 @@ export default function URLShortener() {
|
||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
if (response) {
|
if (response) {
|
||||||
const code: string = response.data.url;
|
const shortUrl: string = response.data.url;
|
||||||
const fullShortenedUrl: string = `http://127.0.0.1:8000/r/${code}`;
|
setShortenedUrl(shortUrl);
|
||||||
setShortenedUrl(fullShortenedUrl);
|
|
||||||
setShowInput(true);
|
setShowInput(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from urllib.parse import urlunparse
|
||||||
|
|
||||||
import trafaret
|
import trafaret
|
||||||
from flask import Flask, abort, redirect, request, send_from_directory
|
from flask import Flask, abort, redirect, request, send_from_directory
|
||||||
|
@ -62,6 +63,16 @@ class SiteHandler:
|
||||||
def _get_server_config(self):
|
def _get_server_config(self):
|
||||||
return self.configuration.get("Server", None)
|
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):
|
def shortenit(self):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
try:
|
try:
|
||||||
|
@ -72,12 +83,13 @@ class SiteHandler:
|
||||||
self.logger.error(e)
|
self.logger.error(e)
|
||||||
abort(400)
|
abort(400)
|
||||||
try:
|
try:
|
||||||
short_url = self.shorten_url(
|
stub = self.shorten_url(
|
||||||
self.configuration.get("Shortener", None),
|
self.configuration.get("Shortener", None),
|
||||||
self.database,
|
self.database,
|
||||||
data["url"],
|
data["url"],
|
||||||
data["timestamp"],
|
data["timestamp"],
|
||||||
)
|
)
|
||||||
|
short_url = self._get_url(stub)
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
self.logger.error(e)
|
self.logger.error(e)
|
||||||
abort(400)
|
abort(400)
|
||||||
|
|
Loading…
Reference in a new issue