This commit is contained in:
Lizandro Guarnizo
2026-05-18 23:50:50 -05:00
parent bbe83c79ba
commit 7f309186d6
10 changed files with 73 additions and 22 deletions
+29 -5
View File
@@ -10,8 +10,21 @@ import (
"github.com/sujit-baniya/fiber-boilerplate/app"
)
// getAppURL devuelve la URL base absoluta (sin slash final).
// Si APP_URL es http://localhost sin puerto, añade APP_PORT para enlaces en desarrollo.
// GetPublicURL devuelve la URL pública para enlaces en correos, Telegram y webhooks.
func GetPublicURL() string {
return getPublicURL()
}
// getPublicURL usa APP_PUBLIC_URL si está configurada; si no, APP_URL (con puerto en localhost).
func getPublicURL() string {
if u := normalizeBaseURL(app.Http.Server.PublicUrl); u != "" {
return u
}
return getAppURL()
}
// getAppURL devuelve la URL base de la app (sin slash final).
// Si APP_URL es http://localhost sin puerto, añade APP_PORT para desarrollo local.
func getAppURL() string {
u := strings.TrimRight(app.Http.Server.Url, "/")
if u == "" {
@@ -33,10 +46,21 @@ func getAppURL() string {
return u
}
// absAppURL convierte una ruta relativa en URL absoluta para correo/Telegram.
func normalizeBaseURL(raw string) string {
u := strings.TrimSpace(strings.TrimRight(raw, "/"))
if u == "" {
return ""
}
if !strings.Contains(u, "://") {
u = "https://" + u
}
return strings.TrimRight(u, "/")
}
// absAppURL convierte una ruta relativa en URL absoluta pública (correo/Telegram).
func absAppURL(path string) string {
if path == "" {
return getAppURL()
return getPublicURL()
}
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
return path
@@ -44,7 +68,7 @@ func absAppURL(path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return getAppURL() + path
return getPublicURL() + path
}
func adminTicketPath(ticketID uint) string {
+31 -3
View File
@@ -19,7 +19,35 @@ func TestGetAppURLLocalhostAddsPort(t *testing.T) {
}
}
func TestAbsAppURLRelativePath(t *testing.T) {
func TestGetPublicURLPrefersPublicURL(t *testing.T) {
app.Http = &config.AppConfig{
Server: config.ServerConfig{
Url: "http://localhost:8080",
PublicUrl: "admin.u-site.app",
Port: "8080",
},
}
if got := getPublicURL(); got != "https://admin.u-site.app" {
t.Fatalf("getPublicURL() = %q, want https://admin.u-site.app", got)
}
}
func TestAbsAppURLUsesPublicURL(t *testing.T) {
app.Http = &config.AppConfig{
Server: config.ServerConfig{
Url: "http://localhost:8080",
PublicUrl: "https://admin.u-site.app",
Port: "8080",
},
}
got := absAppURL("/portal/proyecto/bot?tab=Tickets&ticket=1")
want := "https://admin.u-site.app/portal/proyecto/bot?tab=Tickets&ticket=1"
if got != want {
t.Fatalf("absAppURL() = %q, want %q", got, want)
}
}
func TestAbsAppURLFallsBackToAppURL(t *testing.T) {
app.Http = &config.AppConfig{
Server: config.ServerConfig{
Url: "http://localhost",
@@ -34,8 +62,8 @@ func TestAbsAppURLRelativePath(t *testing.T) {
}
func TestTelegramHTMLLinkEscapesAmpersand(t *testing.T) {
link := telegramHTMLLink("http://localhost:8084/portal/p?tab=Tickets&ticket=1", "Ver portal")
if link != `<a href="http://localhost:8084/portal/p?tab=Tickets&amp;ticket=1">Ver portal</a>` {
link := telegramHTMLLink("https://admin.u-site.app/portal/p?tab=Tickets&ticket=1", "Ver portal")
if link != `<a href="https://admin.u-site.app/portal/p?tab=Tickets&amp;ticket=1">Ver portal</a>` {
t.Fatalf("telegramHTMLLink() = %q", link)
}
}
+1 -1
View File
@@ -80,7 +80,7 @@ func GeneratePasswordResetURL(email string, baseURL string) string {
// SendPortalCredentialsEmail envía al usuario del portal su URL de acceso y contraseña inicial.
func SendPortalCredentialsEmail(email, nombre, password string) {
loginURL := fmt.Sprintf("%s/portal/login", app.Http.Server.Url)
loginURL := absAppURL("/portal/login")
htmlBody := fmt.Sprintf(`<!DOCTYPE html>
<html><body style="font-family:Inter,sans-serif;background:#f1f5f9;padding:32px">
<div style="max-width:480px;margin:0 auto;background:#fff;border-radius:16px;padding:32px;border:1px solid #e2e8f0">