feat: terminada funcionalidad

actualmente funciona en dev falta cocinar deploy
This commit is contained in:
2026-03-17 11:18:22 -03:00
parent 5906cae45e
commit 68ce52f7c8
7 changed files with 87 additions and 12 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
front/node_modules
shorturls.db

View File

@@ -1,7 +1,18 @@
<script> <script>
import { url } from "./assets/hooks/getUrl";
import AcortadorForm from "./assets/lib/AcortadorForm.svelte"; import AcortadorForm from "./assets/lib/AcortadorForm.svelte";
import svelteLogo from "./assets/svelte.svg"; $effect(
import viteLogo from "./assets/vite.svg"; (async () => {
if (window.location.pathname.match(/^\/short\/url\/\d+$/)) {
const numero = window.location.pathname.split("/").pop();
// console.log(numero);
const res = await fetch(`${$url}/url/${numero}`, {
method: "GET",
});
console.log(res);
}
})(),
);
</script> </script>
<section id="center"> <section id="center">

View File

@@ -2,6 +2,12 @@ import { readable } from "svelte/store";
export const url = readable( export const url = readable(
typeof window !== "undefined" && window.location.hostname === "localhost" typeof window !== "undefined" && window.location.hostname === "localhost"
? "http://localhost:8080" ? "/short"
: "https://fedesrv.ddns.net/short", : "https://fedesrv.ddns.net/s",
);
export const redirect = readable(
typeof window !== "undefined" && window.location.hostname === "localhost"
? "http://localhost:5173/short"
: "https://fedesrv.ddns.net/s",
); );

View File

@@ -1,7 +1,9 @@
<script> <script>
import { url } from "../hooks/getUrl"; import { fade } from "svelte/transition";
import { redirect, url } from "../hooks/getUrl";
let input; let input;
let popup = $state("");
$effect(() => { $effect(() => {
input = document.getElementById("acortadorinput"); input = document.getElementById("acortadorinput");
}); });
@@ -20,11 +22,64 @@
}); });
if (res.ok) { if (res.ok) {
const data = await res.json(); const data = await res.json();
console.log(data.shortUrl); // console.log(data.shortUrl);
popup = data.shortUrl;
} }
} catch {} } catch {
popup = "Error";
}
}} }}
> >
<input type="text" id="acortadorinput" /> <input type="text" id="acortadorinput" />
<button type="submit"></button> <button type="submit"></button>
</form> </form>
{#if popup}
<div transition:fade class="popup-dialog">
<p>Short URL creada</p>
<button
onclick={() => {
navigator.clipboard.writeText(`${$redirect}/url/${popup}`);
popup = "";
}}
>
Copy</button
>
</div>
{/if}
<style>
.popup-dialog {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: var(--bg);
border: var(--border) 1px solid;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
z-index: 1000;
text-align: center;
}
.popup-dialog p {
margin: 0 0 15px 0;
font-size: 16px;
color: var(--text);
}
.popup-dialog button {
background-color: #007bff;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
}
.popup-dialog button:hover {
background-color: #0056b3;
}
</style>

View File

@@ -6,11 +6,11 @@ export default defineConfig({
plugins: [svelte()], plugins: [svelte()],
server: { server: {
proxy: { proxy: {
"/url": { "/short/": {
target: "http://localhost:3000", target: "http://localhost:8080",
changeOrigin: true, changeOrigin: true,
secure: false, secure: false,
rewrite: (path) => path.replace(/^\/url/, ""), rewrite: (path) => path.replace(/^\/short/, ""),
}, },
}, },
}, },

View File

@@ -23,7 +23,7 @@ func main() {
db.AutoMigrate(&models.Url{}) db.AutoMigrate(&models.Url{})
http.HandleFunc("/url", func(w http.ResponseWriter, r *http.Request){ http.HandleFunc("/url/", func(w http.ResponseWriter, r *http.Request){
routes.Url(w, r, db) routes.Url(w, r, db)
}) })

View File

@@ -34,8 +34,9 @@ func Url(w http.ResponseWriter, r *http.Request, db *gorm.DB) {
longUrl, err := repository.RecuperarUrl(paths[len(paths)-1], db) longUrl, err := repository.RecuperarUrl(paths[len(paths)-1], db)
if err != nil { if err != nil {
http.Error(w, "Error: "+err.Error(), http.StatusBadRequest) http.Error(w, "Error: "+err.Error(), http.StatusBadRequest)
return
} }
http.Redirect(w, r, longUrl, http.StatusPermanentRedirect) http.Redirect(w, r, longUrl, http.StatusMovedPermanently)
default: default:
http.Error(w, "404", http.StatusBadGateway) http.Error(w, "404", http.StatusBadGateway)