Fix URL across both ends #16

Merged
anthony merged 5 commits from fixurl into main 2024-12-25 14:53:21 +00:00
7 changed files with 63 additions and 19 deletions

View file

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

View file

@ -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<string>("");
const [shortenedUrl, setShortenedUrl] = useState<string>("");
const [showInput, setShowInput] = useState<boolean>(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);
}
});

12
frontend/src/config.json Normal file
View file

@ -0,0 +1,12 @@
{
"frontend": {
"scheme": "http",
"host": "127.0.0.1",
"port": "8000"
},
"api": {
"scheme": "http",
"host": "127.0.0.1",
"port": "8000"
}
}

24
frontend/src/config.tsx Normal file
View file

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

View file

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

View file

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

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