-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.go
More file actions
51 lines (41 loc) · 1.11 KB
/
utils.go
File metadata and controls
51 lines (41 loc) · 1.11 KB
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
package main
import (
"fmt"
"net/http"
"regexp"
"strings"
)
func HostnameToHandle(hostname string) (Handle, error) {
domainParser := regexp.MustCompile(`^(.+?)\.(.+)$`)
parts := domainParser.FindStringSubmatch(strings.ToLower(hostname))
if len(parts) != 3 {
return Handle{}, fmt.Errorf("Handle could not be parsed from hostname %s", hostname)
}
return Handle{
Domain: Domain(parts[2]),
Username: Username(parts[1]),
}, nil
}
type URLTemplate string
func URLFromTemplate(
template URLTemplate,
request *http.Request,
handle Handle,
did DecentralizedID,
) string {
replacements := map[string]string{
"{handle}": handle.String(),
"{did}": string(did),
"{handle.domain}": string(handle.Domain),
"{handle.username}": string(handle.Username),
"{request.scheme}": string(request.URL.Scheme),
"{request.host}": string(request.Host),
"{request.path}": string(request.URL.Path),
"{request.query}": string(request.URL.RawQuery),
}
url := string(template)
for token, value := range replacements {
url = strings.ReplaceAll(url, token, value)
}
return url
}