generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
uri.go
37 lines (30 loc) · 772 Bytes
/
uri.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
// Package uri defines a simple uri type
package uri
import (
"fmt"
"strings"
)
// https://en.wikipedia.org/wiki/Uniform_Resource_Identifier .
type (
Host string
URL string
)
func NewHostFromString(host string) (Host, error) {
if strings.HasPrefix(host, "http") {
return "", fmt.Errorf("host can not start with 'http'")
}
return Host(host), nil
}
func (h Host) AddPrefix(prefix string) Host {
return Host(fmt.Sprintf("%s%s", prefix, h))
}
func (h Host) GetSlug() string {
return strings.Split(string(h), ".")[0]
}
func (h Host) GetRootHost() string {
domains := strings.Split(string(h), ".")
return strings.Join([]string{domains[len(domains)-2], domains[len(domains)-1]}, ".")
}
func (h Host) ToURL() URL {
return URL(fmt.Sprintf("https://%s", h))
}