/
snapshot_server.go
61 lines (51 loc) · 1.49 KB
/
snapshot_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
53
54
55
56
57
58
59
60
61
package entrypoint
import (
"context"
"encoding/json"
"fmt"
"net/http"
"sync/atomic"
snapshotTypes "github.com/datawire/ambassador/pkg/snapshot/v1"
"github.com/datawire/dlib/dhttp"
)
// take the next port in the range of ambassador ports.
const ExternalSnapshotPort = 8005
// expose a scrubbed version of the current snapshot outside the pod
func externalSnapshotServer(ctx context.Context, snapshot *atomic.Value) error {
mux := http.NewServeMux()
mux.HandleFunc("/snapshot-external", func(w http.ResponseWriter, r *http.Request) {
rawSnapshot := snapshot.Load().([]byte)
snapDecoded := snapshotTypes.Snapshot{}
err := json.Unmarshal(rawSnapshot, &snapDecoded)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = snapDecoded.Sanitize()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
sanitizedSnap, err := json.Marshal(snapDecoded)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("content-type", "application/json")
w.Write(sanitizedSnap)
})
s := &dhttp.ServerConfig{
Handler: mux,
}
return s.ListenAndServe(ctx, fmt.Sprintf(":%d", ExternalSnapshotPort))
}
func snapshotServer(ctx context.Context, snapshot *atomic.Value) error {
mux := http.NewServeMux()
mux.HandleFunc("/snapshot", func(w http.ResponseWriter, r *http.Request) {
w.Write(snapshot.Load().([]byte))
})
s := &dhttp.ServerConfig{
Handler: mux,
}
return s.ListenAndServe(ctx, "localhost:9696")
}