Skip to content

Commit 3c1d590

Browse files
authored
Merge pull request #343 from porters-xyz/develop
POKTScan QoS endpoints
2 parents 7cb6626 + 22384a5 commit 3c1d590

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

gateway/common/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const (
2626
LOG_HTTP_RESPONSE = "LOG_HTTP_RESPONSE"
2727
FLY_API_KEY = "FLY_API_KEY"
2828
FLY_GATEWAY_URI = "FLY_GATEWAY_URI"
29+
GATEWAY_API_KEY = "GATEWAY_API_KEY"
30+
GATEWAY_REQUEST_API_KEY = "GATEWAY_REQUEST_API_KEY"
2931
)
3032

3133
// This may evolve to include config outside env, or use .env file for

gateway/proxy/kitMetricsKitRouter.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@ type Machine struct {
1818
Region string `json:"region"`
1919
}
2020

21-
func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl, region string) {
21+
func qosNodesHandler(w http.ResponseWriter, r *http.Request, proxyToUrl, region string) {
22+
// in order to call this endpoint, the user must pass the `GATEWAY_REQUEST_API_KEY` in the header
23+
expectedApiKey := common.GetConfig(common.GATEWAY_REQUEST_API_KEY)
24+
apiKey := r.Header.Get("api-key")
25+
26+
if apiKey == "" || apiKey != expectedApiKey {
27+
http.Error(w, "Unauthorized", http.StatusUnauthorized)
28+
return
29+
}
30+
2231
flyApiKey := common.GetConfig(common.FLY_API_KEY)
2332
// Fetch the machines from Fly.io
2433
machines, err := fetchMachines(flyApiKey)
@@ -44,7 +53,7 @@ func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl, regio
4453
log.Info("Retrieved Machine ID for gatewaykit", "Machine ID", machineID)
4554

4655
// Construct the metrics URL
47-
kitMetricsUrl := fmt.Sprintf("%s/metrics", proxyToUrl)
56+
kitMetricsUrl := fmt.Sprintf("%s/qosnodes", proxyToUrl)
4857

4958
log.Info("Calling metrics endpoint", "kitMetricsUrl", kitMetricsUrl)
5059

@@ -55,8 +64,11 @@ func kitMetricsHandler(w http.ResponseWriter, r *http.Request, proxyToUrl, regio
5564
return
5665
}
5766

58-
// Add the fly-force-instance-id header
67+
gatewayApiKey := common.GetConfig(common.GATEWAY_API_KEY)
68+
69+
//add the headers
5970
req.Header.Set("fly-force-instance-id", machineID)
71+
req.Header.Set("x-api-key", gatewayApiKey)
6072

6173
// Forward the request to the kit's /metrics endpoint
6274
client := &http.Client{}

gateway/proxy/mux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func addMetricsRoute(r *mux.Router) *mux.Router {
4747

4848
// Since the Gateway Kit is on an internal private network, with only the Gateway having access to it, we proxy a gateway-kit/metrics endpoint to expose the data to POKTScan
4949
func addMetricsKitRoute(r *mux.Router, proxyToUrl string) *mux.Router {
50-
subrouter := r.PathPrefix("/gateway-kit/metrics").Subrouter()
50+
subrouter := r.PathPrefix("/gateway-kit/qosnodes").Subrouter()
5151
subrouter.HandleFunc("/{region}", func(w http.ResponseWriter, r *http.Request) {
5252
region := mux.Vars(r)["region"]
53-
kitMetricsHandler(w, r, proxyToUrl, region)
53+
qosNodesHandler(w, r, proxyToUrl, region)
5454
})
5555
return subrouter
5656
}

0 commit comments

Comments
 (0)