Files
probo/pkg/uri/uri.go
Émile Ré 9156d6a16a Add wsl linter and fix
Signed-off-by: Émile Ré <emile@probo.com>
2026-05-20 09:27:28 +04:00

105 lines
2.5 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package uri
import (
"database/sql/driver"
"fmt"
"net/url"
"strings"
"golang.org/x/net/publicsuffix"
)
// URI is a validated absolute URI (scheme + host required).
type URI string
func Parse(raw string) (URI, error) {
u, err := url.Parse(raw)
if err != nil || u.Scheme == "" || u.Host == "" {
return "", fmt.Errorf("%q is not a valid URI", raw)
}
return URI(raw), nil
}
func (u URI) String() string { return string(u) }
func (u *URI) UnmarshalText(text []byte) error {
parsed, err := Parse(string(text))
if err != nil {
return err
}
*u = parsed
return nil
}
func (u URI) MarshalText() ([]byte, error) {
return []byte(u), nil
}
func (u *URI) Scan(value any) error {
var s string
switch v := value.(type) {
case string:
s = v
case []byte:
s = string(v)
default:
return fmt.Errorf("unsupported type for URI: %T", value)
}
parsed, err := Parse(s)
if err != nil {
return err
}
*u = parsed
return nil
}
func (u URI) Value() (driver.Value, error) {
return u.String(), nil
}
// ExtractDomain returns the eTLD+1 (effective top-level domain plus one
// label) from a raw URL string. For example:
//
// "https://www.googletagmanager.com/gtag/js" → "googletagmanager.com"
// "https://cdn.segment.io/v1/projects" → "segment.io"
//
// Returns an empty string when the URL cannot be parsed or has no valid
// hostname (e.g. data: URIs, bare IP addresses without a public suffix).
func ExtractDomain(rawURL string) string {
u, err := Parse(rawURL)
if err != nil {
return ""
}
parsed, _ := url.Parse(string(u))
hostname := strings.ToLower(parsed.Hostname())
domain, err := publicsuffix.EffectiveTLDPlusOne(hostname)
if err != nil {
return ""
}
return domain
}