/
subdomain.go
53 lines (45 loc) 路 1.59 KB
/
subdomain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package middleware
import (
"errors"
"net/url"
"regexp"
"strings"
)
const domainURLRegexPattern = `^(https?:\/\/)?([_a-zA-Z][_a-zA-Z0-9-]{2,}\.)+[a-zA-Z0-9-]+\.[a-zA-Z0-9-]+(:[0-9]+)?$`
var domainURLRegex = regexp.MustCompile(domainURLRegexPattern)
// ExtractSubdomain extracts the subdomain from a given domain URL.
// It adds the scheme if absent and checks if the URL is valid.
// If the subdomain starts with "pg_", it returns an error indicating an invalid subdomain for schema name.
// If the URL has no subdomain, it returns an error indicating that there is no subdomain.
// Otherwise, it returns the extracted subdomain.
//
// Example:
//
// subdomain, err := middleware.ExtractSubdomain("http://test.domain.com")
// if err != nil {
// fmt.Println(err) // nil
// }
// fmt.Println(subdomain) // test
func ExtractSubdomain(domainURL string) (string, error) {
// Add scheme if absent
if !strings.HasPrefix(domainURL, "http://") && !strings.HasPrefix(domainURL, "https://") {
domainURL = "http://" + domainURL
}
// Check if URL is valid
if !domainURLRegex.MatchString(domainURL) {
return "", errors.New("invalid URL, URL should be in the format `http(s)://subdomain.domain.tld`")
}
u, err := url.Parse(domainURL)
if err != nil {
return "", err
}
hostParts := strings.Split(u.Hostname(), ".")
if len(hostParts) > 2 {
subdomain := strings.Join(hostParts[:len(hostParts)-2], ".")
if strings.HasPrefix(subdomain, "pg_") {
return "", errors.New("invalid subdomain for schema name, subdomain starts with `pg_`")
}
return subdomain, nil
}
return "", errors.New("no subdomain found")
}