Skip to content

Commit 865aa26

Browse files
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
1 parent adbc03b commit 865aa26

File tree

13 files changed

+199
-22
lines changed

13 files changed

+199
-22
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SOARCA_URI: http://localhost:8080
2+
GIN_MODE: "release"

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ docs/package-lock.json
2222
docs/.hugo_build.lock
2323
**.hugo_build.lock
2424
**_templ.go
25-
tmp/*
25+
tmp/*
26+
27+
public/public/styles.css

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ lint: build-templ
5656
GOFLAGS=-buildvcs=false golangci-lint run --timeout 5m0s -v
5757

5858
clean:
59-
find . -type f -name '*_templ.go' -exec rm -f {} ls **/*_templ.go
59+
find . -type f -name '*_templ.go' -exec rm -f {} \;
60+
6061

6162

6263
.DEFAULT_GOAL := dev

go.mod

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module soarca-gui
33
go 1.22.2
44

55
require (
6-
github.com/a-h/templ v0.2.707
6+
github.com/a-h/templ v0.2.747
77
github.com/gin-gonic/gin v1.10.0
88
)
99

@@ -18,6 +18,7 @@ require (
1818
github.com/go-playground/universal-translator v0.18.1 // indirect
1919
github.com/go-playground/validator/v10 v10.20.0 // indirect
2020
github.com/goccy/go-json v0.10.2 // indirect
21+
github.com/joho/godotenv v1.5.1
2122
github.com/json-iterator/go v1.1.12 // indirect
2223
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
2324
github.com/leodido/go-urn v1.4.0 // indirect
@@ -30,7 +31,7 @@ require (
3031
golang.org/x/arch v0.8.0 // indirect
3132
golang.org/x/crypto v0.23.0 // indirect
3233
golang.org/x/net v0.25.0 // indirect
33-
golang.org/x/sys v0.20.0 // indirect
34+
golang.org/x/sys v0.21.0 // indirect
3435
golang.org/x/text v0.15.0 // indirect
3536
google.golang.org/protobuf v1.34.1 // indirect
3637
gopkg.in/yaml.v3 v3.0.1 // indirect

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/a-h/templ v0.2.707 h1:T1Gkd2ugbRglZ9rYw/VBchWOSZVKmetDbBkm4YubM7U=
2-
github.com/a-h/templ v0.2.707/go.mod h1:5cqsugkq9IerRNucNsI4DEamdHPsoGMQy99DzydLhM8=
1+
github.com/a-h/templ v0.2.747 h1:D0dQ2lxC3W7Dxl6fxQ/1zZHBQslSkTSvl5FxP/CfdKg=
2+
github.com/a-h/templ v0.2.747/go.mod h1:69ObQIbrcuwPCU32ohNaWce3Cb7qM5GMiqN1K+2yop4=
33
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
44
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
55
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
@@ -30,6 +30,8 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG
3030
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
3131
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3232
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
33+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
34+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
3335
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
3436
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
3537
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
@@ -74,8 +76,8 @@ golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
7476
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
7577
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
7678
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
77-
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
78-
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
79+
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
80+
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7981
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
8082
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
8183
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=

handlers/dashboard.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func ReportingCard(context *gin.Context) {
3333
Value: 10,
3434
Name: "Executed Playbooks",
3535
}
36+
3637
render := utils.NewTempl(context, http.StatusOK, components.ReportingCard(updatedCard))
3738

3839
context.Render(http.StatusOK, render)

handlers/status.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package handlers
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
8+
"soarca-gui/utils"
9+
"soarca-gui/views/components"
10+
11+
"github.com/gin-gonic/gin"
12+
)
13+
14+
const (
15+
statusPingPath = "/status/ping"
16+
)
17+
18+
type statusHandler struct {
19+
Host string
20+
}
21+
22+
func NewStatusHandler(host string) statusHandler {
23+
return statusHandler{Host: host}
24+
}
25+
26+
func (s *statusHandler) HealthComponentHandler(context *gin.Context) {
27+
response, err := s.getPongFromStatus()
28+
indicatorData := components.HealthIndicatorData{Loaded: true}
29+
30+
switch {
31+
case err != nil:
32+
indicatorData.Healthy = false
33+
indicatorData.Message = "error on backend call"
34+
case response == "pong":
35+
indicatorData.Healthy = true
36+
indicatorData.Message = "connected"
37+
default:
38+
indicatorData.Healthy = false
39+
indicatorData.Message = "wrong msg backend"
40+
}
41+
42+
render := utils.NewTempl(context, http.StatusOK, components.HealthIndicator(indicatorData))
43+
context.Render(http.StatusOK, render)
44+
}
45+
46+
func (s *statusHandler) getPongFromStatus() (string, error) {
47+
response, err := http.Get(fmt.Sprintf("%s%s", s.Host, statusPingPath))
48+
if err != nil {
49+
return "", fmt.Errorf("failed to make GET request: %w", err)
50+
}
51+
defer response.Body.Close()
52+
53+
if response.StatusCode != http.StatusOK {
54+
return "", fmt.Errorf("unexpected status code: %d", response.StatusCode)
55+
}
56+
body, err := io.ReadAll(response.Body)
57+
if err != nil {
58+
return "", fmt.Errorf("failed to read response body: %w", err)
59+
}
60+
61+
return string(body), nil
62+
}

public/public/styles.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

routes/routes.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package routes
33
import (
44
"soarca-gui/handlers"
55
"soarca-gui/public"
6+
"soarca-gui/utils"
67

78
"github.com/gin-gonic/gin"
89
)
@@ -11,6 +12,7 @@ func Setup(app *gin.Engine) {
1112
publicRoutes := app.Group("/")
1213
PublicRoutes(publicRoutes)
1314
Reporting(publicRoutes)
15+
StatusGroup(publicRoutes)
1416
}
1517

1618
func PublicRoutes(app *gin.RouterGroup) {
@@ -33,3 +35,12 @@ func Reporting(app *gin.RouterGroup) {
3335
reportingRoute.GET("/reportingcard/:id", handlers.ReportingCard)
3436
}
3537
}
38+
39+
func StatusGroup(app *gin.RouterGroup) {
40+
statusHandler := handlers.NewStatusHandler(utils.GetEnv("SOARCA_URI", "http://localhost:8080"))
41+
42+
statusRoute := app.Group("/status")
43+
{
44+
statusRoute.GET("/indicator/card", statusHandler.HealthComponentHandler)
45+
}
46+
}

server/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ import (
66
"soarca-gui/routes"
77

88
"github.com/gin-gonic/gin"
9+
"github.com/joho/godotenv"
910
)
1011

1112
var (
1213
Version string
1314
Buildtime string
1415
)
1516

17+
// @title SOARCA-GUI
18+
// @version 0.0.1
1619
func main() {
20+
errenv := godotenv.Load(".env")
21+
22+
if errenv != nil {
23+
fmt.Println("Failed to read env variable, but will continue")
24+
}
25+
1726
app := gin.Default()
1827
routes.Setup(app)
1928

0 commit comments

Comments
 (0)