-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.go
52 lines (41 loc) · 1.13 KB
/
server.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package discovery
import (
"encoding/json"
"net/http"
"time"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/horizon/pkg/pb"
)
const HTTPPath = "/.well-known/horizon/hubs.json"
type GetNetlocs interface {
GetAllNetworkLocations() ([]*pb.NetworkLocation, error)
}
type WellKnown struct {
L hclog.Logger
GetNetlocs GetNetlocs
}
type DiscoveryData struct {
ServerTime time.Time `json:"server_time"`
Hubs []*pb.NetworkLocation `json:"hubs"`
}
func (wk *WellKnown) ServeHTTP(w http.ResponseWriter, req *http.Request) {
netlocs, err := wk.GetNetlocs.GetAllNetworkLocations()
if err != nil {
wk.L.Error("error getting network locations for well-known", "error", err)
http.Error(w, "unable to find network locations", http.StatusInternalServerError)
return
}
var dd DiscoveryData
dd.ServerTime = time.Now()
for _, loc := range netlocs {
if loc.IsPublic() {
dd.Hubs = append(dd.Hubs, loc)
}
}
err = json.NewEncoder(w).Encode(&dd)
if err != nil {
wk.L.Error("error encoding discovery data for well-known", "error", err)
}
}