From 22d287f5f36a336752c3a8f5c81e89d1544a53f7 Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Mon, 2 Sep 2024 22:34:38 +0300 Subject: [PATCH] Adds Jicofo endpoint --- go/main.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/go/main.go b/go/main.go index b85b8ef..089910c 100644 --- a/go/main.go +++ b/go/main.go @@ -55,6 +55,20 @@ func getNginxState() string { return "not running" } +// getJicofoState checks the status of the Jicofo service +func getJicofoState() string { + output, err := exec.Command("systemctl", "is-active", "jicofo").Output() + if err != nil { + log.Printf("Error checking the jicofo state: %v", err) + return "error" + } + state := strings.TrimSpace(string(output)) + if state == "active" { + return "running" + } + return "not running" +} + // getNginxConnections gets the number of active connections to the specified web port func getNginxConnections(nginxPort int) int { cmd := fmt.Sprintf("netstat -an | grep ':%d' | wc -l", nginxPort) @@ -119,6 +133,17 @@ func nginxHandler(config Config, w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(data) } +// jicofoHandler handles the /jicofo endpoint +func jicofoHandler(config Config, w http.ResponseWriter, r *http.Request) { + data := JicofoData { + JicofoState: getJicofoState(), + JicofoAPIData: getJicofoAPIData(), + } + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(data) +} + + // main sets up the http server and the routes func main() { @@ -132,10 +157,13 @@ func main() { http.HandleFunc("/nginx", func(w http.ResponseWriter, r *http.Request) { nginxHandler(config, w, r) }) + http.HandleFunc("/jicofo", func(w http.ResponseWriter, r *http.Request) { + jicofoHandler(config, w, r) + }) // start the http server agentPortStr := fmt.Sprintf(":%d", config.AgentPort) - fmt.Printf("Starting agent server on port %d.\n", config.AgentPort) + fmt.Printf("Starting Jilo agent server on port %d.\n", config.AgentPort) if err := http.ListenAndServe(agentPortStr, nil); err != nil { log.Fatalf("Could not start the server: %v\n", err) }