Makes the self-check to use JWT

main
Yasen Pramatarov 2024-10-22 17:25:48 +03:00
parent fe057a1c47
commit 79355f039c
1 changed files with 27 additions and 3 deletions

30
main.go
View File

@ -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