-
Notifications
You must be signed in to change notification settings - Fork 4
/
heartbeatmsg.go
47 lines (39 loc) · 977 Bytes
/
heartbeatmsg.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/url"
"github.com/m-lab/go/content"
"github.com/m-lab/go/host"
v2 "github.com/m-lab/locate/api/v2"
)
// LoadRegistration downloads the registration data from the registration
// URL and matches it with the provided hostname.
func LoadRegistration(ctx context.Context, hostname string, url *url.URL) (*v2.Registration, error) {
h, err := host.Parse(hostname)
if err != nil {
return nil, err
}
if url == nil {
return nil, content.ErrUnsupportedURLScheme
}
provider, err := content.FromURL(ctx, url)
if err != nil {
return nil, err
}
exp, err := provider.Get(ctx)
if err != nil {
return nil, err
}
var registrations map[string]v2.Registration
err = json.Unmarshal(exp, ®istrations)
if err != nil {
return nil, err
}
if v, ok := registrations[h.String()]; ok {
v.Hostname = h.StringWithService()
return &v, nil
}
return nil, fmt.Errorf("hostname %s not found", hostname)
}