Last active
November 6, 2020 10:25
-
-
Save boratanrikulu/7138e9674bf540449abed2718ce4ee3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// countryTopLevelDomains includes all country-code-top-level-domains. | |
// source: https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains#Country_code_top-level_domains | |
var countryTopLevelDomains = []string{ | |
"ac", "ad", "ae", "af", "ag", "ai", "al", "am", "ao", "aq", "ar", "as", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "bq", "br", "bs", "bt", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cw", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sk", "sl", "sm", "sn", "so", "sr", "ss", "st", "su", "sv", "sx", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "za", "zm", "zw", | |
} | |
// extractURL works like this: | |
// | |
// Given URL: "a.awesome.blog.boratanrikulu.dev.tr" | |
// Result: | |
// SUB_DOMAINS: a.awesome.blog | |
// DOMAIN: boratanrikulu" | |
// TLD: dev | |
// C-TLD: tr | |
func extractURL(url string) (string, string, string, string, error) { | |
parts := strings.Split(url, ".") | |
tldCount := 1 | |
// Check if it has valid country tld at the last part. | |
// Increase the tldCount if it does. | |
if _, c := stringSliceContains(countryTopLevelDomains, parts[len(parts)-1]); c { | |
tldCount++ | |
} | |
if tldCount >= len(parts) { | |
return "", "", "", "", errors.New("That's not a valid domain.") | |
} | |
// TLD | |
tld := parts[len(parts)-tldCount] | |
// Country TLD | |
ctld := "" | |
if tldCount == 2 { | |
ctld = parts[len(parts)-1] | |
} | |
// Domain | |
domain := parts[len(parts)-tldCount-1] | |
subDomains := strings.Join(parts[:len(parts)-tldCount-1], ".") | |
return subDomains, domain, tld, ctld, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment