Files
2026-05-18 23:50:50 -05:00

92 lines
2.2 KiB
Go

package services
import (
"fmt"
"html"
"net"
"net/url"
"strings"
"github.com/sujit-baniya/fiber-boilerplate/app"
)
// 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 == "" {
return fmt.Sprintf("http://localhost:%s", app.Http.Server.Port)
}
parsed, err := url.Parse(u)
if err != nil || parsed.Host == "" {
return u
}
host := parsed.Hostname()
if parsed.Port() == "" && app.Http.Server.Port != "" {
if host == "localhost" || host == "127.0.0.1" {
parsed.Host = net.JoinHostPort(host, app.Http.Server.Port)
return strings.TrimRight(parsed.String(), "/")
}
}
return u
}
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 getPublicURL()
}
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
return path
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
return getPublicURL() + path
}
func adminTicketPath(ticketID uint) string {
return fmt.Sprintf("/app/tickets?ticket=%d", ticketID)
}
func portalTicketPath(proyectoSlug string, ticketID uint) string {
if proyectoSlug == "" {
return "/portal/dashboard"
}
return fmt.Sprintf("/portal/proyecto/%s?tab=Tickets&ticket=%d", url.PathEscape(proyectoSlug), ticketID)
}
func escapeTelegramHTML(s string) string {
return html.EscapeString(s)
}
func telegramHTMLLink(absURL, label string) string {
return fmt.Sprintf(`<a href="%s">%s</a>`, escapeTelegramHTML(absURL), escapeTelegramHTML(label))
}