Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ae64072be4 | |||
| 0785d5820a | |||
| d392412c52 | |||
| 248d6c6d19 | |||
| e9566dcc62 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
|||||||
front/node_modules
|
front/node_modules
|
||||||
shorturls.db
|
shorturls.db
|
||||||
|
front/dist
|
||||||
|
url-short
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
href="https://fonts.googleapis.com/css2?family=Instrument+Serif:ital@0;1&display=swap"
|
href="https://fonts.googleapis.com/css2?family=Instrument+Serif:ital@0;1&display=swap"
|
||||||
rel="stylesheet"
|
rel="stylesheet"
|
||||||
/>
|
/>
|
||||||
<title>front</title>
|
<title>Acortador de Urls</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="app"></div>
|
<div id="app"></div>
|
||||||
|
|||||||
@@ -1,18 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
import { url } from "./assets/hooks/getUrl";
|
import { url } from "./assets/hooks/getUrl";
|
||||||
import AcortadorForm from "./assets/lib/AcortadorForm.svelte";
|
import AcortadorForm from "./assets/lib/AcortadorForm.svelte";
|
||||||
$effect(
|
|
||||||
(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">
|
||||||
|
|||||||
@@ -8,6 +8,6 @@ export const url = readable(
|
|||||||
|
|
||||||
export const redirect = readable(
|
export const redirect = readable(
|
||||||
typeof window !== "undefined" && window.location.hostname === "localhost"
|
typeof window !== "undefined" && window.location.hostname === "localhost"
|
||||||
? "http://localhost:5173/short"
|
? "http://localhost:8080/"
|
||||||
: "https://fedesrv.ddns.net/s",
|
: "https://fedesrv.ddns.net/s",
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -13,10 +13,10 @@
|
|||||||
onsubmit={async (e) => {
|
onsubmit={async (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
// console.log(e.target[0].value);
|
// console.log(e.target[0].value);
|
||||||
// console.log(input);
|
// console.log($url);
|
||||||
const req = { longUrl: input.value };
|
const req = { longUrl: input.value };
|
||||||
try {
|
try {
|
||||||
const res = await fetch($url + "/url", {
|
const res = await fetch(`${$url}/url/`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
body: JSON.stringify({ ...req }),
|
body: JSON.stringify({ ...req }),
|
||||||
});
|
});
|
||||||
@@ -30,21 +30,33 @@
|
|||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
<button type="reset">❌</button>
|
||||||
<input type="text" id="acortadorinput" />
|
<input type="text" id="acortadorinput" />
|
||||||
<button type="submit">↪</button>
|
<button type="submit">↪</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{#if popup}
|
{#if popup}
|
||||||
<div transition:fade class="popup-dialog">
|
<div transition:fade class="popup-dialog">
|
||||||
<p>Short URL creada</p>
|
{#if popup == "Error"}
|
||||||
<button
|
<p>Fallo al crear la url</p>
|
||||||
onclick={() => {
|
<button
|
||||||
navigator.clipboard.writeText(`${$redirect}/url/${popup}`);
|
onclick={() => {
|
||||||
popup = "";
|
popup = "";
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Copy</button
|
Cerrar</button
|
||||||
>
|
>
|
||||||
|
{:else}
|
||||||
|
<p>Short URL creada</p>
|
||||||
|
<button
|
||||||
|
onclick={() => {
|
||||||
|
navigator.clipboard.writeText(`${$redirect}/url/${popup}`);
|
||||||
|
popup = "";
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Copiar</button
|
||||||
|
>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
@@ -82,4 +94,8 @@
|
|||||||
.popup-dialog button:hover {
|
.popup-dialog button:hover {
|
||||||
background-color: #0056b3;
|
background-color: #0056b3;
|
||||||
}
|
}
|
||||||
|
button,
|
||||||
|
input {
|
||||||
|
padding: 0.25rem;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { svelte } from "@sveltejs/vite-plugin-svelte";
|
|||||||
|
|
||||||
// https://vite.dev/config/
|
// https://vite.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
|
base: "./",
|
||||||
plugins: [svelte()],
|
plugins: [svelte()],
|
||||||
server: {
|
server: {
|
||||||
proxy: {
|
proxy: {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package repository
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/rand/v2"
|
||||||
"url-short/models"
|
"url-short/models"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -38,7 +39,18 @@ func CrearUrl(longURL string, db *gorm.DB) (string, error) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
var url models.Url
|
var url models.Url
|
||||||
result = db.Create(&models.Url{Longurl: longURL}).Scan(&url)
|
for {
|
||||||
|
url.Shorturl = int64(rand.Uint64())
|
||||||
|
result = db.Model(&models.Url{}).Where("shorturl = ?", url.Shorturl).Count(&counturl)
|
||||||
|
if result.Error != nil {
|
||||||
|
return "", result.Error
|
||||||
|
}
|
||||||
|
if counturl == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = db.Create(&models.Url{Longurl: longURL, Shorturl: url.Shorturl}).Scan(&url)
|
||||||
shortURL := fmt.Sprintf("%d", url.Shorturl)
|
shortURL := fmt.Sprintf("%d", url.Shorturl)
|
||||||
|
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user