-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/7 status indicator in dashboard (#8)
* renaming of handler file + env file example + utility for env file reading * added gotenv * work on progress on health indicator in header bar based on backend connection * progress on health indicator in dashboard * scope change on indicator * fix reportingdashboard * fixed issue on make file + finished healthy indicator wrote handler and right polling * added styles auto gen to gitignore * removed styles from repo as auto generated * bump templ version * fix templ version
- Loading branch information
1 parent
1626377
commit 8a5cea8
Showing
13 changed files
with
199 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
SOARCA_URI: http://localhost:8080 | ||
GIN_MODE: "release" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"soarca-gui/utils" | ||
"soarca-gui/views/components" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
const ( | ||
statusPingPath = "/status/ping" | ||
) | ||
|
||
type statusHandler struct { | ||
Host string | ||
} | ||
|
||
func NewStatusHandler(host string) statusHandler { | ||
return statusHandler{Host: host} | ||
} | ||
|
||
func (s *statusHandler) HealthComponentHandler(context *gin.Context) { | ||
response, err := s.getPongFromStatus() | ||
indicatorData := components.HealthIndicatorData{Loaded: true} | ||
|
||
switch { | ||
case err != nil: | ||
indicatorData.Healthy = false | ||
indicatorData.Message = "error on backend call" | ||
case response == "pong": | ||
indicatorData.Healthy = true | ||
indicatorData.Message = "connected" | ||
default: | ||
indicatorData.Healthy = false | ||
indicatorData.Message = "wrong msg backend" | ||
} | ||
|
||
render := utils.NewTempl(context, http.StatusOK, components.HealthIndicator(indicatorData)) | ||
context.Render(http.StatusOK, render) | ||
} | ||
|
||
func (s *statusHandler) getPongFromStatus() (string, error) { | ||
response, err := http.Get(fmt.Sprintf("%s%s", s.Host, statusPingPath)) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to make GET request: %w", err) | ||
} | ||
defer response.Body.Close() | ||
|
||
if response.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("unexpected status code: %d", response.StatusCode) | ||
} | ||
body, err := io.ReadAll(response.Body) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
|
||
return string(body), nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package utils | ||
|
||
import "os" | ||
|
||
func GetEnv(key, fallback string) string { | ||
if value, ok := os.LookupEnv(key); ok { | ||
return value | ||
} | ||
return fallback | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package components | ||
|
||
|
||
|
||
import "fmt" | ||
|
||
|
||
const ( | ||
statusCardEndpoint = "/status/indicator/card" | ||
pollInterval = "every 30s" | ||
) | ||
type HealthIndicatorData struct { | ||
Loaded bool | ||
Message string | ||
Healthy bool | ||
} | ||
|
||
|
||
|
||
|
||
templ HealthIndicator(data HealthIndicatorData) { | ||
|
||
if !data.Loaded { | ||
<div | ||
hx-get={ string(templ.URL(fmt.Sprintf(statusCardEndpoint))) } | ||
hx-trigger="load" | ||
hx-swap="outerHTML" | ||
class="relative" | ||
> | ||
@indicator(false, "not loaded") | ||
</div> | ||
|
||
} else { | ||
<div | ||
hx-get={ string(templ.URL(fmt.Sprintf(statusCardEndpoint))) } | ||
hx-trigger={ pollInterval } | ||
hx-swap="innerHTML" | ||
class="relative" | ||
> | ||
@indicator(data.Healthy, data.Message) | ||
</div> | ||
} | ||
|
||
} | ||
|
||
templ indicator(healthOk bool, message string) { | ||
<span x-data="{ showTooltip: false }" | ||
@mouseover="showTooltip = true" | ||
@mouseout="showTooltip = false" | ||
class="relative flex items-center" | ||
x-bind:title="showTooltip ? '' : ''" | ||
> | ||
if healthOk { | ||
<div class="relative inline-flex items-center"> | ||
<span class="relative inline-flex rounded-full h-3 w-3 bg-green-500"></span> | ||
<span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span> | ||
|
||
<div x-show="showTooltip" class="absolute left-2 top-1/2 transform -translate-y-1/2 mt-5 p-1 bg-white text-center rounded-br-lg rounded-bl-lg rounded-tr-lg shadow-lg"> | ||
<h1 class="text-sm text-green-500 whitespace-nowrap">{ message }</h1> | ||
</div> | ||
</div> | ||
} else { | ||
<div class="relative inline-flex items-center"> | ||
<span class="relative inline-flex rounded-full h-3 w-3 bg-red-500"></span> | ||
<span class="animate-ping absolute inline-flex h-full w-full rounded-full bg-red-400 opacity-75"></span> | ||
|
||
<div x-show="showTooltip" class="absolute left-2 top-1/2 transform -translate-y-1/2 mt-5 p-1 bg-white text-center rounded-br-lg rounded-bl-lg rounded-tr-lg shadow-lg"> | ||
<h1 class="text-sm text-red-500 whitespace-nowrap">{ message }</h1> | ||
</div> | ||
</div> | ||
} | ||
</span> | ||
} |