From 79355f039c13bc011a7e13e38acf3e2f4284d40a Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Tue, 22 Oct 2024 17:25:48 +0300 Subject: [PATCH] Makes the self-check to use JWT --- main.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 4edc348..7d7cd1b 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "log" "net/http" + "net/http/httptest" "os" "os/exec" "strconv" @@ -242,17 +243,40 @@ func statusHandler(config Config, w http.ResponseWriter, r *http.Request) { endpoints := []string{"nginx", "prosody", "jicofo", "jvb", "jibri"} for _, endpoint := range endpoints { endpointURL := fmt.Sprintf("%s://localhost:%d/%s", protocol, config.AgentPort, endpoint) - resp, err := http.Get(endpointURL) + + req, err := http.NewRequest(http.MethodGet, endpointURL, nil) if err != nil { endpointStatuses[endpoint] = "not available" continue } - defer resp.Body.Close() - if resp.StatusCode == http.StatusOK { + + // Copy the JWT token from the original request + req.Header.Set("Authorization", r.Header.Get("Authorization")) + + // Create a response recorder to capture the response + rr := httptest.NewRecorder() + + // Call the respective handler with the new request + switch endpoint { + case "nginx": + nginxHandler(config, rr, req) + case "prosody": + prosodyHandler(config, rr, req) + case "jicofo": + jicofoHandler(config, rr, req) + case "jvb": + jvbHandler(config, rr, req) + case "jibri": + jibriHandler(config, rr, req) + } + + // Check the status code from the response recorder + if rr.Result().StatusCode == http.StatusOK { endpointStatuses[endpoint] = "available" } else { endpointStatuses[endpoint] = "not available" } + } // Prepare the response data and send back the JSON